Forum Moderators: phranque

Message Too Old, No Replies

rewrite rule

         

cocoy

1:57 pm on Sep 14, 2005 (gmt 0)

10+ Year Member



planning to try mod rewrite but still confused on some rewrite conditions.

what is the difference between these two conditions?

([^-]+)-([^-]+) & (.*)-(.*)

dcrombie

4:03 pm on Sep 14, 2005 (gmt 0)



The difference is that if the input is:

aaa-bbb-ccc

then the first will return:

$1 = aaa
$2 = bbb

and the second (I think):

$1 = aaa-bbb
$2 = ccc

jd01

5:29 pm on Sep 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



aaa-bbb-ccc

The above is correct, but actually, there is more to the difference:
(It is difficult to explain, so I may not be explaining the exact matching sequence exactly, but it is close and should give you an idea.)

([^-]+)-([^-]+) = Any one or more characters that is not a - followed by a - followed by one or more characters that is not a -

This pattern will correctly break as soon as it hits the first -, match the - and then break when it hits the second - : the output aaa-bbb would be stored on the first pass.

(.*)-(.*) = 0 or More characters that are not the end of a line, followed by a - followed by 0 or More characters that are not the end of a line.

The first .* will store the entire line, and check for a match to the pattern, because every character is 'not the end of a line' when the store pattern does not match the set pattern, the engine will then 'back-up' until it hits the second - in the pattern, which creates a match for the pattern up to the *second* - then the .* will match the rest of the line.

Output is: aaa-bbb ccc

To get a correct match from .* you would have to use (.*)-(.*)-(.*) which will cause the engine to match (1st .*), back-up (gets to the second -), match (2nd .*), back-up (1st .* gets to the first -), match (2nd .*), back-up (gets the 2nd .* to the second -), match (3rd .*) finally gets to the end of the line.

Conversly:
([^-]+)-([^-]+)-([^-]+) Would match on one pass.

Hope this makes a little sense.

Justin

cocoy

1:23 pm on Sep 15, 2005 (gmt 0)

10+ Year Member



thanks for the info.

If I have this url:
[domian.com...]

and wanted to change into:
[domain.com...]

Is this rewrite correct?

Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteBase /cars/
RewriteRule ^index/(.*)/(.*)/(.*)\.html$ /search.php?c=$1&s=$2 [L]

Please advise.

jdMorgan

3:05 pm on Sep 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My best advice would be to re-read the post above that jd01 took so much time composing for you. You can use (.*) if you like, but it's horribly inefficient. And as he pointed out, it may give unexpected results with URL-paths containing more than three slashes.
I'd suggest the much more precise and efficient rule:

RewriteRule ^index/([^/]+)/([^/]+)/([^.]+)\.html$ /search.php?c=$1&s=$2 [L]

instead.

But that still doesn't match the description of what you say you want to do.


RewriteRule ^free/search/([0-9])([0-9]{2})\.html /free/search.php?c=$1&s=$2 [L]

is closer to what you want, assuming that it is installed in .htaccess in your root directory.

You will also need to manually change the URLs on your php-generated pages to the "761.html" form in order for this to work. The client browsers and spiders will then see "761.html" and request it. Your mod-rewrite code will then call search.php with the "7" and "61" extracted from the requested URL and placed into the parameters expected by search.php.

If you want this project to succeed, you'll need to study the documents cited in our forum charter [webmasterworld.com] and in the Apache section of the WebmasterWorld library [webmasterworld.com].

Jim

cocoy

6:23 am on Sep 16, 2005 (gmt 0)

10+ Year Member



thanks for pointing out the resource.

cocoy

8:30 am on Sep 17, 2005 (gmt 0)

10+ Year Member



is it possible to test mod rewrite on localhost?

since I couldn't find on the resource you've given. :)

jdMorgan

3:58 pm on Sep 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can test on localhost if you have installed and configured Apache on your local machine.

Jim