繁体   English   中英

.htaccess重定向到整洁的URL

[英].htaccess redirection to tidy URL

这是我的.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的url,例如example.com/sign-up但它没有将我从example.com/sign-up.php重定向到example.com/sign-up

是否有任何代码可以添加到此文件中以实现此目的?

任何见解都会非常有帮助

试试这个 :

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]

我无法在评论中回答这个问题,所以将此作为答案发布:

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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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