Forum Moderators: phranque

Message Too Old, No Replies

Mutliple Rules, dynamic URL

rewriting multiple urls with multiple rules

         

mjasra23

3:24 am on Aug 3, 2006 (gmt 0)

10+ Year Member



I started out with the following and it works great:

RewriteRule example-(.*)\.htm$ example.php?view=$1 [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /example\.php\?view=([^&]+)\ HTTP/
RewriteRule ^example\.php$ http://www.example.com/z_index-%1\.htm? [R=301,L]

so my URL example.php?view=checkout rewrites to example-checkout.htm

But then I had to make it so that example.php?view=checkout&step=2 rewrites to example-checkout-2.htm. I tried the following:

RewriteRule example-(.*)\.htm$ example.php?view=$1 [L]
RewriteRule example-(.*)-([0-9]+)\.htm$ examplephp?view=$1&step=$2 [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /example\.php\?view=([^&]+)\ HTTP/
RewriteRule ^example\.php$ http://www.example.com/z_index-%1\.htm? [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /example.php\?view=([^&]+)&step=([^&]+)\ HTTP/
RewriteRule ^example\.php$ http://www.example.com/z_index-%1-%2\.htm? [R=301,L]

This however ends up rewriting it to: example-checkout.htm?view=checkout&step=2

Can anyone help?

[edited by: jdMorgan at 2:55 pm (utc) on Aug. 3, 2006]
[edit reason]
[1][edit reason] example.com [/edit]
[/edit][/1]

jdMorgan

2:54 pm on Aug 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your rules are in the wrong order, and your (.*) pattern in the first rule will happily match *anything*, including requests with multiple hyphens. Therefore the first rule is always applied, leading to your problem.

I'd suggest:


# Internally rewrite .htm URL request to script
RewriteRule example-([^-]+)-([0-9]+)\.htm$ examplephp?view=$1&step=$2 [L]
RewriteRule example-([^-]+)\.htm$ example.php?view=$1 [L]
#
# Externally redirect requested dynamic URLs to static equivalents, to clean up SE listings
@
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /example.php\?view=([^&]+)&step=([^&]+)\ HTTP/
RewriteRule ^example\.php$ http://www.example.com/z_index-%1-%2\.htm? [R=301,L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /example\.php\?view=([^&]+)\ HTTP/
RewriteRule ^example\.php$ http://www.example.com/z_index-%1\.htm? [R=301,L]

The "[^-]+" pattern means, "one or more characters not equal to a hyphen." Therefore, the now-second rule will not match if there is an additional hyphen in the requested URL, and known cases of two-hyphen .htm page requests will have already been rewritten by the first rule anyway.

General advice: Avoid the use of the greedy and promiscuous ".*" patterns whenever possible; Use a more-specific pattern. Order your rules from most-specific and longest to least-specific and shortest.

Jim

mjasra23

5:44 pm on Aug 4, 2006 (gmt 0)

10+ Year Member



Thanks for the response Jim. Yes you're right I should not be using .* anywhere.

I am still seeing similar results.

So this works: example.php?view=checkout rewrites to example-checkout.htm

However, when I am on this page: example-checkout.htm and click the "next step" button in the checkout process, which has a URL of example.php?view=checkout&step=2

it gets rewritten to:
example-checkout.htm?view=checkout&step=2
hence making the checkout process not work.

Similarily: example.php?view=shop&cat_id=100 gets rewritten to example-shop-100.htm perfectly.

but when going to the next page of results where the URL is: example.php?view=shop&cat_id=100&start=8&page=2

it gets rewritten to
example-shop-100.htm?view=shop&cat_id=100&start=8&page=2

So if a portion of the querystring is already contained in the proceeding URL it breaks.

Thanks.

jdMorgan

6:35 pm on Aug 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



All the rules posted above that will rewrite a .php URL will rewrite that .php URL to "z_index-<something".

So it appears that the problem is not with the rules above, but with some other code that is interfering. Do you have additional rewrites/redirects in httpd.conf, or in any other .htaccess files in other directories?

Either that, or the rules above and the code that you actually have on your site are not the same.

There is a much bigger problem, though. You should not be using mod_rewrite to redirect the dynamic URLs to static equivalents *except as a way to correct old search engine listings*. All conversion of dynamic to static URLs should be done inside your shopping cart script (using preg_replace, for example).

Maybe this primer [webmasterworld.com] in our forum library will be helpful. You're likely familiar with much of it, but look for details...

Jim

mjasra23

7:52 pm on Aug 4, 2006 (gmt 0)

10+ Year Member



I will look into the underlying code, for the time being how would I add a condition to your orignial suggestion amd update so that it will not rewrite a URL that contains any parameter after the?view=.

i.e.: example.com?view=test would get rewritten, but example.com?view=test&step=1&var=2 wouldn't.

Thanks.

jdMorgan

1:39 pm on Aug 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The second and third rules above both clear the query string when redirecting direct requests for dynamic URLs back to their static equivalents. Neither will generate a redirect to a URL containing a query string -- ".html?<anything>" -- on a normally-functioning server.

The second rule will accept only requests with a query of "view=<parm>&step=<parm>"
The third rule will accept only requests with a query of "view=<parm>"

Neither rule will accept additional parameters, as a result of the test for <space>/HTTP at the end.

So there is a "logical disconnect" between what the code above will do, and the results that you are reporting.

Therefore, the problem appears to be with *other* rewrite or redirect code, or with your PHP code. But if you have no time to review the references I cited in order to better understand the problem, then I'm sorry but I can't do much more for you.

Jim