Forum Moderators: phranque

Message Too Old, No Replies

Yet another rewrite questions :-)

rewrite exempt existing directories

         

Gregology

1:40 pm on Feb 19, 2010 (gmt 0)

10+ Year Member



Hello, I've been trawling through forums for ages and still haven't gotten my head completely around the rewrite rules.

I want
domain.tld/particulardivision/particularpage to forward to domain.tld/index.php?division=particulardivision&page=particularpage

and

domain.tld/particulardivision to forward to domain.tld/index.php?division=particulardivision

and I've figured out how to get that working, but I would also like

domain.tld/library/somefolder/anotherfolder/flash.swf to work and be unaffected by the rewrite rules.

here's what I've got so far which is working for the first two parts. I would appreciate any help on the topic.

Thanks,

Gregology.

Options +FollowSymLinks
RewriteEngine On
rewriteCond %{REQUEST_FILENAME} !-f
rewriteCond %{REQUEST_FILENAME} !-d
rewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)/(.+)/?$ index.php?division=$1&page=$2
rewriteCond %{REQUEST_FILENAME} !-f
rewriteCond %{REQUEST_FILENAME} !-d
rewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)/?$ index.php?division=$1 [L]

jdMorgan

2:35 pm on Feb 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This may not be exactly what you want, but the method will improve your server performance enough that it is worth some study and some tweaking if needed. The main advantage is the "centralized" file- and directory-exists check block, and the fact that these checks are only run when absolutely necessary and will 'quit early' if any one of them matches.

The exclusion of /library/somedir/otherdir/xyz.swf requests is accomplished both as a by-product of the more-specific/more-efficient patterns in the RewriteConds and RewriteRules, and by the explicit exclusion of "library/" directory-path by the "skip rule". In addition, the URLs to be rewritten are canonicalized by removing their trailing slashes to avoid duplicate-content, as is the hostname.

Options +FollowSymLinks
RewriteEngine on
#
# Externally redirect to remove trailing slash unless request
# resolves to an existing directory (canonicalize the SEF URLs)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(([^/]+/)*[^/]+)/$ http://www.example.com/$1 [R=301,L]
#
# [i]Domain canonicalization redirect rule goes here (This example forces "www")[/i]
# Externally redirect non-canonical hostname requests to canonical hostname
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#
# Skip the following two internal rewrites to the index.php script if the request has already
# been rewritten to the index.php script, ends with slash or a filetype, contains more
# than two directory levels, or resolves to an existing file, directory, or link
RewriteCond $1 ^index\.php$ [OR]
RewriteCond $1 ^library/ [OR]
RewriteCond $1 !^([^/]+/)?[^/.]+$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^(.*)$ - [S=2]
#
# Internally rewrite URLs of the form "/dir/name" or "/name"
# to index.php script (Qualified by the skip rule above)
RewriteRule ^([^/]+)/([^/.]+)$ index.php?division=$1&page=$2 [L]
RewriteRule ^([^/.]+)$ index.php?division=$1 [L]

Delete your browser cache before testing any new code.

If your "particular division" and "particular page" URLs can be further qualified --for example, if they are known not to contains anything but letters and hyphens or underscores-- then the efficiency of this code can be further improved. As it stands now, these parameters may contain any characters except for "/" or a period.

Jim

Gregology

10:13 pm on Feb 19, 2010 (gmt 0)

10+ Year Member



That works perfectly, thank you for a very good explanation.