简体   繁体   中英

Apache mod_rewrite not redirecting to PHP, but redirecting to other URLs

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]

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 .

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM