Forum Moderators: phranque

Message Too Old, No Replies

Rewriting dynamic urls to static

         

fzx5v0

9:07 pm on Oct 25, 2005 (gmt 0)

10+ Year Member



Hi

I have been trying to rewrite some urls to static with no sucess.

www.mysite.co.uk/?cPath=21_ 386_392&sort=3a&products_id=124

[mysite.co.uk...]

but if it has shopping_cart.php in the string not to rewrite

thankz for any pointers

jdMorgan

12:48 am on Oct 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



fzx5v0,

Welcome to WebmasterWorld!

If you are trying to create 'search engine friendly' static URLs, then it's likely you're trying to do this backwards.

First, change your script to output friendly static URLs on your pages, so visitors and search engines can see them. Then use mod_rewrite to detect those friendly URLs when they are requested from your server, and convert them back into the form needed to call your script.

Mod_rewrite acts after a request arrives at your server, and before any content is served or any scripts are invoked. Therefore, it can change incoming static URLs to the dynamic form needed to call your script, but it cannot change the URLs that appear on your site's pages.

For more information, see the documents cited in our forum charter [webmasterworld.com] and especailly see the mod_rewrite tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

fzx5v0

9:28 am on Oct 26, 2005 (gmt 0)

10+ Year Member



Jd
we already have friendly urls in the form of

[mysite.co.uk...]

Before we changed to this google had indexed our no friendly urls www.mysite.co.uk/?cPath=21_ 386_392&sort=3a&products_id=124

and now we are getting penilised for duplicate content

both url's work and both bring up the same content. Is there no way I can take the dynamic to 301 to the static If not how will I get it out of Google index

jdMorgan

12:59 pm on Oct 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, well this is a bit tricky. You'll probably have to use the server variable %{THE_REQUEST} so that this rule does not combine with your static->dynamic rewrite and ceate an endless loop:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?cPath=([0-9_]+)[^&]*&products_id=([0-9]+)
RewriteRule ^/?$ http://www.example.co.uk/index.php/cPath/%1/products_id/%2? [R=301,L]

The value of server variable %{THE_REQUEST} is always the exact request header sent by the client, and so is not affected by other rewrites. This is key to preventing an endless static->dynamic->static->dynamic... rewrite loop.

The code above will work in if:

li[]cPath and products_id are always present in the URLs that need to be redirected, and always in that order.

  • cpath contains digits 0-9 and underscore only.
  • products_id contains digits only.
  • There is no requirement to retain the sort variable (sort=3a in your example).

    but if it has shopping_cart.php in the string not to rewrite

    I did not understand this part, since it wasn't clear whether you are talking about the old or new URL. However, the code above will only rewrite requests for "/" as it is, so it appears to meet your requirement.

    Jim

  • fzx5v0

    4:53 pm on Oct 29, 2005 (gmt 0)

    10+ Year Member



    Thanks for help JD sorry for slow responce as been on holidaysbut that does not seem to work.

    I do not understand what is wrong as if you shorten it for just catogaries to

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?cPath=([0-9]+)
    RewriteRule ^/?$ [mysite.co.uk...] [R=301,L]

    but this also is not 100% as it puts an extra cpathxx on the end of the url.

    this url www.mysite.co.uk/?cPath=21 rewrites to
    [mysite.co.uk...]

    do not know why it has put?cpath=21 on the end

    I have a few rules to write but once the first one is correct I can adapt on my own.

    Thanks

    jdMorgan

    8:10 pm on Oct 29, 2005 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    You forgot the question mark at the end of the substitution URL:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?cPath=([0-9]+)
    RewriteRule ^/?$ http://www.example.co.uk/index.php/cPath/[b]%1?[/b] [R=301,L]

    This is required in order to clear the current query string.

    Jim

    fzx5v0

    11:19 am on Oct 30, 2005 (gmt 0)

    10+ Year Member



    Thanks JD one last? by the way I have read all the documentation but it is struggling to sink in

    RewriteRule ^index\.php/cPath/(21_22_93)/sort/3a [mysite.co.uk...] [R=301,L]

    I am using the above rul which works fine but I do not want it to redirect url's that are in the form of

    [mysite.co.uk...]

    that is why I included the index.php in the string but it seems to ingnore that and redirect urls with shopping_cart.php in the url aswell

    I do not understand I thought it would mach the whole URL.

    Is there a way to get around this?