Forum Moderators: phranque

Message Too Old, No Replies

Having trouble with RewriteRule. (regex)

htaccess - rewriterule - regular expressions

         

mercedes

8:51 am on Aug 4, 2010 (gmt 0)

10+ Year Member



Hello wworld members,

as i wrote in title, i'm having some troble with regex patterns. i use them in htaccess with rewrtiterule.

following code shows my entire htaccess content.


Options +FollowSymLinks

RewriteEngine On
RewriteBase /dev/regexurl/

RewriteRule ^(register|login|search)\/?$ $1.php

RewriteRule ^(search)(?:\/\?)(.+)$ $1.php?$2

RewriteRule ^(search)(?:\/)([[:lower:]a-z]{1,20})\/?$ $1.php?action=$2

RewriteRule ^(search)(?:\/)([[:lower:]a-z]{1,20})\/\?(.+)$ $1.php?action=$2&$3


in first 3 rules, everyting working perfectly.

First Rule
RewriteRule ^(register|login|search)\/?$ $1.php

browse: hotelspro/dev/regexurl/search/
rewrote as: search.php

working-ok




Second Rule
RewriteRule ^(search)(?:\/\?)(.+)$ $1.php?$2

browse: hotelspro/dev/regexurl/search/?x=x&y=y
rewrote as: search.php?x=x&y=y

working-ok




Third Rule
RewriteRule ^(search)(?:\/\?)(.+)$ $1.php?$2

browse: hotelspro/dev/regexurl/search/action/
rewrote as: search.php?action=action

working-ok




but... the Fourth Rule
RewriteRule ^(search)(?:\/)([[:lower:]a-z]{1,20})\/\?(.+)$ $1.php?action=$2&$3

browse: hotelspro/dev/regexurl/search/action/?x=x&y=y
rewrote as: search.php?action=action

does not work



so, the trouble is in with the fourth rule.

i couldn't get it work.

it should be rewritten as "search.php?action=#*$!xx&y=y&z=z"


so waitting for you help mates. :(

jdMorgan

2:02 pm on Aug 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are trying to match and to create back-references to query strings using RewriteRule patterns. That will not work, because RewriteRule only "sees" the local URL-path. It cannot "see" query strings appended to the URL.

In fact, all of your rules that include "?$1" or "?$1$2" are probably broken, despite what you saw in testing (Did you delete your browser cache between tests?)

I will simplify your rule somewhat, to avoid introducing additional errors, but something like:

RewriteCond %{QUERY_STRING} ^x=[^&]+&y=[^&]+z=[^&]+
RewriteRule ^search/([a-z]{1,20})/ search.php?action=$1 [QSA,L]

would be closer to correct. Here, we match the query string in a RewriteCond, and only the URL-path in the RewriteRule. If it matches, we take the slash-delimited "word" that follows "search" and use that as the "action=" value. We then append the original query string by using the [QSA] flag, and end mod_rewrite processing for this pass using [L].

In your other rules, precede your "$1" substitution paths with a slash if possible. This prevents a particular kind of hack where the client can control the root filepath. Use "/$1" instead, unless it causes your rule to malfunction (as it can do on some server configurations).

Jim

[edited by: jdMorgan at 1:21 pm (utc) on Aug 5, 2010]

g1smd

2:24 pm on Aug 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Additional comments.

Every rule needs the [L] flag (as in jd's example), and the slashes in patterns do not need escaping.

mercedes

8:13 am on Aug 5, 2010 (gmt 0)

10+ Year Member



thank you both, much.
i've just learned the flags. =)