.htaccess在网址中没有扩展名重定向

时间:2016-01-01 17:36:26

标签: .htaccess

我用

隐藏了我的文件扩展名
RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

当我导航到此链接时 http://localhost/website/profile?user=user01&nofollow=1

我在这里被重定向 http://example.com/folder/profile?user=user01

我已将.php从网址中删除。为什么要重定向我?

1 个答案:

答案 0 :(得分:1)

/website/.htaccess内以这种方式:

RewriteEngine On
RewriteBase /website/

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php[?\s] [NC]
RewriteRule ^(.+)\.php$ $1 [R=301,L,NC]

# Resolve .php file for extension-less php urls
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)$ $1.php [L]

确保在测试之前清除浏览器缓存。