Page 1 of 1
Regex problem in htaccess
Posted: Thu Oct 29, 2009 10:39 am
by Inkyskin
Hi!
I'm having a bit of an issue in a htaccess file. I have lines like this (which work great):
Code: Select all
RewriteRule ^(.*?)/$ /news/_index.php?version_url=$1 [L]
But, If I change it to this, it seems to cause a Error 500:
Code: Select all
RewriteRule ^(.*?)$ /news/_index.php?version_url=$1 [L]
All I'm doing is removing the trailing slash, but it just kills it dead... Anyone have any ideas?
Thanks!
Re: Regex problem in htaccess
Posted: Fri Oct 30, 2009 9:43 am
by ridgerunner
Looks like it should work. (Although applying the lazy ? modifier to the * is not necessary and slightly inefficient).
Can you post more (or all) of the .htaccess file? RewriteCond statements preceeding the Rule may be a factor...
(Hey there seems to be spel czheking here now!) Edit: actually it appears that the spell checking is a feature of the new Opera version 10 - cool!
Re: Regex problem in htaccess
Posted: Fri Oct 30, 2009 10:07 am
by Inkyskin
Hey thanks
Code: Select all
RewriteEngine on
RewriteRule ^(.*?)/(.*?)/$ /news/_index.php?version_url=$1&version=$2 [L]
RewriteRule ^(.*?)/$ /news/_index.php?version_url=$1 [L]
RewriteRule ^(.*?)/(.*?)/preview$ /news/_index.php?version_url=$1&version=$2&preview=true [L]
RewriteRule ^(.*?)/preview$ /news/_index.php?version_url=$1&preview=true [L]
Thats the full file for now - nothing major. What I'm attempting to do, is also catch files that end in .php and send them to _index.php aswell (And sending the .php filename as a value in the query string), but stripping the trailing slash just causes instant Error 500. Could it be something in the server set up rather than the regex you think?
Re: Regex problem in htaccess
Posted: Fri Oct 30, 2009 11:08 am
by ridgerunner
You can easily match any URL ending in ".php" with a regex equal to '^(.*\.php)$'. Also, it appears that you want to match URLs having a specific path "depth" (i.e. number of forward slashes). But your regex uses the dot .*? which matches the forward slash, so this may also be part of (or another) problem. In other words, your '^(.*?)/(.*?)/$' regex matches folders two levels deep, but it also matches folders that are twenty levels deep! Instead of the "match-anything" dot, try using the more specific "match-anything-that-is-not-a-forward-slash" '[^/]*' instead. Try the following:
Code: Select all
RewriteEngine on
RewriteRule ^([^/]*)/([^/]*)/preview$ /news/_index.php?version_url=$1&version=$2&preview=true [L]
RewriteRule ^([^/]*)/([^/]*)/$ /news/_index.php?version_url=$1&version=$2 [L]
RewriteRule ^([^/]*)/preview$ /news/_index.php?version_url=$1&preview=true [L]
RewriteRule ^([^/]*)/$ /news/_index.php?version_url=$1 [L]
RewriteRule ^(.*(?<!_index)\.php)$ /news/_index.php?php_file_URL=$1 [L]
I've added a negative lookbehind to the .php rule to only rewrite .php files that are not _index.php - (otherwise it could loop back on itself). You don't want to take a .php file and rewrite it to be another .php file - infinite loop! (This was likeley the cause of the problem when you took away the ending '/'). And of course, you need to fix the destination for the '\.php$' rule.
Edit: now that I think about it, your '^(.*?)$' rule matches (and is re-writing)
every URL in the universe!. That was resulting in an infinite loop and was most definitely the main problem!
Re: Regex problem in htaccess
Posted: Fri Oct 30, 2009 11:24 am
by Inkyskin
Thats worked PERFECTLY, thanks!
