nethnet Posted March 31, 2011 Share Posted March 31, 2011 I've written a regex as part of my mod_rewrite schematic. It works as it should, however, for some reason it allows an infinite number of forward slashes. I'm not sure why it would allow this, could someone help me fix it? ^tutorials/([0-9]{3,})(/([0-9]{1,2}))?(/([^/]*)/?)?$ What it should do is match strings in the following format: tutorials/123/12/example-string/ Any of the three parameters can vary, obviously, and moreover, the last two are optional. Therefore, any of the following would be allowed: tutorials/123/ tutorials/123/12/ tutorials/123/example-string/ Like I said, this is all working, but I'm having a strange issue with forward slashes. If a user duplicates any of the forward slashes in the URL, it still works for some reason. I don't want this to be the case. The following all work, when they shouldn't: tutorials////////123///// tutorials/123/12/example-string///// ///////tutorials/123/12/ Why is this happening? Even when these slashes are added to the URL, each parameter still points to the variable it is meant to, so it isn't affecting the page at all, I just don't want it to match. All help appreciated. P.S. I've tried adding {1} after all of the slashes in the REGEX, but this didn't solve the issue. Quote Link to comment Share on other sites More sharing options...
.josh Posted April 1, 2011 Share Posted April 1, 2011 hmm...the only flaw I outright see in it is you should be using a + instead of * towards the end there, as it will allow for tutorials/123/12// But other than that...here is what I get as results, using a list of both your good and bad examples: Array ( [0] => Array ( [0] => tutorials/123/ [1] => tutorials/123/12 [2] => tutorials/123/example-string/ ) [1] => Array ( [0] => 123 [1] => 123 [2] => 123 ) [2] => Array ( [0] => [1] => /12 [2] => ) [3] => Array ( [0] => [1] => 12 [2] => ) [4] => Array ( [0] => / [1] => [2] => /example-string/ ) [5] => Array ( [0] => [1] => [2] => example-string ) ) IOW I'm thinking your problem isn't really with the regex but somewhere else in your mod_rewrite Quote Link to comment Share on other sites More sharing options...
nethnet Posted April 2, 2011 Author Share Posted April 2, 2011 Thanks for that input CV. I think you are right with there being an error elsewhere in the mod_rewrite. Upon playing with it a bit more, I found that ALL of my rewrites were allowing this strange behavior, even simple ones like RewriteRule ^home/?$ index.php This was accepting things like /////home and ///home/// for some strange reason. The contents of .htaccess before the RewriteRules is as following: AddType 'text/html; charset=utf-8' php Options -Indexes ErrorDocument 410 /410/ ErrorDocument 404 /404/ ErrorDocument 403 /403/ ErrorDocument 302 /302/ ErrorDocument 301 /301/ RewriteEngine on RewriteCond %{HTTP_HOST} !^www.nethnet.com$ RewriteRule ^(.*)$ http://www.nethnet.com/$1 [R=301] I don't see how anything would be allowing this. Is there perhaps an Apache directive that correlates to this behavior? All of a sudden this has gone far beyond the realm of REGEX. Apologies.. Quote Link to comment Share on other sites More sharing options...
nethnet Posted April 2, 2011 Author Share Posted April 2, 2011 I was able to rectify the situation with the following solution: RewriteCond %{REQUEST_URI} ^(.*)//(.*)$ RewriteRule . %1/%2 [R=301,L] I'm posting this here for anyone who may encounter a problem like this in the future. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.