PHP mod 重写无法正常工作

发布于 2025-01-01 16:49:33 字数 1459 浏览 1 评论 0原文

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

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

发布评论

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

评论(2

苏辞 2025-01-08 16:49:33

这条规则

RewriteCond %{REQUEST_URI} /([0-9a-zA-Z]+)$ [NC]

将匹配以 / 结尾的字符串,后跟字母数字字符串,因此它将匹配 gallery/hgJ56,因为它以 /hgJF6 结尾。

如果规则仅匹配以 / 开头,后跟字母数字字符串的字符串,那么您可以将此规则更改为

RewriteCond %{REQUEST_URI} ^/([0-9a-zA-Z]+)$ [NC]

,它将不再匹配 gallery/hgJ56 并且您的其他规则将起作用。

请注意,您不需要转义 / 字符,即 \// 是等效的。

您可能还想将 ^ 应用于其余规则,以防它们也只应该匹配字符串的开头,即更改

RewriteCond %{REQUEST_URI} \/gallery/([0-9a-zA-Z]+)$ [NC]

RewriteCond %{REQUEST_URI} ^/gallery/([0-9a-zA-Z]+)$ [NC]

This rule

RewriteCond %{REQUEST_URI} /([0-9a-zA-Z]+)$ [NC]

will match a string that ends with a / followed by an alphanumeric string, so it will match gallery/hgJ56, because it ends with /hgJF6.

If the rule is only supposed to match a string that begins with a / followed by an alphanumeric string, then you can change this rule to

RewriteCond %{REQUEST_URI} ^/([0-9a-zA-Z]+)$ [NC]

and it will not longer match gallery/hgJ56 and your other rule will work.

Note that you do not need to escape the / character i.e. \/ and just / are equivalent.

You may also want to apply the ^ to the rest of your rules in case they also are only supposed to match the start of the string i.e change

RewriteCond %{REQUEST_URI} \/gallery/([0-9a-zA-Z]+)$ [NC]

to

RewriteCond %{REQUEST_URI} ^/gallery/([0-9a-zA-Z]+)$ [NC]
最佳男配角 2025-01-08 16:49:33

这是您的重写规则的“更干净”版本。

RewriteRule ^/([0-9a-zA-Z]+)$ image.php?names[]=$1 [QSA,NC,L]
RewriteRule ^/([0-9a-zA-Z]+),([0-9a-zA-Z]+)$ image.php?names[]=$1&names[]=$2 [QSA,NC,L]
RewriteRule ^/([0-9a-zA-Z]+),([0-9a-zA-Z]+),([0-9a-zA-Z]+)$ image.php?names[]=$1&names[]=$2&names[]=$3 [QSA,NC,L]
RewriteRule ^/gallery/([0-9a-zA-Z]+)$ gallery.php?id[]=$1 [QSA,NC,L]

告诉我它们是否有效。

如果这还不够:

两个提示:

如果您不在托管环境中(= 如果它是您自己的服务器并且可以.htaccess文件),尝试使用RewriteLog指令:它可以帮助您追踪此类问题:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

我的最喜欢的检查正则表达式的工具:

http://www.quanetic.com/Regex (不要忘记选择 ereg(POSIX) 而不是 preg (PCRE)!)

Here's a "cleaner" version of your rewriterules.

RewriteRule ^/([0-9a-zA-Z]+)$ image.php?names[]=$1 [QSA,NC,L]
RewriteRule ^/([0-9a-zA-Z]+),([0-9a-zA-Z]+)$ image.php?names[]=$1&names[]=$2 [QSA,NC,L]
RewriteRule ^/([0-9a-zA-Z]+),([0-9a-zA-Z]+),([0-9a-zA-Z]+)$ image.php?names[]=$1&names[]=$2&names[]=$3 [QSA,NC,L]
RewriteRule ^/gallery/([0-9a-zA-Z]+)$ gallery.php?id[]=$1 [QSA,NC,L]

Tell me if they work.

And if that's not enough:

Two hints:

If you're not in a hosted environment (= if it's your own server and you can modify the virtual hosts, not only the .htaccess files), try to use the RewriteLog directive: it helps you to track down such problems:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

My favorite tool to check for regexp:

http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文