Forum Moderators: phranque
RewriteEngine on
RewriteCond %{HTTP_HOST}!^example\.com
RewriteRule ^.*$ http://example.com%{REQUEST_URI} [R=301,L]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.example\.com
RewriteRule (.*) http://example.com/$1 [R=301,L]
RewriteEngine On
RewriteCond %{HTTP_HOST}!^example.com$ [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
What do the differences mean and which is best?
Also, I'd rearranged my site a few months ago, so I also have a few of these in my htaccess file:
redirect 301 /page.shtml http://example.com/folder/page.php
Would the www -> non www redirect go after that, or would it make a difference? Will having both cause a problem anywhere?
The codes you have posted are all basically the same. The biggest difference is the one in the middle will work across all HTTP1.0 and 1.1 clients without causing a loop (as long as it is adjusted so both lines are either with www. or without: see below), because the first Condition checks to see if a HOST header has been sent. That could be added to the others to and they would work properly also.
The Options +FollowSymLinks line may or may not be needed, depending on the settings in the httpd.conf file. (IOW depends on the server settings, not which ruleset you decide to use.)
The first ruleset use the %{REQUEST_URI} 'variable' to determine the correct path to rewrite to, while the other two both use a stored backreference to determine the path. ( ()creates the backreference, $1 gets the information out.)
There is really almost no difference in the sets, so you can use the one you prefer. I personally use the one below. (If you use one of the other two, remember to add RewriteCond %{HTTP_HOST} . )
Normally, you will check/change the host at the beginning of the file, but you may not have a choice in the order, because the other rule you posted is a redirect (mod_alias), not a rewrite (mod_rewrite) and they will execute in the order the server loads the modules, so if mod_alias loads first, the page redirect will happen, then the domain rewrite -- the converse if mod_rewrite is loaded first. (I would think about changing them all to mod_rewrite, so I had control of them personally.)
They should not have any confilct with each other, and where they are in relation to each other in the file (top or bottom) should have no real impact.
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule (.*) http://example.com/$1 [R=301,L]
Hope this helps.
Justin
I don't know how to find out which my server would load first, so to be on the safe side I guess I'll redo the original 301's with mod_rewrite instead.
Which leaves me with another question, how do I write that? I looked around and can only seem to find code that (I'm guessing) is more complex than I need.
Since it was a total re-organisation (within the same domain), I can't just tell it to rewrite all .shtml to .php, everything was shifted to different folders and filenames. So that leaves me writing each one seperatly (not a big issue, its a relativley small site.) Is there a way to just write something that means
RewriteRule 301 /page.shtml http://example.com/folder/page.php I definetly want to avoid redirecting more than once, so if there is a way to use rewrite that, I'd add those then the www -> non www code at the end just to catch any missed by the first set of rules right? (Since if they hit an old page with the www's they'd be redirected to new page without)
And Options +FollowSymLinks line is only needed if it doesn't work without it right? So leave it out, test and see how it goes?
Sorry I sound so lost, .htaccess is one of those things that are more than a bit terrifying to a beginner with it, so I want to make sure I understand and get it right.
Thank you so much for helping me through this, the apache docs and any other site I found with this info was way above my head.
my .htaccess works for 401 and 404 pages.
I definetly want to avoid redirecting more than once, so if there is a way to use rewrite that, I'd add those then the www -> non www code at the end just to catch any missed by the first set of rules right? (Since if they hit an old page with the www's they'd be redirected to new page without)
I would not worry too much about redirecting twice, as long as you do not have links to the version of the site you will be redirecting from. If this were the initial setting of both redirects, I would be careful about it, but since some have been in place the new pages should be known to the search engines, and they should be requesting those pages from the start, so the only one they should 'see' is from the version of the site you are redirecting.
That said you can find more information about what you would like to do in the library and forum charter (links top left of the pages).
Your new rules will probably end up looking something like this:
RewriteRule ^oldpage\.shtml$ http://yoursite.com/wherethenew/pageis.php
Hope this helps.
Justin
the new pages should be known to the search engines, and they should be requesting those pages from the start
Should be doing and Are doing is the problem I'm afraid. I don't have links to the old pages, but many linkers still do and Google in particular seems to be confused about what the current url actually is... causing many duplicate and supplemental pages. Often with the new, correct url being the one going supplemental and later deleted.
They seem to be pulling up pages that haven't existed for 4 years (a few .html pages), along with the nearly year ago removed .shtml and the current .php.. as well as all those file extensions with and without the www's.
With all those problems my listings are a mess, search placement and current toolbar pr taking a bad hit. PR currently at 6 for non-www, 5 for www and showing as 0 in the Google directory :S Not a good sign.
Which is why I'm trying to make sure this is totally, 100% clear to Google what the page addresses actually are, and hopefully prompting them to delete the old urls.
From the mod_rewrite tutorial:
B. know a condition(s) will only be read after a rule matches the pattern of a request. If the rule does not match, no condition will ever be checked.
I'm taking that to mean what it sounds like... it will only redirect if the rule matches, so sending oldpage.shtml to [site...] won't trigger a www -> non www rule... so no double redirect if the www -> non www is written after, right?
ie. it follows the rules in the order they appear in the htaccess file as long as it's the same rule type (ie all rewrites)
GoogleGuy said somewhere that multiple redirects are a problem, so I'm hoping that a single step to the propper page is what he means. Also very much hoping that redirecting from a few old urls is within the rules, as long as it takes a single step to arrive at the correct url.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^oldfile\.shtml$ http://example.com/folder/file.php [R=301,L]
RewriteRule ^oldfile2\.shtml$ http://example.com/folder/file2.php [R=301,L]
#
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule (.*) http://example.com/$1 [R=301,L]
1) Is that propper format?
2) If someone (ie a bot) went to http://www.example.com/oldfile.shtml would they get redirected only once since the second rule would no longer be in effect after the 301 to http://example.com/folder/file.php?
3) Is there anywhere to validate .htaccess files or to check that only 1 redirect is in place? (I looked all over and can't find anything, other than a few other people looking for the same things.)
[edited by: jdMorgan at 7:39 pm (utc) on Sep. 22, 2005]
[edit reason] Code display formatting fixed. [/edit]
You can use the Server Headers Checker [webmasterworld.com] in the WebmasterWorld Control panel to observe your server's response to requests.
Other than that, there is no way to test mod_rewrite code, except on a server. Most folks either set up a test server or test on a 'live' server late at night and/or on weekends when site traffic is lowest.
Jim