.htaccess rewrite www to non-www and remove .html
I need rewrite rules to redirect the following:
http://www.
to http://
/file.html
to /file
I've tried using a combination of these but each time it results in a redirect loop on one of the situations
RewriteBase /
RewriteRule ^(.*).html$ $1 [NC]
RewriteCond %HTTP_HOST ^www.domain.co.uk [NC, L]
RewriteRule ^(.*)$ http://domain.co.uk/$1 [R=301]
I figure it's probably something to do with the flags but I don't know how to fix it. Just to be clear it needs to do all these situations:
http://www.domain.co.uk to http://domain.co.uk
http://www.domain.co.uk/file.html to http://domain.co.uk/file
http://domain.co.uk to http://domain.co.uk
http://domain.co.uk/file.html to http://domain.co.uk/file
Thanks!
Here's my real-life rule, and I strongly suggests to "force" the "www" because when you'll want to optimize your website you'll see you'll always need "www". Read the advices of the YSlow (Yahoo Slow) plugin, and you'll understand why it's technically better to use www (=> website optimization).
RewriteCond %HTTP_HOST ^mysite.(fr|com|net|org|eu) [NC]
# Without www => add "www" :
RewriteRule (.*) http://www.mysite.%1$1 [QSA,R=301,L]
So your rule may be (once again you should avoid that):
RewriteCond %HTTP_HOST ^www.mysite.(fr|com|net|org|eu) [NC]
# With www => remove "www" :
RewriteRule (.*) http://mysite.%1$1 [QSA,R=301,L]
Now you're sure the rules after those ones will be executed only if the host begins with "www".
So here's the last rule:
RewriteRule ([A-Za-z]+) $1.htm [QSA]
... which means: "if there are only alphabetical characters, add ".htm" at the end (and add the query string too).
Edit: thanks to Odant: if you're using an .htaccess file you'll need to change
RewriteRule (.*) http://www.mysite.%1$1 [QSA,R=301,L]
into this
RewriteRule (.*) http://www.mysite.%1/$1 [QSA,R=301,L]
To correct @OlivierPons post,
what you need is to strip the html extension off, you can achieve this doing like :
RewriteRule ^([^/]+).html$ $1 [QSA,L]
(again using an .htaccess
file)
Comments
Post a Comment