Reputation: 47
i have a code in my .htaccess file.. It redirect every .php to non php. I want it to only direct one php file and dont redirect the rest.. forexample i want abc.php to be abc but bcd.php stays as bcd.php.. How can i modify this script to get this result? thanks.
RewriteEngine on
#Redirect non-php to php and stop futher processing
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
#redirect .php to non-php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)\.php$ $1 [R=301,L]
Upvotes: 1
Views: 126
Reputation: 23719
Put the rule with exception and L flag before the general RewriteRule:
RewriteEngine on
#redirect abc.php to abc
RewriteRule ^abc\.php$ abc [R=301,L]
#Redirect non-php to php and stop futher processing
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} ! abc$
RewriteRule ^(.*)$ $1.php [L]
Upvotes: 0
Reputation: 784998
This code should work for you:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(abc)\.php[?\s] [NC]
RewriteRule ^ %1 [R=301,L]
#Redirect non-php to php and stop futher processing
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
Important to use %{THE_REQUEST}
here which represents original HTTP request as received by Apache to avoid looping. %{THE_REQUEST}
doesn't get rewritten with various rewrite rules as opposed to the case with URI pattern used for RewriteRule
.
Upvotes: 2