homepage Welcome to WebmasterWorld Guest from 50.19.169.37
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

    
Redirect with multiple parameters
LinusIT




msg:4620303
 1:01 pm on Oct 31, 2013 (gmt 0)

I've searched for a solution but to no avail. I don't think what I'm trying to do is unique or difficult to solve.

I've changed the ecommerce software a client of mine uses and on their old software there was a page called "product_info.php" which could have extra parameters such as currency and product_id. The new software doesn't work like this so I can't do straight redirects.

Here's an example of the URLs:

product_info.php?currency=USD&products_id=55
product_info.php?currency=GBP&products_id=19

I would like to either redirect these to the new relevant products or simply to the root. I have tried the following, but I still get 404 not found:

RewriteCond %{QUERY_STRING} ^currency=USD$ [NC]
RewriteRule ^product_info\.php$ index.php [R=301,L]


Any help would be greatly received.

 

lucy24




msg:4620410
 8:12 pm on Oct 31, 2013 (gmt 0)

RewriteCond %{QUERY_STRING} ^currency=USD$

The anchors mean that "currency=USD" is the entire query. What you need instead is

(^|&)currency=USD($|&)

to the new relevant products or simply to the root

No, you don't ;) "New relevant product" yes, definitely. But search engines get grumpy if you redirect everything to the root, and they start asking for imaginary files in search of "soft 404s".

If there are discontinued products, that's a case for a 410 with a nicely customized page.

LinusIT




msg:4620419
 8:52 pm on Oct 31, 2013 (gmt 0)

Thank you for your help. I've changed the code to the below but still when I accesss getting 404 page not found when I access domain.com/product_info.php?currency=USD

RewriteCond %{QUERY_STRING}(^|&)currency=USD($|&) [NC]
RewriteRule ^product_info.php$ index.php [R=301,L]


Instead of getting the above working I think I need to extend the parameter so I can redirect to the correct product as per your comments.

The page is always product_info.php, the currency parameter is always USD, GBP or EUR and the product_id parameter is always numerical.

Hope you can help

[edited by: phranque at 12:18 am (utc) on Nov 1, 2013]
[edit reason] disabled graphic smileys [/edit]

phranque




msg:4620474
 12:19 am on Nov 1, 2013 (gmt 0)

you're missing a space between "%{QUERY_STRING}" and "(^|&)currency=USD($|&)"

lucy24




msg:4620476
 12:36 am on Nov 1, 2013 (gmt 0)

%{QUERY_STRING}(^|&)currency

I hope that was a typo in the post. Without a space in the middle, nothing will work.

The rule as written-- assuming it works as intended-- will keep the old parameters while redirecting to a different page. But will index.php still use those parameters? If not, get rid of them.

:: wait, stop, rewind ::

Now, wait a minute. You don't want
index.php [301,L]
as the target anyway. You want
http://www.example.com/ [R=301,L]

On the one hand you never say "index.xtn" outright. Just the directory name-- in this case the root. On the other hand a redirect should always include the full protocol-plus-hostname.

product_info.php?currency=USD&products_id=55
product_info.php?currency=GBP&products_id=19

=
currency=(USD|GBP|EUR)&products_id=\d+

but better
(^|&)currency=(USD|GPD|EUR)($|&)
and
(^|&)products_id=\d+($|&)
as two separate lines. This allows search engines to amuse themselves by twisting the queries around or appending bum requests.

What about other values for "currency"? Are there other currencies that don't get redirected? I got the impression that those are the only possible values. So somewhere-- either in your php or in another RewriteRule-- you need to be sure to return a 404 if the parameter has some other value. Is there a default if it's blank?

Same for product_id. Are some values not to be redirected?


How many different URLs are involved? Just looking at three currencies and 99 products (going by the two-digit examples) that's definitely too many to clutter up an htaccess file. How about rewriting to a php script that looks up the new URLs and then issues a redirect?

<conditions here>
RewriteRule ^product_info\.php$ /fixup.php [L]


The same page would then issue a 404 if it meets a parameter it can't handle.

LinusIT




msg:4620900
 9:07 pm on Nov 3, 2013 (gmt 0)

Thanks lucy24 for the in depth reply, you've made some good points and an interesting read.

I will be redirecting all values that come across, there are 60 products in total. I agree that redirecting to a php script would be a good idea, although wouldn't it be better to create a product_info.php page and do all the work inside that?

On the new system the currency part of it isn't set within the url so I can ignore that part of it. I think I'd just need to write a line by line redirect to the new URLs.

lucy24




msg:4620910
 10:57 pm on Nov 3, 2013 (gmt 0)

I agree that redirecting to a php script would be a good idea, although wouldn't it be better to create a product_info.php page and do all the work inside that?

Yes, absolutely. If things are already going via a php page, then just make sure the page is set up to evaluate parameters and return the appropriate response, including displaying the 404 page if appropriate. Beware of unintended blank lines in the wrong part of the php ;) They have been many people's downfall. ("Sorry, folks, I've already sent out a carriage return so don't talk to me about headers!")

LinusIT




msg:4621016
 12:00 pm on Nov 4, 2013 (gmt 0)

I've created a php script that reads the products_id variable and then redirects to the new product url, tested and working great.

I did some reading about large .htaccess files and they are a big no no.

Thanks for your help.

phranque




msg:4621164
 9:56 pm on Nov 4, 2013 (gmt 0)

make sure it's returning a 301 status code.
it wouldn't hurt to test it in GWT using "fetch as googlebot".

LinusIT




msg:4621230
 9:32 am on Nov 5, 2013 (gmt 0)

Here's a snippet of what I'm using:

$product_id = $_GET['products_id'];

if ($product_id == 1) {
header("HTTP/1.1 301 Moved Permanently");
header('Location: http://www.domain.com/product1.html');
} else if ($product_id == 2) {
header("HTTP/1.1 301 Moved Permanently");
header('Location: http://www.product2.html');
}

[edited by: phranque at 11:20 am (utc) on Nov 5, 2013]
[edit reason] unlinked urls [/edit]

phranque




msg:4621255
 11:27 am on Nov 5, 2013 (gmt 0)

Here's a snippet of what I'm using

regardless of what you are using in your script you should test the response since other things can affect the response.
i would assume one of your more important test cases is the response googlebot gets.

it looks like you have a typo there.
perhaps you meant this?
$product_id = $_GET['products_id'];

if ($product_id == 1) {
header("HTTP/1.1 301 Moved Permanently");
header('Location: http://www.example.com/product1.html');
} else if ($product_id == 2) {
header("HTTP/1.1 301 Moved Permanently");
header('Location: http://www.example.com/product2.html');
}



also, please note - IMPORTANT: Please Use example.com For Domain Names in Posts:
http://www.webmasterworld.com/apache/4452736.htm [webmasterworld.com]

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