.htaccess and regex working unexpectedly

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
Luna-tic
Forum Newbie
Posts: 3
Joined: Wed Apr 23, 2008 8:47 am

.htaccess and regex working unexpectedly

Post by Luna-tic »

Good day.

I have an http sever and I use mod_rewrite. My .htaccess file looks like this:

Code: Select all

 
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
/([a-z\/-]+)/$    /index.php?map=$1
 
It works fine for queries such as /category1/category2/ and it gives map variable the "category1/category2" string. All that is ok. However, if I enter an URL like /category1/category2/////////// it passes the map variable only "category1/category2".
What I don't understand is where does that series of slashes disappear, because they are not in the constant part of the expression nor are they passed on through the map variable. Could someone please explain me why is this happening?

Thanks!
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: .htaccess and regex working unexpectedly

Post by prometheuzz »

Hi, I have no idea how this mod_rewrite module rewrites urls, but perhaps you could explain your problem in a more "regex general" way?
Perhaps you could some example input and desired output?
Luna-tic
Forum Newbie
Posts: 3
Joined: Wed Apr 23, 2008 8:47 am

Re: .htaccess and regex working unexpectedly

Post by Luna-tic »

prometheuzz wrote:Hi, I have no idea how this mod_rewrite module rewrites urls, but perhaps you could explain your problem in a more "regex general" way?
Perhaps you could some example input and desired output?
Good point. So, my expression looks like this:

Code: Select all

/([a-z\/-]+)/$
And if this was used in PHP str_replace() function it would like this:

Code: Select all

str_replace('/\/([a-z\/-]+)\/$/', '$1', $data)
In this case, if $data was "/abc/abc-a/", the function should return "abc/abc-a".
However, if $data was "/abc/abc-a////////", the function returns the same. So what I don't understand is where do those repeating slashes disappear and why don't they get passed on to $1.
Hope You understand my problem a little better now.

EDIT: Sorry, I included the numbers in the examples by mistake. Fixed.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: .htaccess and regex working unexpectedly

Post by prometheuzz »

Thanks, for the explanation.
B.t.w., I think you meant preg_replace(...) instead of str_replace(...).
But I don't see any problem with your regex. When executing the following lines of code:

Code: Select all

#!/usr/bin/php
 
<?php
 
  $data = "/abc/abc-def/";
  $repl = preg_replace('/\/([a-z\/-]+)\/$/', '$1', $data);
  echo "[1] " . $data . " -> " . $repl . "\n";
  
  $data = "/abc/abc-def///";
  $repl = preg_replace('/\/([a-z\/-]+)\/$/', '$1', $data);
  echo "[2] " . $data . " -> " . $repl . "\n";
 
?>
I see the following as output:

Code: Select all

[1] /abc/abc-def/ -> abc/abc-def
[2] /abc/abc-def/// -> abc/abc-def//
which is exactly what you want, correct?
If so, then I suspect the regex engine that rewrite-module is using differs from PHP's engine.
Luna-tic
Forum Newbie
Posts: 3
Joined: Wed Apr 23, 2008 8:47 am

Re: .htaccess and regex working unexpectedly

Post by Luna-tic »

Yes, I suppose You're right. I'll try getting some feedback from someone who knows mod_rewrite by heart :)

Thanks!

P.S. Yes, I meant preg_replace :)
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: .htaccess and regex working unexpectedly

Post by prometheuzz »

You're welcome, and I'd appreciate you posting the solution back here if you eventually find out!

Good luck.
Post Reply