【发布时间】:2019-04-17 12:15:06
【问题描述】:
早上好, 我在 Apache2 服务器上配置了一个虚拟主机。 mod_rewrite 模块处于活动状态且正常运行。
我在 https://regex101.com/ 网站上测试了正则表达式,它的行为符合我的预期。
我在 https://htaccess.madewithlove.be/ 网站上测试了 .htaccess 文件,它工作正常。
我不明白为什么,当我在 apache 服务器上尝试时它不起作用。
这是完整的 .htaccess 文件
.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(assets|locale)/(.*)$ user/$2/$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(/{0,1})user/([^/]+)(/?.*) user/$2.html [QSA,L]
虚拟主机配置
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
网站结构
mywebsite.local
user/
- .htaccess
- index.html
- app.html
测试网址
http://mywebsite.local/user/index/
http://mywebsite.local/user/app/
http://mywebsite.local/user/app/dashboard/
http://mywebsite.local/user/app/planningWeek/current/
http://mywebsite.local/user/app/task/taskId/day/month/year/
我的预期
http://mywebsite.local/user/index/
go to index.html (it works)
http://mywebsite.local/user/app/
go to app.html (it works)
http://mywebsite.local/user/app/dashboard/
it must go to app.html but I have this error
"The requested URL /user/app.html/dashboard was not found on this server."
我不明白为什么正则表达式是有效的并且来自指示站点的 .htaccess 文件有效,但是如果我将它加载到服务器号上。
谢谢
【问题讨论】:
-
您需要做的第一件事就是将
+MultiViews更改为-MultiViews(因为它以超出您想象的方式与您的请求混淆) -
接下来,您可能希望将
.htaccess移动到您网站的根文件夹 -
(其实既然你可以访问apache的.conf文件,you should not use .htaccess at all)
标签: apache .htaccess mod-rewrite