Forum Moderators: phranque

Message Too Old, No Replies

RewriteCond syntax problem

         

irock

5:29 pm on Mar 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi, I was wondering if you guys know what I did wrong with this RewriteCond

RewriteCond %{REQUEST_URI} ^community [OR]
RewriteCond %{REQUEST_URI} ^admin/ [OR]
RewriteCond %{REQUEST_URI} ^administrator/ [OR]
RewriteCond %{REQUEST_URI} ^node/
RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA]
RewriteRule ^(.*)$ redirect.php?page=$1 [QSA,L]

What I want to do with this is to get mod_rewrite to send the request to index.php if the URL contains one of the following: community, admin, administrator or node.

If none of the above has a match, then the request will be sent to redirect.php.

This didn't work as expected. What exactly did I do wrong?

Thanks!

jdMorgan

9:37 pm on Mar 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



REQUEST_URI syntax includes leading slash, and 2nd rule must be prevented from looping:

RewriteCond %{REQUEST_URI} ^/community [OR]
RewriteCond %{REQUEST_URI} ^/admin/ [OR]
RewriteCond %{REQUEST_URI} ^/administrator/ [OR]
RewriteCond %{REQUEST_URI} ^/node/
RewriteRule (.*) /index.php?q=$1 [L,QSA]
#
RewriteCond %{REQUEST_URI} !^/redirect\.php
RewriteRule (.*) redirect.php?page=$1 [QSA,L]

This could also be rewritten as:


RewriteCond $1 ^community [OR]
RewriteCond $1 ^admin/ [OR]
RewriteCond $1 ^administrator/ [OR]
RewriteCond $1 ^node/
RewriteRule (.*) /index.php?q=$1 [L,QSA]
#
RewriteCond $1 !^redirect\.php
RewriteRule (.*) redirect.php?page=$1 [QSA,L]

or even as:


RewriteRule ^((community¦admin/¦administrator/¦node/).*) /index.php?q=$1 [L,QSA]
#
RewriteCond $1 !^redirect\.php
RewriteRule (.*) redirect.php?page=$1 [QSA,L]

Replace all broken pipe "¦" characters with solid pipes before use; posting here modifies pipe characters.

Jim

irock

10:19 am on Mar 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi jdMorgan,

I can't possibly express my feelings when your trick works! THANK YOU SO MUCH!

irock

10:29 am on Mar 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi, just 1 more mini question if you don't mind.

When I enter "http://www.domain.com/community" in the address bar, it returns "http://www.domain.com/community/?q=community". I can't use "community/" with the slash due to the CMS limitation. What other option do I have without the system adding "?q=community" to my URL?

RewriteCond $1 ^community [OR]
RewriteCond $1 ^admin/ [OR]
RewriteCond $1 ^administrator/ [OR]
RewriteCond $1 ^node/
RewriteRule (.*) /index.php?q=$1 [L,QSA]
#
RewriteCond $1!^redirect\.php
RewriteRule (.*) redirect.php?page=$1 [QSA,L]

Thanks for your help again.

jdMorgan

1:42 pm on Mar 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since there are no redirects in this code, it's likely that this external redirect is being done by Apache mod_dir in order to correct the invalid URLs (those with no filetype and no trailing slash). So to avoid that, you'll need to add the slash yourself:

# Add trailing slash if trailing slash is not present and
# URI does not end with a period and filetype (e.g. .gif)
RewriteCond %{REQUEST_URI} !(\.[^/]+¦/)$
RewriteRule (.+) /$1/
#
# Rewrite specific subdirectory requests to index.php
RewriteCond $1 ^community [OR]
RewriteCond $1 ^admin/ [OR]
RewriteCond $1 ^administrator/ [OR]
RewriteCond $1 ^node/
RewriteRule (.*) /index.php?q=$1 [L,QSA]
#
# Rewrite everything else to redirect.php
RewriteCond $1 !^redirect\.php
RewriteRule (.*) redirect.php?page=$1 [QSA,L]

Note also that you may want to add some exclusions to the final rule. In order to make your site more robust, files like robots.txt, /w3c/p3p.xml, labels.rdf, and custom error pages should not be rewritten or scripted if it can be avoided -- You want to reduce dependencies for critical site-control files.

Also consider whether you want or need to rewrite requests for images, external stylesheets and JavaScripts, etc. Simply add exclusions similar that that already in place for /redirect\.php to prevent requests for these files from being rewritten.

Jim