Forum Moderators: phranque

Message Too Old, No Replies

redirect to different url

category and item are different on new url

         

Ivanna

8:48 pm on May 14, 2012 (gmt 0)

10+ Year Member



Hello,
I am new to mod-rewrite and do not understand all of it.
My client had lost access to a site backend and so wanted to move to a new site. I build the new site using a different shopping cart (ZenCart) and so much or the URL and categories are different. I have found a lot of the old URL because of 404 errors. I then match the product from the old URL to the new URL and try to write a single line to test if it works. It does not work.

This is what I did:
RewriteRule ^item_55/Modern-Silver-Jewelry-Ring.htm$ /Rings/Silver-Rings/Modern-Sterling-Silver-Ring
ErrorDocument 404 /index.php

As you will see, the category is different and also the item name is different.
When I go to mydomain/item_55/Modern-Silver-Jewelry-Ring.htm it does not redirect to mydomain/Rings/Silver-Rings/Modern-Sterling-Silver-Ring instead it goes to mydomain/index.php

Q1. What is the correct syntax for this to work please?
Q2. I will have a lot of lines because each URL is different. Must I put RewriteRule at the start of each line?

I have already change from dynamic URL to Static URL and it is in the sitemap.

Thank you.

g1smd

9:36 pm on May 14, 2012 (gmt 0)

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



Your code is for an internal rewrite.

RewriteRule ^item_55/Modern-Silver-Jewelry-Ring.htm$ /Rings/Silver-Rings/Modern-Sterling-Silver-Ring [L]

... when user requests
http://www.example.com/item_55/Modern-Silver-Jewelry-Ring.htm
look in this place on the hard drive
/[SERVER_ROOT]/Rings/Silver-Rings/
for a physical file called
Modern-Sterling-Silver-Ring 
without revealing to the user that the internal file pointer has been amended to point to a different internal location.


You need a redirect:

RewriteRule ^item_55/Modern-Silver-Jewelry-Ring.htm$ http://www.example.com/Rings/Silver-Rings/Modern-Sterling-Silver-Ring [R=301,L]

... when user requests
http://www.example.com/item_55/Modern-Silver-Jewelry-Ring.htm
tell them to go request a different URL:
http://www.example.com/Rings/Silver-Rings/Modern-Sterling-Silver-Ring
instead.


Don't use your home page as the ErrorDocument. That will always end in tears.

Ivanna

10:43 pm on May 14, 2012 (gmt 0)

10+ Year Member



Thank you for the reply.
I understand the difference is http://www.example.com in the second part.
Also what does [R=301,L] do?
Will this become permanent? I want the old index to become indexed for the new URL.

Why is the home page not good for the ErrorDocument. I found the code on the web and it worked so I thought it is good.

Thank you

g1smd

10:56 pm on May 14, 2012 (gmt 0)

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



Adding the domain name to the rule target makes the RewriteRule into a redirect. The default is 302. The R=301 makes it a 301 redirect.

Every RewriteRule should have the L flag too. This stops mod_rewrite processing at "this" rule if the pattern matched and the rule was invoked.


Using your home page content as the ErrorDocument is confusing to both humans and searchengines alike.

[edited by: g1smd at 11:00 pm (utc) on May 14, 2012]

Ivanna

10:59 pm on May 14, 2012 (gmt 0)

10+ Year Member



It is not working for me.
The site is a shopping cart and so has dynamic URL. I have code to change it to static URL which is at the start of the file
then my redirect code
then errordocument.

I think perhaps the first line of code will not allow redirect for URL that end with .htm. Is this correct?

RewriteEngine On

# Don't rewrite any URIs ending with a file extension (ending with .[#*$!xx])
RewriteCond %{REQUEST_URI} !\.[a-z]{2,5}$ [NC]
# Don't rewrite any URIs for some, popular specific file format extensions,
# which are not covered by main file extension condition above
RewriteCond %{REQUEST_URI} !\.(mp3|mp4|h264)$ [NC]
# Don't rewrite any URIs for some specific file format extensions,
# which are not covered by main file extension condition above
# Uncomment the following line to apply this condition! (Remove the # at the start of the next line)
#RewriteCond %{REQUEST_URI} !\.(3gp|3g2|h261|h263|mj2|mjp2|mp4v|mpg4|m1v|m2v|m4u|f4v|m4v|3dml)$ [NC]
# Don't rewrite admin directory
RewriteCond %{REQUEST_URI} !^/manage [NC]
# Don't rewrite editors directory
RewriteCond %{REQUEST_URI} !^/editors/ [NC]
# Don't rewrite cgi-bin directory
RewriteCond %{REQUEST_URI} !^/cgi-bin/ [NC]
# Don't rewrite .smileys directory
RewriteCond %{REQUEST_URI} !^/.smileys/ [NC]
# Handle all other URIs using Zen Cart (its index.php)
RewriteRule .* index.php [QSA,L]

