简体   繁体   中英

Slash after file extension can not be redirected

I am using these htaccess code to remove and redirect php extension to without file extension.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

Then I try to use another slightly different code

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.[a-zA-Z0-9]+$ $1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Both code produce same problem, if try to access

  1. domain.com/other/content.php goes to domain.com/other/content (200 response)

Now here is strange behaviour, if try to access

  1. domain.com/other/content.php/ goes to domain.com/other/content.php/ (200 response not 404)

  2. domain.com/other/content.php/any-word goes to domain.com/other/content.php/any-word (200 response not 404 error)

All other redirect work perfectly, besides this problem. Can any one help with both code, I need both code rectification, as I have two different site with both htaccess rule.

If I keep both code

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

Plus, I add new code then it works

RewriteEngine On
RewriteCond %{THE_REQUEST} /([^.]+)\.php/? [NC] 
RewriteRule ^ /%1/ [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /$1.php [L,NC]

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