这就是我的 .htaccess 文件:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
#removes trailing slash if not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]
#adds ".php" to a URL that isn't a directory or a file
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) $1.php [L]
虽然它允许我访问最终没有.php的网址,例如example.com/sign-up
它不会将我从example.com/sign-up.php
重定向到example.com/sign-up
是否有任何代码可以添加到此文件中以实现此目的?
任何见解都会非常有用
答案 0 :(得分:2)
试试这个:
RewriteEngine On
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
答案 1 :(得分:1)
我无法在评论中回答这个问题,所以将此作为答案发布:
RewriteEngine On
# remove PHP from root level requests
RewriteCond %{THE_REQUEST} \.php[\s?] [NC]
RewriteRule ^([^/]+)\.php$ /$1 [R=302,NE,L,NC]
# To internally redirect /foo to /foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]