Mobile app version of vmapp.org
Login or Join
Vandalay111

: .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

@Vandalay111

Posted in: #Htaccess #ModRewrite #Redirects #UrlRewriting

I need rewrite rules to redirect the following:
www. to
/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$ [NC]

RewriteCond %{HTTP_HOST} ^www.domain.co.uk [NC, L]
RewriteRule ^(.*)$ domain.co.uk/ [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:
www.domain.co.uk to domain.co.uk http://www.domain.co.uk/file.html to domain.co.uk/file http://domain.co.uk to domain.co.uk http://domain.co.uk/file.html to domain.co.uk/file

Thanks!

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Vandalay111

2 Comments

Sorted by latest first Latest Oldest Best

 

@Sue5673885

To correct @OlivierPons post,
what you need is to strip the html extension off, you can achieve this doing like :

RewriteRule ^([^/]+).html$ [QSA,L]


(again using an .htaccess file)

10% popularity Vote Up Vote Down


 

@Hamm4606531

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 (.*) www.mysite.%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 (.*) mysite.%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]+) .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 (.*) www.mysite.%1 [QSA,R=301,L]


into this

RewriteRule (.*) www.mysite.%1/ [QSA,R=301,L]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme