我在Debian中使用站点根目录中的.htaccess
运行Apache 2.2.22:file
Options +FollowSymlinks -Indexes -MultiViews
RewriteEngine on
RewriteBase /
#Redirect without www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#Allow access without .php extension
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
这是主要代码,我们还有一些<FilesMatch>
指令和一些mod_deflate
支票。
问题是,我们正在查看此网址:
www.example.com/abc
这只是站点根目录中的abc.php
文件,因此服务器会按预期重定向到example.com/abc
。
但是如果我访问(www.)example.com/abc/
,服务器只会抛出HTTP 404错误,这很明显,因为目录/abc/
不存在。
那些定义的选项不应该避免吗?这可能是什么问题?
答案 0 :(得分:1)
您需要先使用重定向规则删除尾部斜杠:
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [NE,R=301,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)$ $1.php [L]