Forum Moderators: phranque

Message Too Old, No Replies

Problem with duplicate urls

         

inssomnia

1:56 pm on May 25, 2010 (gmt 0)

10+ Year Member



Hello,

Can you help me solve this problem,please..

In my .htaccess file I used this rewrite rule:

RewriteRule ^country/([0-9]+)/([0-9]+)/([0-9]+)/(.*)/(.*)?$ search.php?loc1=$1&loc2=$2&loc3=$3 [L]

to get this kind of urls:

mydomain.com/country/13/196/2693/germany/frankfurt

everithing works fine,but I get unwanted duplicate urls also:

mydomain.com/country/13/196/2693/germany/frankfurt?loc1=13&loc2=196&loc3=2693

Both links are working.

Where I made mistake, and what is causing duplicate urls?


P.S. In my .htaccess file I have more rules and redirects, but I hope that is not related with this problem.


Thanks in advance!

Best regards

jdMorgan

3:26 pm on May 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You didn't make a mistake per-se. But you might want to add another rule to redirect the old dynamic "URLs" --now used only as filepaths inside the server-- to the new "static" URLs.

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /search\.php\?loc1=([0-9]+)&loc2=([0-9]+)&loc3=([0-9]+)\ HTTP/
RewriteRule ^search\.php$ http://example.com/country/%1/%2/%3/ [R=301,L]

However, you can see that there is a problem: The "Germany/Frankfurt/ part of the new static URL cannot be derived from the old dynamic URL. So the the process is not completely reversible.

In order to do this properly, you will need to rewrite incoming requests for old dynamic URLs to your script (or a script) that can look up the old dynamic URL in your database, and generate a 301 redirect to the new static URL. This will allow the process to be completely reversible. However, you must be sure that the dynamic URL is being directly requested by a client, and not as a result of your new static-to-dynamic rewriterule. This can be done by passing a variable based on THE_REQUEST to your script, telling it that a redirect is required. Something like:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(search\.php\?loc1=[0-9]+&loc2=[0-9]+&loc3=[0-9]+)\ HTTP/
RewriteRule ^search\.php$ redirect-script.php?need-redirect=yes&requested-URL=%1 [L]

So your script looks at the "need-redirect" parameter and if its value is "yes" looks up the URL-path passed as the value of "requested-URL" in your database to get the new URL, and generates a 301 status header and a Location header to tell the client to re-request the resource using this new URL.

This requires that your database contain a field (or fields) for the old dynamic URL(s) for each new static URL.

Jim

inssomnia

4:42 pm on May 25, 2010 (gmt 0)

10+ Year Member



Jim, thank you very much for quick response.

I'll try that way you advised.

Best regards!