views:

63

answers:

2

I have a small issue with some .htaccess rules on our website - http://www.presencemultimedia.co.uk

We've recently re-built the website using CodeIgniter. To use nice URLs I've added some lines to our .htaccess file as below :

RewriteEngine on

# CodeIgniter rules (forwards requests to index.php)
RewriteCond $1 !^(index\.php|images|robots\.txt|public)
RewriteRule ^(.*)$ /index.php/$1 [L]

# rewrites path to our primary domain (www.presencemultimedia.co.uk)
RewriteCond %{http_host} !www.presencemultimedia.co.uk$
RewriteRule ^(.*)$ http://www.presencemultimedia.co.uk/$1 [r=301,nc]

The second rewrite rule is designed to ensure the domain is always our primary domain (www.presencemultimedia.co.uk).

The issue I have is that if the website is accessed by an aliased domain, e.g. http://www.prmulti.com, the URL is rewritten to the primary domain but adds /index.php/ to the path.

For example - http://www.prmulti.com/about/ should rewrite to http://www.presencemultimedia.co.uk/about/ instead of http://www.presencemultimedia.co.uk/index.php/about

Can anyone see where I'm going wrong?

Cheers, Phil

+2  A: 

I would have thought the 301 re-direct should be above your CodeIgniter rules, giving:

RewriteEngine on

# rewrites path to our primary domain (www.presencemultimedia.co.uk)
RewriteCond %{http_host} !www.presencemultimedia.co.uk$
RewriteRule ^(.*)$ http://www.presencemultimedia.co.uk/$1 [r=301,nc]

# CodeIgniter rules (forwards requests to index.php)
RewriteCond $1 !^(index\.php|images|robots\.txt|public)
RewriteRule ^(.*)$ /index.php/$1 [L]

I recently developed a multi-site framework for a website, and that's the order of re-writing I'm using: domain first, then direct the result through my index.php. The theory should be the same for an off-the-shelf framework like CodeIgniter.

Martin Bean
This sounds correct. The observed behaviour matches this explanation. In that the request comes in to the alternative domain, is 302'd to http://www.prmulti.com/index.php/ then 301'd to http://www.presencemultimedia.co.uk/index.php//. The [L] on your first rule would ensure it's the last rule, so it won't reach anything afterwards.
Garethr
Hmm... this returns a 301 for www.prmulti.com now (essentially any domain which isn't www.presencemultimedia.co.uk) - http://cl.ly/307e
Phil
Hi Martin - Craig's code did the job. As far as I can tell, the only major difference to yours is the L flag added to the first rewrite rule. Not sure why this worked though...
Phil
I think it may be a cascade issue. Although the first condition was met, the second was also met. When placing the `L` flag on the domain direction honoured that request on none others; starting the process over again in which the first request didn't match, but the second did. Hope that makes sense! And if I'm wrong, I'm sure someone will correct me.
Martin Bean
Sounds plausible to me! Thanks again :)
Phil
+3  A: 

I had luck with this configuration:

RewriteEngine on

# rewrites path to our primary domain (www.presencemultimedia.co.uk)
RewriteCond %{http_host} !www.presencemultimedia.co.uk$
RewriteRule ^(.*)$ http://www.presencemultimedia.co.uk/$1 [r=301,nc,l]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

If the domain rule matches, redirect (but making it the last rule). Then on the correct domain, if the request isn't an existing file or directory, pass to Code Igniter.

Craig A Rodway
I does indeed work - cheers :) I had to replace the last 2 RewriteCond lines with the CodeIgniter line : RewriteCond $1 !^(index\.php|images|robots\.txt|public) (to exclude some requests from being routed to index.php)Cheers!
Phil
Cool. I tried the two !-f/!-d conditions (as I've used those before) after the single (images|public...) entry gave me a 500 here - not sure why.
Craig A Rodway