.htaccess rewrite www to non-www and remove .html

.htaccess rewrite www to non-www and remove .html - If a page has internal and external outgoing links to redirecting URLs, it’s returning 3xx (301, 302, etc.) HTTP status codes standing for redirection. This issue means that the page does not exist on a permanent or temporary basis. It appears on most of the popular web browsers, usually caused by a misconfigured website. However, there are some steps you can take to ensure the issue isn’t on your side. You can find more details about redirecting URLs by reading the Google Search Central overview. In this article, we’ll go over how you can fix the .htaccess rewrite www to non-www and remove .html error on your web browser. Problem :


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!


Solution :

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)


We hope that this article has helped you resolve the htaccess, redirects, mod-rewrite error in your web browsers. Enjoy browsing the internet uninterrupted!

Comments

Popular posts from this blog

Redirected urls show in serp, with the original permalink but with the title and meta of the target page

How can I redirect everything but the index as 410?

How do I redirect traffic only if being accessed from a specific port?