Forum Moderators: phranque

Message Too Old, No Replies

Apache Mod Rewrite rule problem

         

blainehilton

2:24 am on Jan 10, 2005 (gmt 0)

10+ Year Member



My problem is that I am converting a live site to a new PHP based system, while keeping the old system running as well.

I have mod_rewrite working, but need to create a rule and my regex skills leave a lot to be desired.

I have:

RewriteRule ^calc/(.*) /calc/beta/index.php?id=$1

This will take any url in the form of [localhost...] and make it go to index.php?id=100, however I need this changed so that it ONLY works if the url doesn't end in ".php". So if somebody goes to [localhost...] it should pull up the file /calc/100.php, but if it is [localhost...] it needs to go to index.php?id=100.

I hope it doesn’t matter but this will be running on systems with Apache 1.x and 2.x and PHP 4.x and PHP 5.x.

--
Thanks in advance
Blaine

jdMorgan

3:29 am on Jan 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Blaine,

Simply add a condition to your rule:


RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^calc/(.*) /calc/beta/index.php?id=$1

An alternate form would be:

RewriteCond $1 !\.php$
RewriteRule ^calc/(.*) /calc/beta/index.php?id=$1

Where the RewriteCond back-references the same value ($1) that your substitution does.

The "!" character is a NOT operator in mod_rewrite, and the backslash escapes the literal period in the ".php" filetype.

Jim

blainehilton

5:40 am on Jan 10, 2005 (gmt 0)

10+ Year Member



That first example did the trick! Thanks, I'll have to remember the meaning of the!.

--
Blaine