0

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!

3
  • 2
    The asker just asked the same question over on SO.
    – Nick Q.
    Nov 9, 2011 at 21:09
  • Is it not relevant on both sites?
    – samlester
    Nov 9, 2011 at 21:20
  • 3
    No. .htaccess/mod_rewrite questions belong here. Plus cross-posting is very frowned upon.
    – John Conde
    Nov 9, 2011 at 21:41

2 Answers 2

1

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]
2
  • 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]
    – vdegenne
    Nov 14, 2011 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. Nov 14, 2011 at 21:32
0

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)

1
  • 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 Nov 14, 2011 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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