Forum Moderators: phranque
redirect 301 http://www.example.com/index.shtml http://www.example.com
RewriteCond %{HTTP_HOST} ^domain\.com/index.shtm
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,L]
I have tried several variations of the above examples and I get one of two results, either a 500 internal server error, or no redirect or rewrite.
I already have the following things working:
AddHandler server-parsed .htm .html .shtml
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?otherdomain\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,L]
RewriteCond %{QUERY_STRING} ^([A-Za-z0-9])(.*)$
RewriteRule ^(.*)?(.*)$ http://www.example.com/$1? [R=301,L]
the problem is that google has my main index page indexed several times, like this:
www.example.com
example.com
www.example.com/index.shtml
example.com/index.shtml
example.com/?ppc-tracking=url
www.example.com/index.shtml?ppc-tracking=url
etc.
the things I have working now are getting rid of the ppc tracking urls and also the www/non-www problem but I can't seem to find a solution for the index.shtml problem
[edited by: jdMorgan at 4:05 pm (utc) on May 16, 2005]
[edit reason] Examplified. [/edit]
AddHandler server-parsed .htm .html .shtml
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
# Redirect client requests for /index.shtml to "/" in main doamin
# (This will not affect internal rewrites)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.shtml
RewriteRule ^index\.shtml$ http://www.example.com/ [R=301,L]
#
# Redirect all requests for alternate domain names to 'favored' domain to prevent duplicate content in SEs
# (Removed the filename from your RewriteCond - it will not be present in %{HTTP+HOST})
RewriteCond %{HTTP_HOST} ^example\.com [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?otherdomain\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Remove query string (guessing -- not sure if this is what you intended).
RewriteCond %{QUERY_STRING} ^[a-z0-9] [NC]
RewriteRule (.*) http://www.example.com/$1? [R=301,L]
If the /index.shtml fix above still results in a redirection loop, then rename the /index.shtml file to something else and add an internal rewrite from "/" to the new file. This should prevent the loop. The loop, if any, would be a result of the interaction of mod_dir, and mod_rewrite.
%{THE_REQUEST} is the original request header line received from the client. It might look like this, for example:
GET /widgets.shtml?color=blue&texture=fuzzy HTTP/1.1
Jim
Thanks for the help, this works great! Is there anything I can add or modify to do one more thing?
all of the rewrites work properly and return the proper header responses, the only thing that does not work is if you go to:
[example.com?ppc=query...]
it only rewrites to:
and I would like it to go to, or rewrite:
[example.com...]
it works as long as the query string url has the www in it though.
when I input: [example.com?query=string...]
it rewrites and 301's to: [example.com...]
PERFECT!
when I input: [example.com?query=string...]
(notice, no www)
it goes to: [example.com?query=string...]
So now, the third rule should kick in, and once again redirect the client to the www domain, but now without the query string.
If you're asking can you do it all at once, sure, but then you'll end up with rules for all possible combinations of domain names and query strings -- more rules for you to write and maintain, and more rules for the server to process on every request...
So, is the problem that your browser does not end up at the proper URL eventually, or is it that you want to do all redirects at once?
If it's not redirecting eventually to the right place, then make sure you didn't inadvertently change anything about the code above except for the domain name. Most notably, the pattern anchors are present when needed, and missing when not desired. I've reviewed it several times, and it should work as you desire, albeit using two separate redirects.
Jim