.htaccess 隐藏页面扩展
.htaccess hide page extension
每当我尝试使用 .htacess
隐藏 .php
扩展名并转到使用 .php
扩展名的页面时,屏幕上显示的都是 "Page Not Found"。我以前运行得很好,但我最近更换了托管公司,现在我使用 Plesk Onyx 17.0.17
和 CentOS Linux 7.3.1611
和 PHP 7.1.4
。
这是我的 .htaccess
文件的内容:
Options -Multiviews -Indexes
# Disable Automatic Directory detection
DirectorySlash Off
ErrorDocument 404 /!404.php
RewriteEngine On
RewriteBase /
# Remove trailing slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/+$ [R=301,L]
RewriteRule (.*?)index\.php/*(.*) / [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ .php
感谢 Anubhava's answer on this question,我现在可以使用它了。我仍然不知道为什么我的旧不能工作。
这是我用来解决问题的代码:
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
每当我尝试使用 .htacess
隐藏 .php
扩展名并转到使用 .php
扩展名的页面时,屏幕上显示的都是 "Page Not Found"。我以前运行得很好,但我最近更换了托管公司,现在我使用 Plesk Onyx 17.0.17
和 CentOS Linux 7.3.1611
和 PHP 7.1.4
。
这是我的 .htaccess
文件的内容:
Options -Multiviews -Indexes
# Disable Automatic Directory detection
DirectorySlash Off
ErrorDocument 404 /!404.php
RewriteEngine On
RewriteBase /
# Remove trailing slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/+$ [R=301,L]
RewriteRule (.*?)index\.php/*(.*) / [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ .php
感谢 Anubhava's answer on this question,我现在可以使用它了。我仍然不知道为什么我的旧不能工作。
这是我用来解决问题的代码:
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]