2016-07-25 83 views
3

这是文件看起来什么我的.htaccess这样的:的.htaccess重定向到整齐URL

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

有,我可以添加到该文件要做到这一点的任何代码?

任何有识之士将是非常有益的

回答

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] 
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] 
+0

哇!这太棒了!谢谢! –

+0

还有我怎么重定向来自:http://example.com/?slug=hey-there 到:http://example.com/hey-there 请帮@ ansuman,肖汉 –

+0

有什么办法通过它我可以将此重定向仅限于根级别,例如:example.com/sing.php应该重定向到example.com/sing 但这不应该发生在example.com/data/sync.php .. 任何想法@anubhava 感谢您的时间! –