URL中尾随%20空格在Codeigniter中产生404错误 [英] Trailing %20 white space in URLs producing 404 errors in Codeigniter

查看:79
本文介绍了URL中尾随%20空格在Codeigniter中产生404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的带有URL编码尾部空格(%20 )的URL产生404错误.该应用程序在Apache的Codeigniter上运行.

Our URLs with a URL encoded trailing white space (%20) are producing a 404 error. The application is run on Codeigniter on Apache.

  • /directory/page%20 将返回404错误

/目录/页面将返回200 OK

/directory/page will return a 200 OK

如何将所有带有尾随%20 的URL路由到目标URL?

How can I route all URLs with a trailing %20 to the intended URL?

推荐答案

问题在于某些第三方网站通过HREF中的尾随空白与我们链接

The problem is that some third party websites are linking to us with trailing white space in the HREF

在这种情况下,您可以将 .htaccess 文件顶部的以下内容添加到 redirect (规范化)这样的请求中,以删除尾随空格.

In that case you can add something like the following at the top of your .htaccess file to redirect (canonicalise) such requests to remove the trailing space.

例如,在Codeigniter前控制器的之前:

For example, before the Codeigniter front-controller:

RewriteCond %{REQUEST_URI} \s$
RewriteRule (.*) /$1 [R=302,L]

RewriteRule pattern 匹配的已处理" URL路径已经删除了尾部斜杠,但是 REQUEST_URI 服务器变量还没有.因此,我们可以检查 REQUEST_URI 上的尾随空格,并直接重定向到相同"(已处理)URL路径,如 RewriteRule pattern所捕获的那样.

The "processed" URL-path matched by the RewriteRule pattern has already had the trailing slash removed, however, the REQUEST_URI server variable has not. So, we can check for the trailing space on the REQUEST_URI and simply redirect to "the same" (processed) URL-path, as captured by the RewriteRule pattern.

REQUEST_URI 服务器变量已被%解码. \ s 速记字符类与任何空格字符匹配,并且结尾的 $ 将其锚定在URL路径的末尾.

The REQUEST_URI server variable is already %-decoded. The \s shorthand character class matches against any whitespace character and the trailing $ anchors this to the end of the URL-path.

首先使用302(临时)重定向进行测试,以确保它可以正常工作,然后再更改为301(永久)重定向.

Test first with a 302 (temporary) redirect to make sure that it works OK before changing to a 301 (permanent) redirect.

这篇关于URL中尾随%20空格在Codeigniter中产生404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