Google Answers Logo
View Question
Q: Mod_Rewrite - Redirect Dyamic URLs to Rewritten Static Versions ( Answered,   0 Comments )
Subject: Mod_Rewrite - Redirect Dyamic URLs to Rewritten Static Versions
Category: Computers > Programming
Asked by: seonick-ga
List Price: $50.00
Posted: 02 Nov 2005 06:50 PST
Expires: 02 Dec 2005 06:50 PST
Question ID: 587953
I've successfully re-written our dynamic product urls using the following code:

RewriteRule (.*)-(.*)\.htm$ /proddetail.php?prod=$1&cat=$2

Now my question is, how do I re-direct the currently indexed urls to
their new static versions?
For example:
==> Need to redirect to:

Subject: Re: Mod_Rewrite - Redirect Dyamic URLs to Rewritten Static Versions
Answered By: palitoy-ga on 02 Nov 2005 08:08 PST
Hello seonick-ga

Thank-you for your question.

To solve your problem you need to employ further regular expressions. 

From what you have written I believe you need the following rewrite rule:

RewriteRule ^proddetail\.php\?prod\=([0-9]+)\&cat\=([0-9]+)$ $1-$2.htm

You have to take care that the RewriteRule's do not go into a loop.  I
am not quite clear from your question as to why you would want to
rewrite your dynamic pages back to a static page.

These questions are notoriously difficult to solve without access to
the webserver they are running on so the answer may need a little
tweaking here and there.  If this is the case please do not hesitate
to ask for clarification and I will do my best to respond swiftly.

Request for Answer Clarification by seonick-ga on 02 Nov 2005 09:01 PST

Thanks for the answer. Let me clarify a few things and maybe that will
help you out.

First: Our urls are completely indexed with the dynamic variables (ie.
proddetail.php?prod=27&cat=12) - I've employed the Mod_rewrite so now
that same URL is served as but the problem is
that the Dynamic versions still work and since they remain in the
indices, it's causing duplicate URLs to become indexed. So the goal is
to Redirect the old dynamic URLs to the new re-written urls (using
301) so that it speeds up the indexing of the static URLs and the
duplicate URLs are removed ASAP.

Second: Here's what our mod_rewrite file looks like now:
<IfModule mod_php4.c> 
php_value session.use_trans_sid 0 
AddType application/x-httpd-php .htm .xml .rss
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteRule (.*)-(.*)\.htm$ /proddetail.php?prod=$1&cat=$2

Clarification of Answer by palitoy-ga on 02 Nov 2005 09:17 PST
I am still not 100% clear as to your situation.

1) You have dynamic pages (proddetail.php) which have content for a product.
2) You have static pages which also correspond to the same products.
3) You need the static pages to point to 1) and the dynamic pages to point to 2)

Is this correct?  I suspect it isn't...

Also you state that it is causing duplicate pages to become indexed. 
Where are these being indexed?

Are you removing all your dynamic content and moving to static pages?  

Are you aware that if someone knows your pages are generated by
proddetail.php?prod=xyz&cat=abc that they will always be able to type
this in instead of xyz-abc.htm and get the same result?

At the moment your mod_rewrite appears to be simply directing the
static xyz-abc.htm pages to proddetail.php?prod=xyz&cat=abc

Request for Answer Clarification by seonick-ga on 02 Nov 2005 10:55 PST
With the mod_rewrite, we now have both:
Indexed in Google, Yahoo! and MSN.

The site now only delivers the static versions of the URLs, but the
dynamic URLs still work and render with the dynamic variables. The
dynamic URLs are still indexed within the engines and we have incoming
links that continue to point to them.

We need the dynamic URLs to redirect to their static versions, so:
will automatically forward to--> 

This way if anyone does type in the dynamic version or follows an old
incoming link, they will be properly re-directed to the static

Clarification of Answer by palitoy-ga on 02 Nov 2005 11:28 PST
OK, now I understand!

What you need to do is to perform a permanent redirect when someone
types in the proddetail.php URL.  This can be achieved with something
like this:

RewriteRule ^proddetail\.php\?prod\=([0-9]+)\&cat\=([0-9]+)$ $1-$2.htm [R=301,L]

You should add this line directly after the "RewriteBase /" line in
your current mod_rewrite.  This basically searches in the input URL
for the product id and the category id and then redirects the user
permanently to the html page.

Let me know how you get on with this.

Clarification of Answer by palitoy-ga on 02 Nov 2005 12:06 PST
This might be a more elegant solution (again just add it after the
"RewriteBase /" line):

RewriteCond %{QUERY_STRING} ^prod\=([^&]+)\&cat\=([^&]+)$
RewriteRule ^$ %1-%2.htm [R=301,L]

Request for Answer Clarification by seonick-ga on 02 Nov 2005 13:21 PST
Neither of those are working, either an internal server error is
thrown or nothing occurs.

I've also tried: RedirectMatch permanent

which did not work

Clarification of Answer by palitoy-ga on 03 Nov 2005 01:39 PST
I would concentrate on the more elegant solution I provided.  Can you
please change it back to this and then try again?  If you get an error
message can you please look in your server error log and let me know
what message you are getting?  This will help in trying to track down
the problem.

In the meantime I have been searching for other possible solutions and
came across this webpage which is interesting as it provides a
solution to a problem very similar to yours:

Clarification of Answer by palitoy-ga on 03 Nov 2005 03:08 PST
This may be an alternate solution (it appears to work on my test set-up):

RewriteCond %{QUERY_STRING} ^prod\=([^&]+)\&cat\=([^&]+)$
RewriteRule ^(.*)proddetail\.php(.*)$ %1-%2.htm [R=301,L]

Request for Answer Clarification by seonick-ga on 03 Nov 2005 03:22 PST
Alright - Now I have: 
# Rewrite static URLs to dynamic
RewriteRule ([^-]+)-([^.]+)\.htm$ /proddetail.php?prod=$1&cat=$2 [L]
# Redirect only client-requested dynamic URLs to static
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /proddetail\.php\?prod=([^&]+)&cat=([^&]+)
RewriteRule ^proddetail\.php$ [R=301,L]

Which lets the static urls render properly, but the dynamic urls are
still throwing an error. This time the error comes out in the url
(which goes to a 404 page):

Clarification of Answer by palitoy-ga on 03 Nov 2005 03:45 PST
I think you need to escape the period (.) character in your last
RewriteRule so it should look something like this (notice the extra

RewriteRule ^proddetail\.php$\.htm [R=301,L]

Clarification of Answer by palitoy-ga on 03 Nov 2005 03:52 PST
I am also looking at your RewriteCond which seems to be a little wrong...

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /proddetail\.php\?prod=([^&]+)&cat=([^&]+)

At the moment this gets THE_REQUEST and searches for 3-9 capital
letters in a row followed by a space, then /proddetail.php?prod=,
followed by a match of everything up to a & character, then &cat=, and
followed by everything up to another & character.

At first glance this would appear wrong because of the space character
but I do not know fully your set-up.  You should also add a $
character to the end of the query so it would become (all on one

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /proddetail\.php\?prod=([^&]+)&cat=([^&]+)$
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy