htaccess Rewrite规则在任何方式下都无法正常工作

huangapple go评论96阅读模式
英文:

htcaccess Rewrite Rule not working in any way

问题

我正在进行一个项目,并尝试在我的网站根目录进行URL重写。.htaccess 目前如下所示:

RewriteEngine On 
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L]

RewriteRule ^screenshot-of-the-week/screenshot/([^/]+) /screenshot-of-the-week/screenshot/index.php?id=$1

重写在我的网站上已启用。我通过 htaccess tester 检查了代码,它显示了正确的值。

然而,当我访问 screenshot-of-the-week/screenshot/123 作为示例时,我得到一个404页面。当我访问 screenshot-of-the-week/screenshot/ 时,它运行正常。

此外,手动访问“真实” URL 时,也按预期运行:screenshot-of-the-week/screenshot/index.php?id=123

我似乎无法找出问题出在哪里,并尝试将.htaccess 移动到其他文件夹,当然也编辑了路径,但都没有成功。

还有其他人可以提供的故障排除提示吗?我如何检查重定向实际上在后台执行了什么?

英文:

I am working on a project and I’m trying to do a URL rewrite in the root of my website. The .htaccess as it is now is as follows

RewriteEngine On 
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L]

RewriteRule ^screenshot-of-the-week/screenshot/([^/]+) /screenshot-of-the-week/screenshot/index.php?id=$1

Rewrite is enabled on my site. I have checked the code through htaccess tester and it shows the correct values.

Yet when I visit screenshot-of-the-week/screenshot/123 for an example, I get a 404 page. When I visit screenshot-of-the-week/screenshot/ it works just fine.

Also, when going to the "real" URL manually, it also works as expected: screenshot-of-the-week/screenshot/index.php?id=123

I can’t seem to figure out where the problem is and have tried moving the .htaccess to other folders and of course also editing the path, yet no success.

Any other troubleshooting tips anyone can give? How can I check what the redirect actually is doing in the background?

答案1

得分: 1

请注意,以下是已翻译的内容:

重新写规则 ^screenshot-of-the-week/screenshot/([^/]+) /screenshot-of-the-week/screenshot/index.php?id=$1

该规则的“问题”在于它也匹配了重写后的URL(具体来说,[^/]+匹配了index.php),因此被再次重写为/screenshot-of-the-week/screenshot/index.php?id=index.phpid=123参数被覆盖)。所以,我会期望_您的脚本_生成404响应(而不是Apache)?

要解决这个问题,您应该使正则表达式更具体,和/或使用END标志(Apache 2.4)以防止重写引擎进行额外循环(和第二次重写)。

在您的示例中,您传递了一个数字值 - 所以如果您只期望一个数字值,那么只匹配数字。

例如:

RewriteRule ^screenshot-of-the-week/screenshot/(\d+)$ /screenshot-of-the-week/screenshot/index.php?id=$1 [END]

请注意,我还在正则表达式中包括了结尾的$,否则您的网站可能会受到滥用,因为_任何东西_都可以附加到URL上,并且相同的资源将被提供。(在您的原始规则中附加/<anything>将导致相同的响应。)

(我假设您的.htaccess文件位于文档根目录。)


我已经通过htaccess测试器检查了代码,显示出了正确的值。

MWL测试器只对文件进行单次遍历。这不是实际Apache服务器的工作方式。因此,MWL测试器无法检测到重写/重定向循环。

当我访问screenshot-of-the-week/screenshot/时,它运行得很好。

当您访问screenshot-of-the-week/screenshot/时,规则不会被处理(正则表达式不匹配)。mod_dir提供了DirectoryIndex文档(index.php),但没有id参数。

英文:

> RewriteRule ^screenshot-of-the-week/screenshot/([^/]+) /screenshot-of-the-week/screenshot/index.php?id=$1

The "problem" with this rule is that is also matches the rewritten URL (specifically, [^/]+ matches index.php) so gets rewritten a second time to /screenshot-of-the-week/screenshot/index.php?id=index.php (the id=123 parameter is overwritten). So, I would expect your script to be generating the 404 response (not Apache)?

To resolve this, you should make the regex more specific and/or use the END flag (Apache 2.4) to prevent an additional loop (and second rewrite) by the rewrite engine.

In your example you are passing a numeric value - so if you are only expecting a numeric value then match digits only.

For example:

RewriteRule ^screenshot-of-the-week/screenshot/(\d+)$ /screenshot-of-the-week/screenshot/index.php?id=$1 [END]

Note that I also included the trailing $ on the regex, otherwise your site is open to abuse since anything could be appended to the URL and the same resource would be served. (In your original rule appending /&lt;anything&gt; would result in the same response.)

(I'm assuming your .htaccess file is located in the document root.)


> I have checked the code through htaccess tester and it shows the correct values.

The MWL tester only makes a single pass through the file. This is not how a real Apache server works. Consequently the MWL tester is unable to detect rewrite/redirect loops.

> When I visit screenshot-of-the-week/screenshot/ it works just fine.

When you visit screenshot-of-the-week/screenshot/ then the rule is not processed (the regex does not match). mod_dir serves the DirectoryIndex document (index.php), but without the id parameter.

huangapple
  • 本文由 发表于 2023年7月6日 22:15:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定