Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite vs. Redirect 301

Choosing between mod_rewrite and Redirect 301 page name changes.

         

cooldogs

3:06 pm on Sep 15, 2008 (gmt 0)

10+ Year Member



I am currently renaming about 15 pages as part of a site revision, the names do need to be changed but I do not want to lose search engine rankings.

I am new to the technical part of all this and have succesfully implemented a Redirect 301 on my .htaccess file.

However, I have had several people that think I should use mod_rewrite instead. This would be more complicated for me and would involve a new learning curve to overcome.

I would love to know the pros and cons of each before making a decision as to which to use for the final transition.

Thanks

jdMorgan

3:24 pm on Sep 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Redirect 301 (mod_alias) is simple

RedirectMatch 301 (mod_alias) allows pattern-matching on the requested URL, and "copying" the matched pieces of it into the new URL.

RewriteRule (mod_rewrite) allows pattern-matching on the requested URL and "copying" pieces of it into the new URL, just like RedirectMatch does. However, RewriteRule can also be used with one or more RewriteConds to make redirects conditional upon many other factors -- for example, the query string value, the requested hostname (domain or subdomain), the IP address that the request arrived from, http versus https, etc., and to "copy" pieces of any or all of those different variables into the new URL.

mod_rewrite can also be used to invoke an internal rewrite or a proxy through-put, in addition to an external redirect. It's a much more powerful and flexible tool.

However, for what you're doing, with only 15 URLs to redirect, the simple approach using a mod_alias Redirect 301 directive sounds best to me.

Jim

cooldogs

4:56 pm on Sep 15, 2008 (gmt 0)

10+ Year Member



One last thing that might affect my decision.

My customer's intial poorly named pages include a mix of capital and lower case in the names. The 301 Redirects I've been able to implement succesfully work when the case is correct but return a 404 when they do not. This fine with me as long as it doesn't bother the search engines, do you know if the search engines index (or see files) by case specific name? Thank you.

jdMorgan

12:25 am on Sep 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Any difference whatsoever between two URLs makes them two separate and distinct URLs. Since search engines request, parse, rank, index, and display URLs, any inconsistency whatsoever is a problem.

Mod_rewrite can be used with the [NC] flag, which makes it case-insensitive when inspecting a URL to see if it should be redirected. But if the *only* problem with the URL is the casing of its letters, then you must also check to see that the URL is not already properly-cased, in order to avoid a redirection loop. An example --and it sounds like you may need one-- would be:


# If not already properly-cased
RewriteCond %{REQUEST_URI} !^/Images/Logo\.gif$
# Redirect any URL with any mix of upper or lowercase to the properly-cased URL
RewriteRule ^images/logo\.gif$ http://www.example.com/Images/Logo.gif [[b]NC[/b],R=301,L]

This is just the sort of thing that demonstrates why mod_rewrite's ability to do *conditional* redirects is so useful.

Note that I strongly suggest using all-lowercase URLs to avoid problems in the future; The above code is an example only, and I varied the casing to demonstrate the technical point. It is very difficult to do case-correction on large numbers of URLs, and it is also horribly inefficient to do case conversion using the directives available in .htaccess only -- The usual situation for the vast majority of Webmasters.

Jim

g1smd

8:52 am on Sep 16, 2008 (gmt 0)

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



Having found Mod_Rewrite first, I always use that for everything.

I don't think I have ever used Mod_Alias for redirects.

An important point that I picked up very early on (thanks jd!) is that you must never mix rules from the two directives in the same .htaccess file.

If you do mix them, you will not be able to guarantee the order they are processed in.

Since Mod_Rewrite seems to the be Swiss Army Knife of choice as far as server tools on Apache goes, I just pack that one and go.

cooldogs

1:36 pm on Sep 16, 2008 (gmt 0)

10+ Year Member



Thank you both for your helpful insight, and yes I am quite the newbie to all this Redirection stuff. What the situation is, is that when the customer named his pages he did so mixing caps and lower case and some names didn't even related to the subject, which is why I've tried to change them over to pertinent lower case names, names that are entirely different from the old (not just different by capitalization).

Just for fun I went to Google and typed in the original name of the indexed page with the Capital letters it now has ... My_Example.html and also tried my_example.html in the search - just to see what would happen if someone typed in the wrong case - both versions brought up the page by it's correct name My_Example.html BUT when I did that in Yahoo, Yahoo only returned the page in the results if I typed it into the search with the capital letters, which is how the page is now indexed.