Disable re-rewriting

Discuss practical ways rearrange URLs using mod_rewrite.

Disable re-rewriting

Postby bilygates » Sun Oct 05, 2008 3:36 am

Hello,

Let's suppose I have the following rewrite rules in my .htaccess file:

Code: Select all
RewriteEngine on

RewriteRule ^firstpage$ firstpage.php [L]

RewriteRule ^secondpage$ secondpage.htm [L]

RewriteRule ^(.*)$ otherpage.php


Basically, I want it to redirect "firstpage" to "firstpage.php", "secondpage" to "secondpage.htm" and everything else to "otherpage.php".
But, for example, if I access "firstpage", it internally redirects to "firstpage.php" and then internally redirects again to "otherpage.php", because "firstpage.php" matches "(.*)".

I hope I have made myself understood. Is there any way to fix this?
Thanks!
bilygates
 
Posts: 1
Joined: Sun Oct 05, 2008 3:27 am

Postby richardk » Sun Oct 05, 2008 11:13 am

You have to use RewriteConds to add restrictions to the final rule.

Code: Select all
Options +FollowSymLinks

RewriteEngine On#

RewriteRule ^firstpage$  /firstpage.php  [QSA,L]
RewriteRule ^secondpage$ /secondpage.htm [QSA,L]

# Ignore existing files.
RewriteCond %{SCRIPT_FILENAME} !-f
Ignore existing directories.
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /otherpage.php [QSA,L]


Or you could use
Code: Select all
# Ignore internal sub requests.
RewriteCond %{ENV:REDIRECT_STATUS} ^$

or
Code: Select all
# Ignore specific files.
RewriteCond %{REQUEST_URI} !^(firstpage\.php|secondpage\.htm)$

instead of
Code: Select all
# Ignore existing files.
RewriteCond %{SCRIPT_FILENAME} !-f
Ignore existing directories.
RewriteCond %{SCRIPT_FILENAME} !-d
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 7 guests

cron