Forum Moderators: phranque

Message Too Old, No Replies

Tricky rewrite

         

bluesting

8:02 am on Jul 13, 2006 (gmt 0)

10+ Year Member



Hi,

Been battling with this tricky rewrite script for 2 days now, getting on my nerves, thought I'd throw it out there and see if there's any suggestions or advice.

OK, I want any URL that matches a REAL file/directory to simply execute. For all other matches, I have a separate set of rules.

REAL files/directories:

http://www.example.com/users/
http://www.example.com/users/login.php
http://www.example.com/advertising/
http://www.example.com/faq/
http://www.example.com/item.php
http://www.example.com/items.php

For these I use a rewrite like this which should just continue the script normally and end the .htaccess file:

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -s [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -d
RewriteRule ^(.*) - [L]

This script just want to pass ANYTHING as true, so everything fails here and I'm not sure what else to try, then further down ...

VIRTUAL files/directories:

http://www.example.com/intel/
http://www.example.com/intel/item.php
http://www.example.com/gigabyte/
http://www.example.com/gigabyte/item.php
http://www.example.com/asus/
http://www.example.com/msi/

For this I use this script, which is WORKING (except that it is also working on any of the REAL files):

RewriteRule ^(.*)/$ /items.php [L]
RewriteRule ^(.*)/item.php$ /item.php [L]


Problem: I need the script to terminate normally if it finds a REAL existing file/directory!

Regards

bluesting

8:19 am on Jul 13, 2006 (gmt 0)

10+ Year Member



Never mind guys, sorry ... amazing how you can read things over and over and then after reading it for the hundredth time something stands out that you never "read" before ...

Not sure where that "DOCUMENT_ROOT" #*$! came from, some dude that was trying to "help" actually set me back!

Here's the new WORKING (I hope) script:

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*) - [L]

RewriteRule ^(.*)/$ /items.php [L]
RewriteRule ^(.*)/item.php$ /item.php [L]

Can anyone see any possible issues?

Regards

jdMorgan

6:54 pm on Jul 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You don't need to parenthesize sub-patterns unless you want to create a back-reference to them or to group them for the purpose of applying a regular-expressions quantifier to that group, e.g. "(abc)+" "(123)?", "(z3x)*" or "(ab){3,6}".

Start and end anchors are not required on a ".*" pattern standing alone.

When "^" or "$" is found adjacent to ".*", then both may be eliminated, as they add no meaning.

So, your code could be shortened/simplified to:


RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
#
RewriteRule /$ /items.php [L]
RewriteRule /item.php$ /item.php [L]

For more information, see the regular expressions documentation cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim