Looking for a more efficient way to do this...

Discuss practical ways rearrange URLs using mod_rewrite.

Looking for a more efficient way to do this...

Postby Quasiuna » Tue Aug 12, 2008 9:41 am

Here is part of our current .htaccess file:

Code: Select all
# aaa section
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} aaa/(.*)$
RewriteRule ^aaa/(.*)$ aaa.php?a=$1 [L]

#bbb section
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} bbb/(.*)$
RewriteRule ^bbb/(.*)$ bbb.php?a=$1 [L]

#ccc section
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ccc/(.*)$
RewriteRule ^ccc/(.*)$ ccc.php?a=$1 [L]


What I'm trying to achieve is the relevant PHP script being called depending on the REQUEST_URI, e.g. if the REQUEST_URI contains aaa, then aaa.php is used, but only id the REQUEST_FILENAME is not a directory or real file.

Does anyone know whether the same thing can be achieved without replicating all of the RewriteCond statements as I've done here?

Any help much appreciated.

Thanks.
Quasiuna
 
Posts: 3
Joined: Tue Aug 12, 2008 9:35 am

Postby richardk » Wed Aug 13, 2008 12:26 pm

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(aaa|bbb|ccc)(/(.*))?$ /$1.php?a=$3 [L]


Or (if you don't want to list each filename)
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)(/(.*))?$ /$1.php?a=$3 [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Quasiuna » Thu Aug 14, 2008 1:43 pm

Genius, many thanks Richard

8)
Quasiuna
 
Posts: 3
Joined: Tue Aug 12, 2008 9:35 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 69 guests

cron