homepage Welcome to WebmasterWorld Guest from 54.163.72.86
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
New site structure. Need help redirecting query string urls to static
301 redirect dynamic query string urls to static urls
rumblepup




msg:4082865
 12:01 pm on Feb 18, 2010 (gmt 0)

I have been searching through the forums all night, and I just could not find a post that matched my situation. I checked out the Apache documentation on mod_rewrite, but I'm just too new at this to really understand. Hope you guys can help with yet another mod rewrite question.

I've changed the source code my e-commerce site runs on, and there is a new url structure. I have to 301 redirect old query string urls to static directory type urls. So I have two kinds of urls currently, category urls which look like this:
/SearchResult.aspx?CategoryID=2

and product details pages that look like this:
/detail.aspx?ID=30

In the case of the categories, I have to 301 redirect from
SearchResult.aspx?CategoryID=2
to
/parent-category/
or
/parent-category/child-category/

In the case of the product detail pages, I have to 301 redirect
/detail.aspx?ID=30
to
/product-name/

Could someone help me with a basic template to follow? Like I said, I'm just too new at this to understand the documentation.

 

g1smd




msg:4082872
 12:26 pm on Feb 18, 2010 (gmt 0)

This question comes up at least once every week, and there already was a recent post with all the steps, sometime in the last 2 or 3 days.

There's three steps:
- changing the links on the page to use the new URLs that users 'see' and 'use'.
- setting up a rewrite to connect the new URL requests with the real server-internal filepaths.
- redirecting direct client requests for old URLs to the new URLs.

List the redirects first, the last redirect being the general non-www to www canonical redirect.

List the rewrites after all of the redirects.

Every rule needs the [L] flag. Use RewriteRule for all of the rules. Do not mix Redirect or RedirectMatch rules in here.

Let's see your example code and we'll offer ways to improve and optimise it.

rumblepup




msg:4083297
 3:17 am on Feb 19, 2010 (gmt 0)

This question comes up at least once every week, and there already was a recent post with all the steps, sometime in the last 2 or 3 days.


Yes, there are in fact plenty of posts about mod rewrite, however, like I said, none that match my scenario. Each one of the posts I found, and I've gone back a few dozen pages already, as well as doing an exhaustive Google search on these forums as well as other places, are people who already OWN a php based website, and are trying to build friendly static urls using their current app. My current site runs on asp.net, as you can see from the type of url I posted, and I'm switching to php, Magento to be exact. So no, there is not a post amongst those in the past few days, at least as far as I've found, that is the same scenario as mine.


- changing the links on the page to use the new URLs that users 'see' and 'use'.


By nature of my changing the e-commerce engine I'm using, this step happens automatically. This is really kind of a "no-duh" scenario.

List the redirects first, the last redirect being the general non-www to www canonical redirect.


Ok, that's actually a good tip. Bonus. Thank you. But not what I'm asking.

List the rewrites after all of the redirects.


Magento, much like Wordpress, already does that.

Every rule needs the [L] flag. Use RewriteRule for all of the rules. Do not mix Redirect or RedirectMatch rules in here.


Shouldn't that be [R=301,L}? I know that I need to use both RewriteCond and RewriteRule, but I've been getting the syntax wrong again and again, and I was hoping that someone more intelligent than I can make quick work of this.

Let's see your example code and we'll offer ways to improve and optimise it.

My code is non-existent, because I don't know what I'm doing with REGEX syntax, but ok, I'll give it a shot. But I feel like I'm trying to write a grammatically correct Aramaic poem when all I really know is the word Aramaic.

So here we go.


RewriteCond %{QUERY_STRING} ^SearchResult.aspx?CategoryID=\2$
RewriteRule ^/parent-category/ [NC,R=301,L]

jdMorgan




msg:4083324
 5:00 am on Feb 19, 2010 (gmt 0)

Your best bet in this kind of scenario is to internally rewrite all "old" URLs to a script -- likely one that you'll have to write in PERL, for example, unless you can roll this functionality and the required database entries into your Magento stuff. The function of this script will be to open a database file, look up the old URL, retrieve the new URL, and output a 301-Moved Permanently redirect response including that new URL to the client.

The reason you'll need to do it this way is that mod_rewrite --on its own-- has no way to "associate" your new URLs with the old -- to take "/detail.aspx?ID=30" as input, for example, and to "know" that it needs to generate "/widget-fuzzy-green-small/" as output.

Another approach is to use a RewriteMap --if you have server configuration-level access-- and again call a script to do the database lookup and return the appropriate new URL. Then mod_rewrite can generate the 301 redirect. Most Webmasters prefer the "familiarity" of the first approach.

One thing to watch out for when using either of these methods is that the lookup script must be robust: It cannot be allowed to "die" under any circumstances. So this means that *all* possible errors must be handled, and if no 301 can be generated for any reason, you'll probably want to return a 404-Not Found response.

If the scripted approach isn't feasible, then you'll have to have one two-line rewriterule for every old URL on your site that you wish to preserve. In this case the code devolves to that posted many times here -- just a RewriteRule examining the URL-path and a RewriteCond examining the %{QUERY_STRING} variable.

There are a couple of ways to optimize this "brute force" approach so that fewer lines of code are needed, but I can't make a good recommendation without knowing the number of URLs you might want to redirect and seeing more examples with each field of each example fully-described (as in uppercase/lowercase/mixed-case alphabetic, numeric, hyphens, underscores,etc. -- the valid "character-set" of each field).

