Forum Moderators: phranque
I'm new to working with .htaccess, and have spent time reading and searching this site for similar issues. I think I'm likely missing something fundamental, but I can't see it.
I'm trying to simply perform a mod rewrite in the root directory. If I point my browser at:
[localhost...]
The following rule works:
RewriteRule ^var/([^/\.]+)/?$ /example.php?var=$1 [L]
However, I would like [localhost...] to act the same way, but when I write:
RewriteRule ^/([^/\.]+)/?$ /example.php?var=$1 [L]
I get errors. Any help would be appreciated.
I've tried the solution recommended in another recent article:
Options +FollowSymLinks -MultiViews
RewriteEngine on
#
RewriteCond %{REQUEST_URI}!^/index\.php$
RewriteRule ^(([^/]+/)*[^/]+)/?$ /index.php?mode=$1 [L]
But I get an error message. My error log says:
Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
It appears that it's in an infinite loop.
Your RewriteCond is missing support for the possible subdirectory path-parts:
RewriteCond %{REQUEST_URI} !^/([^/]+/)*index\.php$
RewriteRule ^(([^/]+/)*[^/]+)/?$ /index.php?mode=$1 [L]
RewriteRule ^(([^/]+/)*[^/.]+)/?$ /index.php?mode=$1 [L]
Jim
[edited by: jdMorgan at 7:23 pm (utc) on Mar. 11, 2007]
RewriteRule ^page/([^/\.]+)/?$ /example/menu.php?section=$1 [L]
RewriteRule ^page/([^/\.]+)/([^/\.]+)/?$ /example/menu.php?section=$1&var2=$2 [L]
To replace the second line, I used:
RewriteRule ^(([^/]+/)*[^/.]+)/([^/\.]+)/?$ /example/menu.php?section=$1&var2=$2 [L]
My non-existent understanding of regular expressions is likely my achilles heel here, but if you could help me with this, as well, I'd appreciate it. Additionally, do you know of any good resources to learn regex?
Thanks,
Jesse
[added]Take the period out of the second sub-expression, and count left-parentheses to determine which back-reference to use:
RewriteRule ^(([^/]+/)*[b][^/]+[/b])/([^/.]+)/?$ /example/menu.php?section=$1&var2=[b]$3[/b] [L]
Jim
[edited by: jdMorgan at 7:56 pm (utc) on Mar. 11, 2007]
Thank you for the snippet and resources. I've read the resources you recommended as well as a tutorial found at [regular-expressions.info....] I also found a nice tool called regBuddy, which is helping me figure out regex.
I still have a few questions, though. First of all, am I correct that the string being evaluated is:
(assume [localhost...] or
[localhost...]
rico/suave...
or is it
/rico/suave...?
The code you provided for 2 variables worked fine. But didn't work if there was only one. I've been grinding away with things like:
RewriteRule ^(([^/]+/)*[^/.]+)/?$ /atx07/menu.php?section=$1 [L]
RewriteRule ^(([^/]+/)*[^/]+)/([^/.]+)/?$ /atx07/menu.php?section=$1&var2=$3 [L]
But I couldn't make it work. The first rule grabs everything to the last /.
Any thoughts?
Thanks again,
Jesse
You may in fact not need the first part of the patterns in either rule -- I kept that in there based on the code you posted above. It is the "catch all the rest of the leading URL-path" behaviour of that subpattern that is causing this complication. You might be happier with the more-specific patterns:
RewriteRule ^([^/]+)/([^/]+)/([^/.]+)/?$ /atx07/menu.php?section=$1&var2=$2&var3=$3 [L]
RewriteRule ^([^/]+)/([^/.]+)/?$ /atx07/menu.php?section=$1&var2=$2 [L]
RewriteRule ^([^/.]+)/?$ /atx07/menu.php?section=$1 [L]
In httpd.conf and other server-config files, the URL-path that RewriteRule 'sees' starts with a slash and the full URL-path. In .htaccess, RewriteRule only sees the path in or below the directory in which its .htaccess file is located. So in simplistic terms, RewriteRule won't see a leading slash if the code is in .htaccess.
Jim