Apache:mod_rewrite:Spcaes & URL 中的特殊字符不起作用

发布于 2024-10-03 03:37:59 字数 1775 浏览 6 评论 0原文

我正在使用 APACHE:mod_rewrite 定义一组重写 URL 的规则,

我想要此链接                             sp;               要显示为

/myDIR/walls.php? f=全部&of=0&s=最新 -> All.html

所以我使用以下规则

来自 (.htaccess) 的文本

RewriteEngine  on
RewriteBase    /myDIR/   
RewriteRule ^All\.html$  papers.php?f=All&of=0&s=Newest

现在这些变量被传递为 f=All of=0 s=Newest 这些显然在查询中使用,并且这些变量之一,即 f 有时会有带有空格和特殊字符的值,我无法避免这种情况,因为数据库已经就位,而且我正在重写 URL....

现在当我尝试定义这样的规则时

我想要此链接   nbsp;  nbsp; nbsp; nbsp; nbsp; nbsp; nbsp;    ;                         NBSP ;                         NBSP ;             显示为

/myDIR/walls.php?f=Characters & Supers&of=0&s=最新 -> 字符& Supers.html

我知道这是错误的,因为不应该有任何空格..所以为了使其正确,我定义了这样的规则,

RewriteRule ^Characters%20%26%20Supers\.html$  papers.php?f=Characters%20%26%20Supers&of=0&s=Newest

它允许我定义规则,但是当我单击我的链接时,我得到了这个 404 Not Found 错误“在此服务器上未找到请求的 URL /wallz/Characters & Supers.html。”

问题:该怎么办?

我的猜测是我不应该在 .htaccess 中进行 HTML URL 编码

i am using APACHE:mod_rewrite to define a set of rules for rewritting URLS

i want this link                                                           to be displayed as

/myDIR/walls.php?f=All&of=0&s=Newest -> All.html

so i am using the following rule

text from (.htaccess)

RewriteEngine  on
RewriteBase    /myDIR/   
RewriteRule ^All\.html$  papers.php?f=All&of=0&s=Newest

now these variables that are being passed as
f=All of=0 s=Newest these are being used in query, OBVIOUSLY, and one of these variables, i.e f sometimes has values with spaces and special characters, and i can't avoid that because the database is already in-place and all i am doing rewrite of URLs....

NOW when i try to define a rule like this

i want this link                                                                                          to be displayed as

/myDIR/walls.php?f=Characters & Supers&of=0&s=Newest -> Characters & Supers.html

which is wrong i know because there shouldn't be any spaces.. so to make it right i define the rule like this

RewriteRule ^Characters%20%26%20Supers\.html$  papers.php?f=Characters%20%26%20Supers&of=0&s=Newest

it lets me define the rule but when i click my link i get this
404 Not Found Error "The requested URL /wallz/Characters & Supers.html was not found on this server."

QUESTION: WHAT To Do ?

my guess is i am not supposed to be doing HTML URL Encoding inside .htaccess

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

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

发布评论

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

评论(1

离去的眼神 2024-10-10 03:37:59

你的猜测是正确的。当 URL 到达 mod_rewrite 进行处理时,Apache 已经为您解码了 URL。因此,如果您想检查任何其他编码的字符,则需要使用它们的文字表示形式。

由于此处使用空格作为分隔符,因此您还需要转义空格:

RewriteRule ^Characters\ &\ Supers\.html$ papers.php?f=%0&of=0&s=Newest

我相信使用引号也可以,因此以下内容应该是等效的(尽管未经测试):

RewriteRule "^Characters & Supers\.html$" papers.php?f=%0&of=0&s=Newest

关于 Apache 解码过程的最后一点是它会自动为您删除多个斜杠。虽然与您的示例无关,但它是在达到 mod_rewrite 规则之前如何转换 URL 的另一个示例。

Your guess is correct. By the time that a URL has reached mod_rewrite for processing, Apache has already decoded the URL for you. Therefore, if you want to check for any otherwise-encoded characters, you need to use their literal representations.

Since whitespace is used as a delimiter here, you'd also need to escape your spaces:

RewriteRule ^Characters\ &\ Supers\.html$ papers.php?f=%0&of=0&s=Newest

I believe that using quotation marks will also work, so the following should be equivalent (though it's untested):

RewriteRule "^Characters & Supers\.html$" papers.php?f=%0&of=0&s=Newest

A final note about Apache's decoding process is that it will automatically remove multiple slashes for you. While not relevant for your example, it's another example of how the URL is transformed prior to reaching your mod_rewrite rules.

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