Jim

rumblepup




msg:4083348
 6:25 am on Feb 19, 2010 (gmt 0)

If the scripted approach isn't feasible, then you'll have to have one two-line rewriterule for every old URL on your site that you wish to preserve. In this case the code devolves to that posted many times here -- just a RewriteRule examining the URL-path and a RewriteCond examining the %{QUERY_STRING} variable.


That's my situation exactly. The script isn't feasible right now because I don't have the resources or knowledge myself to do that, however, I can do 301 redirects in .htaccess one by one. The new /this-product/ url I will be creating as I go. The new /category/ or /category/child-category/ urls have already been created. I just need a the correct syntax.

Like I said before, I looked up and down and in and around these forums, as well as across the web. Most search results are about rewriting the url. I just need to 301 redirect them. The closest post on the forums I could find was
[webmasterworld.com...]

I only have about 220 products and about 20 categories/child categories, so it's relatively easier for me just to do it this way, I just need the right template.

After reading

RewriteCond %{query_STRING} ^q=123$
RewriteRule ^index\.php$ /abcd/ [R=301,L]


The only thing I could come up with as an example of the redirects I need are

RewriteCond %{QUERY_STRING} ^?=2$ (# would change as I go)
RewriteRule ^SearchResult\.aspx$ /parent-category/ [R=301,L]

or

RewriteCond %{QUERY_STRING} ^?=3$ (# would change as I go)
RewriteRule ^SearchResult\.aspx$ /parent-category/sub-category/ [R=301,L]

and for my product detail pages

RewriteCond %{QUERY_STRING} ^?=30$ (# would change as I go)
RewriteRule ^detail\.aspx$ /product-name/ [R=301,L]

and to do this for each one of my categories and products, one by one, switching out the query string and the new url as I go.

Will this work?

g1smd




msg:4083376
 9:37 am on Feb 19, 2010 (gmt 0)

If the query string has any additional parameters, those rules would fail. I always allow for additional parameters by using 'soft' anchors.
RewriteCond %{QUERY_STRING} &?id=30&?
This changes things from query string must be 'exactly id=30' to query string 'contains id=30'.

With each redirect, do also include both protocol and domain name, not just the path part of the URL in the redirect target.

jdMorgan




msg:4083488
 2:53 pm on Feb 19, 2010 (gmt 0)

Note that the query string is delimited by a question mark. It does not *contain* a question mark. You should also include the name of the query parameter, and use a question mark on the end of the rewriterule substitution URL to clear the query string. Therefore, your first example in your most recent post should be:

RewriteCond %{QUERY_STRING} ^CategoryID=2$ (# would change as I go)
RewriteRule ^SearchResult\.aspx$ /parent-category/? [R=301,L]

or, as g1smd suggests in cases where this parameter might be preceded or followed by additional parameters (intentionally, accidentally, or maliciously):

RewriteCond %{QUERY_STRING} ^([^&]*&)*CategoryID=2(&.*)?$
RewriteRule ^SearchResult\.aspx$ /parent-category/? [R=301,L]

Jim

rumblepup




msg:4083834
 8:07 am on Feb 20, 2010 (gmt 0)

Sorry it actually takes me a day to respond. My mom is in the hospital, and I've been away from the computer since last night.


If the query string has any additional parameters, those rules would fail. I always allow for additional parameters by using 'soft' anchors.
RewriteCond %{QUERY_STRING} &?id=30&?
This changes things from query string must be 'exactly id=30' to query string 'contains id=30'.

With each redirect, do also include both protocol and domain name, not just the path part of the URL in the redirect target.


The query string does not have additional parameters, only queries are the base number (?=thenumber) in both of those urls samples, so doing an exact match looks preferable.

I'm developing locally, not localhost, but 127.0.0.1/site/ so I can't put the domain name in. If I only use the path, will this make the redirect fail? If I have to put the domain in, where and how do I place the full domain http:// www . thesite . com/ in the RewriteCond and RewriteRule?


Note that the query string is delimited by a question mark. It does not *contain* a question mark. You should also include the name of the query parameter, and use a question mark on the end of the rewriterule substitution URL to clear the query string. Therefore, your first example in your most recent post should be:

RewriteCond %{QUERY_STRING} ^CategoryID=2$ (# would change as I go)
RewriteRule ^SearchResult\.aspx$ /parent-category/? [R=301,L]


Understood. So by applying this as a template for my product detail pages, which are:
/detail.aspx?ID=30
I would do the following :

RewriteCond %{QUERY_STRING} ^ID=2$ (# would change as I go)
RewriteRule ^detail\.aspx$ /product-name/? [R=301,L]

Did I get this correct?

g1smd




msg:4083856
 9:35 am on Feb 20, 2010 (gmt 0)

The query string does not have additional parameters, only queries are the base number (?=thenumber) in both of those urls samples, so doing an exact match looks preferable.


What you have to cater for is if someone pastes a link to your content on their site, but accidentally adds other junk on the end of the URL in their link. Your site must provide an appropriate response for that. In this case it would be to redirect to the correct URL. The redirect is here to stop your site directly returning content for 'parameter' URLs. It should do that for all URLs containing parameters.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved