我的htaccess文件有问题,我试图隐藏我网站上的所有.php扩展名,
我尝试了很多htaccess代码,但大部分都给了我错误,404 Not Found,我的大多数文件都包含变量,如?id和?category,index /?cat = Action,我希望htaccess隐藏php扩展名和forward / dir / foo到/dir/foo.php,请帮助我:)
RewriteEngine On
RewriteBase /
# turn off index.php for home page
RewriteRule ^index\.php/?$ / [L,R=301,NC]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
答案 0 :(得分:1)
你需要将php写到URL的末尾,而不是相反。您可以编写如下变量:href="foo?bar=1"
。这段代码对我有用:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
答案 1 :(得分:0)
您可以在根.htaccess
中使用此代码:
RewriteEngine On
RewriteBase /
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]