Forum Moderators: phranque

Message Too Old, No Replies

Problem with 301 redirect

Edited my .htaccess file and now my WWW.example.com "isn't found"

         

vaulter

8:27 am on Mar 4, 2011 (gmt 0)

10+ Year Member



I've been searching the internet for some time now trying to find some solid instructions on 301 redirects. I pasted the following into my .htaccess file in an attempt to redirect the non-www, and www.example.com/index.html versions to www.example.html:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

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


The redirects worked, but Chrome and Firefox now tell me that www.example.com is "not found"

Help?!

g1smd

9:06 pm on Mar 4, 2011 (gmt 0)

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



The index redirect does not redirect for index requests in folders, only the root.

The domain canonicalisation redirect does not fix non-canonical requests with a port number.

Try:

# Externally redirect only direct client requests for */index.php
# and */index.html and */index.htm to URL ending with slash.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(html?|php)\ HTTP/
RewriteRule ^(([^/]+/)*)index.(html?|php)$ http://www.example.com/$1 [R=301,L]


# Externally redirect to canonicalize the domain name if a non-canonical
# hostname is requested, in order to prevent duplicate-content problems
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

vaulter

1:00 am on Mar 5, 2011 (gmt 0)

10+ Year Member



Thanks g1smd! That worked like a charm. Any advice on the mod rewrite that gets rid of the ".html" file extension from each file on the site?

g1smd

1:33 am on Mar 5, 2011 (gmt 0)

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



The most major point is that
mod_rewrite
cannot "get rid of" anything.

The first step is to link to the URLs that you want users to "see" and "use" by altering the links on the pages of your site. It is those links that define the URLs within your site.

The job of
mod_rewrite
is to use the RegEx pattern matching such that one rule matches the current request and rewrites that request to fetch content from elsewhere inside the server.

That is,
mod_rewrite
translates or "rewrites" an incoming URL request into a non-default internal server location, as compared to that suggested by the path part of the requested URL.

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


In this case, an incoming request for an extensionless URL is rewritten to fetch content from a file with the .html extension.

That is exactly backwards to most people's perception of what
mod_rewrite
does.



However, If you don't take extra steps to prevent it happening, people will still be able to also access your content with the .html extension present on the end of their URL request.

That can be taken care of with a redirect:

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


The ".html to extensionless" redirect goes ahead of your standard "non-www to www" redirect.

All of your redirects go ahead of the internal rewrite code.