[英]Automatic URL Rewriting not working
我想隐藏.php扩展名,所以我在.htaccess文件中编写了以下代码,该文件在此链接中找到。 如何从网站地址中删除文件扩展名?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
当我键入localhost / testsite / index时,此方法工作正常,它可以正确显示localhost / testsite / index.php并根据需要在地址栏中显示localhost / testsite / index。转换为localhost / testsite / index。 我想删除扩展名,即使用户在页面名称后键入.php时也是如此。
让您的.htaccess像这样:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1%2 [R=302,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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.