Forum Moderators: phranque

Message Too Old, No Replies

Redirect non www to www issue

         

setjo

5:23 pm on Feb 21, 2014 (gmt 0)

10+ Year Member



I want to redirect non www to www and index.html to root but it is not working, I have used the following code but when I type http://example.com/about-us.html it redirects to http://www.example.com/html/about-us.html

Why it adds HTML in the url

Here is the code

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ http://www.example.com/$1 [R=301,L]

[edited by: aakk9999 at 5:33 pm (utc) on Feb 21, 2014]
[edit reason] Please use example.com to avoid autolinking [/edit]

phranque

6:29 pm on Feb 21, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, setjo!


do you have any other mod_rewrite or mod_alias directives in your configuration?

you should switch the order of those rulesets.
you want to order them from most specific to most general.

if you have any internal rewrite they should come after the external redirects to avoid exposing internal urls or file paths.

lucy24

9:32 pm on Feb 21, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



it redirects to http://www.example.com/html/about-us.html

That definitely sounds like a physical filepath. Do your error logs --assuming they note all 404s-- say the same thing? I don't know if this is universally true, but in my error logs they always show the physical filepath while regular access logs show the URL. So the error logs might say something like /html/html/filename.html representing the two occurrences of "html", one in the filepath and one in the (erroneous) URL.