g1smd

11:02 pm on May 14, 2012 (gmt 0)

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



Those rules are for internal rewrites.

You must list internal rewrites after external redirects otherwise meyhem ensues.

lucy24

11:02 pm on May 14, 2012 (gmt 0)

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



Edit: Ouch! Youse guys type too fast. I am lagging several posts behind.


The R=301 flag changes your Redirect from the default 302 to a proper 301. (R alone, or protocol-plus-domain alone, only gives you a 302.)

The L flag means "If this rule has executed, don't look for any further rules." Normally you will append [L] to all rewrites unless (1) the rule ends in [F] or [G] which automatically implies [L] ... or (2) if you really, really know what you are doing. And by that time you will be ready to answer questions instead of asking them :)

After you have taken care of the Redirects, you will need a further Rewrite (not redirect) to a page that ends in .html or .php or whatever it may be. The user does not need to see the extension, but the computer does. There are several thousand posts in this Forum about the Redirect-plus-Rewrite package.

Why is the home page not good for the ErrorDocument.

Well, for one thing, because it annoys many users. Instead, make a nice customized 404 page that matches the "look" of your site, and that has some useful links and/or a site search. You will probably have some 410s. You can send those to the same 404 page.

Ivanna

12:01 am on May 15, 2012 (gmt 0)

10+ Year Member



Thank you both. Please excuse me if I do not always understand.

So, internal rewrite is when I change something already on this site and external is when I change something that is coming to the site ?
So I write my redirect code
then I write my errordocument to somewhere else
then I write the remap ?

I do not think I should add [L] because there are lots of code
What is differents of rewrite and redirect? I thought they are the same.

Maybe oneday I will know (2) and can help others :-)

thank you

lucy24

3:59 am on May 15, 2012 (gmt 0)

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



[L] does not mean "stop looking". It means "If this rule has been applied, then stop". Normally, a request that moves through mod_rewrite will only have one thing done to it. This is why rules are put in order:

--first any RewriteRules that result in [F] (the same as Deny From). Bye-bye, go away, we are completely finished. There is no point in redirecting or rewriting or doing anything else to a request if you are just going to kick them out the door later.

--then any RewriteRules that return a [G] (410 error). The page you want is gone, so you can stop looking. A 410 never happens by itself. You use it when you have deleted a page and there isn't anything appropriate to redirect people to.

--then the RewriteRules that end in [R=301]. These will actually pass through mod_rewrite again later, when the new redirected request comes through. Human users don't have to do anything about these rules. Their browser does the work, and ends up showing a different address.

--finally the internal rewrites. "Internal" means that only the server (and you!) know it's happening. The user's address bar will say one thing, while the content is coming from some other place. Sometimes you will find a Rewrite called an Internal Redirect. Apache just says this to confuse you.

Within each group of rules, move from most specific to most general. A rule that only applies to a few named pages, or some particular type of request, comes first. Then the ones that apply to bigger groups. And finally the rules that apply to anyone left over: For example, redirecting users from example.com to www.example.com. (Or the other way around.) Anyone who has already been redirected does not need this rule; they have already been sent to the correct form of your domain name.

mod_rewrite can make both rewrites and redirects, depending on how you write the rule.

There is one special kind of Rewrite that you have met in person. Everyone has. It's the regular error page (403 or 404)-- the one you get if you go to any web site and misspell a page name. Look at the browser's address bar next time you land on an error page. You will see that it gives the address of the page you thought you were going to. If you try it on your own site, you can also look in the logs. They will only show the name of the page you asked for, not the error page.

You do not use mod_rewrite to make error pages. But it is a good way to understand what a Rewrite is. Another very common rewrite that you may have met is the "No Hotlinks!" message. This one is made with mod_rewrite. I think it was the first thing I ever did with mod_rewrite :)