Forum Moderators: phranque

Message Too Old, No Replies

Mod_rewrite issues

RewriteRule and RewriteCond

         

ellisgolf

3:26 am on Apr 23, 2004 (gmt 0)

10+ Year Member



I'm having difficulties getting my RewriteCond to work properly. My problem is this, I want a rewrite to occur ONLY if there isn't a PHP file being accessed.

---------------------------------------------------
What I'm trying to do......
---------------------------------------------------
[mydomain.com...]
REWRITES TO
[mydomain.com...]

BUT

[mydomain.com...]
STAYS AT
[mydomain.com...]

(NOT: [mydomain.com...]
----------------------------------------------------

This is the difficulty for me. I can get the first set to work, but not the second. With my rules I also have trouble getting the plain old [mydomain.com...] to load up properly. Are there any gurus out there that know what I'm doing wrong? Here is the code I have been trying to use:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/([a-z0-9]*)$
RewriteRule ^(.*) jump.php?section=$1

I have no idea what is the problem at this point and need a fresh perspective. Thanks!

-Ryan

jdMorgan

4:10 am on Apr 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ryan,

Welcome to WebmasterWorld [webmasterworld.com]!

Your RewriteCond as written requires that the requested URI contain a slash followed by any number (including zero) of only the characters a to z lowercase or the digits 0 to 9. So, as long as there are no characters other than those in the URI, the redirect will occur. That's why you get a redirect on your index file when requested as "/" or blank.

I would suggest that you change your RewriteCond to something like:


RewriteCond %{REQUEST_URI} !^/?$
RewriteCond %{REQUEST_URI} !\.php$

meaning that the following rule will only be invoked if the requested URI does NOT end in ".php". Note that query strings are not considered to be part of the requested URI - They are simply a list of arguments to be passed to the resource AT the URI. Therefore, the query string (if any) will not be 'seen' by this RewriteCond, and won't affect anything.

You could also write that more compactly as


RewriteCond %{REQUEST_URI} !(^/?¦\.php)$

but remember to edit the "¦" and change it to a solid vertical pipe before use - Posting on this board changes them.

Apache mod_rewrite documentation [httpd.apache.org]
Apache URL Rewriting Guide [httpd.apache.org]
Regular-Expressions Tutorial [mnot.net]

Jim

ellisgolf

12:21 pm on Apr 23, 2004 (gmt 0)

10+ Year Member



Oh that is great....works like a charm. You are a amazing! The only problem I encountered was that my images would not load anymore, so I added another condition (same one) with the .jpg extension on it.

Thank you very much!

-Ryan