Forum Moderators: phranque

Message Too Old, No Replies

another root rewrite question

         

Ajime

6:49 pm on Mar 11, 2007 (gmt 0)

10+ Year Member



Hi there,

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.

Ajime

7:16 pm on Mar 11, 2007 (gmt 0)

10+ Year Member



As a note:

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.

jdMorgan

7:16 pm on Mar 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please, what errors? What is in your server error log?

Jim

Ajime

7:20 pm on Mar 11, 2007 (gmt 0)

10+ Year Member



Hi Jim,

To answer your question. With my original code, I get sent to my error page and the log shows:

[error] [client 127.0.0.1] File does not exist: C:/AppServ/www/test/page

Thanks,
Jesse

jdMorgan

7:22 pm on Mar 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cross--posted. Sorry.

Your RewriteCond is missing support for the possible subdirectory path-parts:


RewriteCond %{REQUEST_URI} !^/([^/]+/)*index\.php$
RewriteRule ^(([^/]+/)*[^/]+)/?$ /index.php?mode=$1 [L]

You could also exclude any URL which has a file-extension in the last part of the path:

RewriteRule ^(([^/]+/)*[^/.]+)/?$ /index.php?mode=$1 [L]

If you do it this way, then you don't need the RewriteCond.

Jim

[edited by: jdMorgan at 7:23 pm (utc) on Mar. 11, 2007]

Ajime

7:33 pm on Mar 11, 2007 (gmt 0)

10+ Year Member



Thank you very much. That works great with a single variable. What would I need to do if I had multiple variables? The code I've been toying with is:

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

jdMorgan

7:48 pm on Mar 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com] for useful resources.

[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]

You do not need to escape periods within character-[groups].
[/added]

Jim

[edited by: jdMorgan at 7:56 pm (utc) on Mar. 11, 2007]

Ajime

5:42 pm on Mar 12, 2007 (gmt 0)

10+ Year Member



Hi Jim,

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

jdMorgan

6:26 pm on Mar 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Reverse the rules... Long one first.

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]

(Three-variable expansion shown just for an example.)

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

Ajime

6:59 pm on Mar 12, 2007 (gmt 0)

10+ Year Member



:)

Of course...It didn't even occur to me to reverse the order.

Thanks again,
Jesse