htaccess重写引起侧面影响

发布于 2025-01-23 20:57:27 字数 1179 浏览 3 评论 0原文

我的.htaccess文件中有以下内容。

RewriteEngine On
RewriteRule ^([^/]+)?$ /member/profile.php?user=$1 [L]
RewriteRule  ^assets(/.*)?$  /member/assets$1 [L]
RewriteRule  ^images(/.*)?$  /member/images$1 [L]
RewriteRule  ^php(/.*)?$  /member/php$1 [L]

所需的效果是:

https://example.com/username -> https://example.com/member/profile.php?user=$1

这项工作是,问题是从中发生了2种不希望的结果。

首先: https://example.comhttps://example.com/返回404错误,但https:// example.com/index.php正常工作。

第二: https://example.com/username/最终转发到https:// example/example/example/php/?user = username并返回404错误。

我也尝试过尝试

DirectoryIndex index.htm index.html index.php

,但这似乎对

我的实际期望最终结果看起来更像是:

https://example.com -> https://example.com/index.php
https://example.com/ -> https://example.com/index.php
https://example.com/username -> https://example.com/member/profile.php?user=$1
https://example.com/username/ -> https://example.com/member/profile.php?user=$1

I have the following in my .htaccess file.

RewriteEngine On
RewriteRule ^([^/]+)?$ /member/profile.php?user=$1 [L]
RewriteRule  ^assets(/.*)?$  /member/assets$1 [L]
RewriteRule  ^images(/.*)?$  /member/images$1 [L]
RewriteRule  ^php(/.*)?$  /member/php$1 [L]

The desired effect is:

https://example.com/username -> https://example.com/member/profile.php?user=$1

This works, however, the issue is there are 2 undesired outcomes happening from this.

First: https://example.com and https://example.com/ return 404 errors but https://example.com/index.php works just fine.

Second: https://example.com/username/ ends up forwarding to https://example/member/php/?user=username and returning a 404 error.

I have also attempted

DirectoryIndex index.htm index.html index.php

But this seems to have no effect on the issue

My actual desired end result would look more like:

https://example.com -> https://example.com/index.php
https://example.com/ -> https://example.com/index.php
https://example.com/username -> https://example.com/member/profile.php?user=$1
https://example.com/username/ -> https://example.com/member/profile.php?user=$1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜中书 2025-01-30 20:57:27
 重新写入 ^([ ^/]+)?$/MEMBER/profile.php?user = $ 1 [l]
 

首先: https://example.comhttps://example.com/返回404错误,但https ://domain.name/index.php正常工作。

第一个规则将捕获请求(因为它允许一个空的URL路径),并将请求重写为/member/profile.php?user =。因此,大概是您的脚本触发了404?

实际上,看起来您在之前缺少斜线?匹配可选的tailting slash(即/username/username/username/) ,而不是使整个模式可选! IE。 ^([^/]+)/?$

您也需要nsnosubreq)标志,以防止mod_dir subrequest for the subrequest DirectoryIndex(即index.php)也被此规则捕获。但是,可以说该规则匹配太多,因为它还将收集index.php的直接请求(以及您可能存在的任何其他文件)。因此,也许您需要在用户名中允许哪些字符中更加限制?例如,至少将^([^/。]+)/?$排除点(以及斜线)?或仅允许字母和数字(以及下划线),例如。 ^(\ W+)/?$。 (\ w是代表[0-9A-ZA-Z _]的速记字符类。)

请注意,第一个规则也将匹配Assets,<代码>图像和php - 因此这些都是有效的用户名。那是故意的吗?您可以扭转规则,以免发生这种情况,但是您需要确保没有匹配这些字符串的用户名。

nb:https://example.comhttps://example.com/是完全相同的请求。 (浏览器在主机名之后有效地附加了斜线,以提出有效的http请求。请参阅网络管理员堆栈上的以下问题:在浏览器中单击“主页” URL时,tailling slash of automaging of the slash?

第二: https://example.com/username/最终转发到https://example.com/member/member/php/ =用户名并返回404错误。

我看不出发布的指令将如何发生。您的规则都不匹配/username/(带有落后斜线),除非用户名是“资产”,“ images”或“ php” - 但这仍然是't导致已陈述的重写?但是,/username/将导致404,因为实际上没有什么可以重写URL!

您的规则也许应该像这样写:

RewriteEngine On
RewriteRule ^(\w+)/?$ member/profile.php?user=$1 [L]
RewriteRule ^assets(/.*) member/assets$1 [L]
RewriteRule ^images(/.*) member/images$1 [L]
RewriteRule ^php(/.*) member/php$1 [L]

规则2、3和4中的捕获子图案不是可选的,因此我删除了Tailting ?$

我还删除了替换字符串上的斜杠前缀,以使其成为相对文件路径。

也可以进一步“简化”到:

RewriteEngine On
RewriteBase /member
RewriteRule ^(\w+)/?$ profile.php?user=$1 [L]
RewriteRule ^((assets|images|php)/.*) $1 [L]
RewriteRule ^([^/]+)?$ /member/profile.php?user=$1 [L]

First: https://example.com and https://example.com/ return 404 errors but https://domain.name/index.php works just fine.

The first rule will catch the request (since it allows an empty URL-path) and will rewrite the request to /member/profile.php?user=. So, presumably it is your script that is triggering the 404?

In fact, it looks like you are missing a slash before ? to match an optional trailing slash (ie. /username or /username/), rather than making the entire pattern optional! ie. ^([^/]+)/?$

You would also need the NS (nosubreq) flag to prevent the subrequest by mod_dir for the DirectoryIndex (ie. index.php) also being caught by this rule. However, this rule is arguably matching too much, as it will also catch direct requests for index.php (and any other files you might have in the root). So, maybe you need to be more restrictive in what characters are allowed in usernames? For example, at a minimum, exclude dots (as well as slashes) with ^([^/.]+)/?$? Or allow only letters and numbers (and underscores), eg. ^(\w+)/?$. (\w is a shorthand character class that represents [0-9a-zA-Z_].)

Note that the first rule will also match assets, images and php - so these are valid usernames. Is that intentional? You could reverse the rules so this does not happen, but you would need to ensure that there are no usernames that match these strings.

NB: https://example.com and https://example.com/ are exactly the same request. (The browser effectively appends the slash after the hostname to make a valid HTTP request. See the following question on the Webmasters stack: Is trailing slash automagically added on click of home page URL in browser?)

Second: https://example.com/username/ ends up forwarding to https://example.com/member/php/?user=username and returning a 404 error.

I can't see how that would happen with the directives as posted. None of your rules would match /username/ (with a trailing slash), unless the username is "assets", "images" or "php" - but that still wouldn't result in the stated rewrite? However, /username/ would result in a 404 because nothing actually happens to rewrite the URL!

Your rules should perhaps be written like this instead:

RewriteEngine On
RewriteRule ^(\w+)/?$ member/profile.php?user=$1 [L]
RewriteRule ^assets(/.*) member/assets$1 [L]
RewriteRule ^images(/.*) member/images$1 [L]
RewriteRule ^php(/.*) member/php$1 [L]

The capturing subpattern in rules 2, 3 and 4 is not optional, so I've removed the trailing ?$.

I've also removed the slash prefix on the substitution string, to make it a relative file-path.

Which could also be further "simplified" to:

RewriteEngine On
RewriteBase /member
RewriteRule ^(\w+)/?$ profile.php?user=$1 [L]
RewriteRule ^((assets|images|php)/.*) $1 [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文