【问题标题】:Why RewriteRule doesn't work as I expected, according the regular expression?根据正则表达式,为什么 RewriteRule 不能按我的预期工作?
【发布时间】: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


【解决方案1】:

在您的示例中,mod_rewrite 似乎只是将app 替换为app.html,从而从/user/app/dashboard 中生成/user/app.html/dashboard

如果您的.htaccess 文件在您测试过的网站中有效,是否有其他组件干扰请求?

也许重写日志可以引导您找到答案?如需更多信息,请参阅this Stackoverflow answer

希望对您有所帮助,问候。

【讨论】:

    猜你喜欢
    • 2017-09-04
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-06
    • 1970-01-01
    • 2014-04-28
    相关资源
    最近更新 更多