Page 1 of 1
.htaccess and regex working unexpectedly
Posted: Wed Apr 23, 2008 9:01 am
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!
Re: .htaccess and regex working unexpectedly
Posted: Wed Apr 23, 2008 10:05 am
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?
Re: .htaccess and regex working unexpectedly
Posted: Wed Apr 23, 2008 10:29 am
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:
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.
Re: .htaccess and regex working unexpectedly
Posted: Wed Apr 23, 2008 11:19 am
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.
Re: .htaccess and regex working unexpectedly
Posted: Wed Apr 23, 2008 11:57 am
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

Re: .htaccess and regex working unexpectedly
Posted: Fri Apr 25, 2008 2:42 am
by prometheuzz
You're welcome, and I'd appreciate you posting the solution back here if you eventually find out!
Good luck.