Forum Moderators: phranque

Message Too Old, No Replies

Dodgy rewrite rule

         

jontce

11:15 am on Oct 27, 2009 (gmt 0)

10+ Year Member



Here's my code.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $2 !^projects/([^/]*?)/(index\.php¦assets¦robots\.txt¦favicon\.ico¦readme\.txt¦temp)
#RewriteCond /projects/$1 -F
RewriteRule ^projects/([^/]+)(.*)$ /projects/$1/index.php/$2 [L]
</IfModule>

See the commented out line? Let's say I have two URLs:

/projects/a
/projects/b

a is a physical folder that exists, b does not exist. I'm trying to get it so:

/projects/a/somecontroller/someparam goes to:
/projects/a/index.php/somecontroller/someparam

AND

/projects/b/<anything here> goes to a 404.

Any pointers?

jdMorgan

1:48 pm on Oct 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are many things that are unclear here, such as why you are testing -f and -d in addition to your other stated conditions. So this is just a guess to put you on the right path:

RewriteEngine On
#
# if not an exempted final URL-path-part
RewriteCond $2 !^(index\.php¦assets¦robots\.txt¦favicon\.ico¦readme\.txt¦temp)
# if requested URL-path does not resolve to an existing file
RewriteCond %{REQUEST_FILENAME} !-f
# if requested URL-path does not resolve to an existing directory
RewriteCond %{REQUEST_FILENAME} !-d
# if document_root+/projects/<subdirectory>/index.php exists
RewriteCond %{DOCUMENT_ROOT}/projects/$1/index.php -f
# then rewrite /projects/<subdir>/<name> to /projects/<subdir>/index.php/<name>
RewriteRule ^projects/([^/]+)/(.*)$ /projects/$1/index.php/$2 [L]

You can dump the <IfModule> container, unless you plan to deploy this code to multiple servers and you want the code to fail silently on servers where mod_rewrite is not installed.

"RewriteBase /" is the default, and a waste of time unless you previously set it to something else and need to set it back.

Always put RewriteConds for -d, -f, etc. filesystem checks and reverse-DNS lookups *last* in the set of RewriteConds whenever possible -- all are *very* resource-intensive, and should not be done unless all other conditions have been checked first, and it is determined that these additional checks are actually required. Understand that the code posted here will make three calls to the operating system's filesystem, invoking thousands of lines of server code. In addition, if the filesystem state is not cached, this could results in up to three read operations on the physical disk for each request that matches the rule pattern and passes the preceding RewriteConds. This kind of thing is known to force early and otherwise-unnecessary server upgrades...

Replace the broken pipe "¦" characters above with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Jim