htaccess重写引起侧面影响
我的.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.com
和https://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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个规则将捕获请求(因为它允许一个空的URL路径),并将请求重写为
/member/profile.php?user =
。因此,大概是您的脚本触发了404?实际上,看起来您在
之前缺少斜线?
匹配可选的tailting slash(即/username
或/username/username/
) ,而不是使整个模式可选! IE。^([^/]+)/?$
您也需要
ns
(nosubreq
)标志,以防止mod_dir subrequest for the subrequestDirectoryIndex
(即index.php
)也被此规则捕获。但是,可以说该规则匹配太多,因为它还将收集index.php
的直接请求(以及您可能存在的任何其他文件)。因此,也许您需要在用户名中允许哪些字符中更加限制?例如,至少将^([^/。]+)/?$
排除点(以及斜线)?或仅允许字母和数字(以及下划线),例如。^(\ W+)/?$
。 (\ w
是代表[0-9A-ZA-Z _]
的速记字符类。)请注意,第一个规则也将匹配
Assets
,<代码>图像和php
- 因此这些都是有效的用户名。那是故意的吗?您可以扭转规则,以免发生这种情况,但是您需要确保没有匹配这些字符串的用户名。nb:
https://example.com
和https://example.com/
是完全相同的请求。 (浏览器在主机名之后有效地附加了斜线,以提出有效的http请求。请参阅网络管理员堆栈上的以下问题:在浏览器中单击“主页” URL时,tailling slash of automaging of the slash?)我看不出发布的指令将如何发生。您的规则都不匹配
/username/
(带有落后斜线),除非用户名是“资产”,“ images”或“ php” - 但这仍然是't导致已陈述的重写?但是,/username/
将导致404,因为实际上没有什么可以重写URL!您的规则也许应该像这样写:
规则2、3和4中的捕获子图案不是可选的,因此我删除了Tailting
?$
。我还删除了替换字符串上的斜杠前缀,以使其成为相对文件路径。
也可以进一步“简化”到:
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 theDirectoryIndex
(ie.index.php
) also being caught by this rule. However, this rule is arguably matching too much, as it will also catch direct requests forindex.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
andphp
- 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
andhttps://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?)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:
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: