我正试图美化我的URL,并希望摆脱.php文件扩展名。我在这里还发现了其他几个问题,例如这个(Removing the PHP file type extension),但是这些建议都对我没有用。他们将我发送到我的404.php或根本不更改URL。
我认为htaccess中的整个RewriteEngine代码本身都存在冲突。
这是我到目前为止得到的,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.php [R]
</IfModule>
我希望我的域看起来像
答案 0 :(得分:2)
请勿将Redirect
,RedirectMatch
和RewriteRule
伪指令混合在同一.htaccess中,因为它们来自不同的Apache模块,并且它们的加载顺序是不可预测的。
具有这种方式:
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
在测试此更改之前,请确保清除浏览器缓存。
答案 1 :(得分:1)
放入.htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
#1)externally redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
#2)Internally map "/file" back to "/file.php"
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,L]
ErrorDocument 404 ./404
RewriteCond %{REQUEST_URI} ^404/$
RewriteRule ^(.*)$ ./404 [L]
Redirect 301 /index /
答案 2 :(得分:0)
从链接中删除.php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]