Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


share|improve this question
The asker just asked the same question over on SO. – Nick Q. Nov 9 '11 at 21:09
Is it not relevant on both sites? – samlester Nov 9 '11 at 21:20
No. .htaccess/mod_rewrite questions belong here. Plus cross-posting is very frowned upon. – John Conde Nov 9 '11 at 21:41
up vote 1 down vote accepted

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]
share|improve this answer
as a note, 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] – 발렌탕 Nov 14 '11 at 15:46
Thank you very much for the note, I seldom use .htaccess files (they slow down Apache), I'm using only vhost files. – Olivier Pons Nov 14 '11 at 21:32

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)

share|improve this answer
I think he asked for the opposite but in many rewrite rule questions, people are confused and ask the wrong way ;) Thanks for your comment I'll add that to my answer – Olivier Pons Nov 14 '11 at 16:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.