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

noob help rewriting of products?.php url to homepage

 9:19 am on Mar 5, 2013 (gmt 0)

hi webmasters of the world.

i hope someone can help. I have just moved to a new installation of open cart, but have a lot of old indexed pages in google that are still getting visitors. The problem is that the old site had no seo on it at all, so the product pages are all like products?.php


i simply want to redirect any url like this to the home page?

i know its probably simple, but is there a way to redirect any thing that has products and .php in the referring url. The numbers could be different ?

many thanks



 10:02 am on Mar 5, 2013 (gmt 0)

It is very bad form to mass redirect to the home page. It's a signal of "low technical quality" as far as Google is concerned. You should redirect to the equivalent page.


 11:18 am on Mar 5, 2013 (gmt 0)

Hi, okay i can do that, always listening and learning !

do i need to specify them in all big list then as there is nothing in the original url apart from the number that can identify the new product url? There is no relationship between the old number and the new url.

It seems like a slow method rule 1=1, would rather have a rule where 1 redirect pattern will cover 500+ ? any other ideas?


 11:34 am on Mar 5, 2013 (gmt 0)

You could rewrite (rewrite, rather than redirect) those requests to a simple PHP script that contains an array of old and new URLs where the PHP script will send the redirect (or will send 404 if the old URL is not found in the array).

This is one line of htaccess code (plus an extra exclusion before your non-www/www canonicalisation rule) and a few lines of PHP code (plus the array of however many URLs we're talking about).


 1:16 pm on Mar 5, 2013 (gmt 0)

yes rewrite i think is the way... thanks..


 4:15 pm on Mar 5, 2013 (gmt 0)

This goes near the start of the root htaccess file:
RewriteRule ^product([0-9]+)\.php /fix-me.php?id=$1 [L]

You'll also need a RewriteCond as part of your existing non-www/www canonicalisation rule:
RewriteCond %{REQUEST_URI} !^/fix-me\.php$
otherwise non-www requests for old URLs will expose the fix-me script as a new URL on www.

The PHP script contains an array:
1 => /new-page-for-one
9 => /new-page-for-nine
and simply looks for the new URL given the old ID. It then uses the HEADER directive to send a 301 response.

When there is no new URL for the old ID found in the array, the PHP script MUST send a 404 Not Found status code.


 10:44 pm on Mar 5, 2013 (gmt 0)

This goes near the start of the root htaccess file:

Generic question now that it has come up.

When you've got a redirect that's too complicated for htaccess alone, so a detour to a php script is required, there are really two things happening. First the htacces rewrite to the php stuff; then the php redirect to the target page.

In this situation, would the RewriteRule be located as if it were a redirect-- that is, after the hard-coded [F] and [G], before most of the [R=301,L] --even though on the surface it's "only" a rewrite [L]?


 11:26 pm on Mar 5, 2013 (gmt 0)

The "rewrite to special PHP script" can go in one of two places.

1. After rules that block and indeed after rules that redirect, down there where "normal" rewrites usually go. This is usually NOT a good idea because non-www requests for the old URL will be redirected to www before the rewrite kicks in to the PHP script which then issues its own redirect. Non-www requests for the old URL will create a two-step redirection chain:
non-www OLD URL -> www OLD URL -> www new URL
It's also possible that some other earlier more general rule will match the request and produce the wrong result.

2. After rules that block and before rules that redirect. Ultimately, URL requests that match the RegEx pattern will be redirected (by the PHP script). I prefer this, as it means all requests that will be redirected are listed before the non-www/www canonicalisation rule (it usually also nicely fits the required ordering from most specific to most general too). However, you MUST add the fixup PHP script path as an exclusion to the non-www/www rule, otherwise non-www requests for old URLs will be redirected to a www URL with the PHP script location as the path:
non-www OLD URL -> www PHP script -> www new URL
Again, this multiple step redirection chain is a bad thing, and the extra RewriteCond attached to the non-www/www canonicalisation rule prevents it happening.

I remember a site that had hundreds of thousands of old URLs that needed to be redirected via just such a PHP script. To invoke the PHP script(s), just five RewriteRules were needed. These went at the top of the htaccess file right after rules that block access to malicious requests. All other requests breezed through these five rules without matching and on to the more normal sort of htaccess code below.

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