我想删除所有客户端请求查询字符串,没有任何异常。我已经尝试了我能找到的一切,以及我所知道的关于正则表达式的一切,但这项任务让我感到困惑。我已经能够删除查询字符串,但现在所有请求都在重写和重定向时将完整的文件路径预先设置为工作目录。
示例:其中没有http,因为stackoverflow不允许我发布URL。
我访问文件:localhost/testing/dogs/ppuss.txt是的,pups.txt就存在并生活在那里。
服务器将其返回到浏览器:localhost/home/user/public_html/testing/dogs/pus.txt
如果我访问它时附加了一个查询字符串:/localhost/testing/dogs/pus.txt?树皮=发出的呜呜声
我在浏览器中得到了相同的输出:/localhost/home/gost/public_html/testing/dogs/pus.txt
所以我知道查询字符串被否决了,而整个根路径被添加到超文本地址中。
我该如何告诉mod_rewrite保留现有文件的相对路径,以便停止对完整文件路径的预处理,并准确地使其在内部和外部重写,从而使任何查询字符串都无法到达php?
这是当前的.htaccess文件。它位于目录/home/user/public_html/testing中。在网上部署的地方,不可能将其放在根web目录中,这一明显的举措可以立即解决这个问题。
# These are commented out on purpose because they kill access to the server.
# The weird rules for regex in Apache configurations is baffling me. This
# does remove all QUERY_STRING characters, but, it then rewrites by
# prepending the web root path to the current directory, causing error 404.
# RewriteCond %{QUERY_STRING} ^
# RewriteRule (.*) $1? [R=301,L]
# These rules work fine. Whatever does not end with a media or document
# extension gets sent to index.php
RewriteRule ^.*'.(gif|jpe?g|png|txt|svg|pdf|rtf|odt|doc|docx)$ - [L]
RewriteRule ^.*'.(tex|epub|mobi|csv|ods|xls|swf|flv)$ - [L]
RewriteRule ^ index.php [L]
将此RewriteCond %{QUERY_STRING} ^
更改为此RewriteCond %{QUERY_STRING} .
并将目录添加到规则中,因为您不能使用rewritebase。所以它应该看起来像这个
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) /testing/$1? [R=301,L]
我两天都无法接受自己的答案,所以如果有人想为未来的提问者添加关于mod_rewrite的逻辑,请尝试一下。根据Panama Jack的建议,这个.htaccess文件可以完成任务,这个问题就得到了回答。
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) /testing/$1? [R=301,L]
RewriteRule ^.*'.(gif|jpe?g|png|txt|svg|pdf|rtf|odt|doc|docx)$ - [L]
RewriteRule ^.*'.(tex|epub|mobi|csv|ods|xls|mm|)$ - [L]
RewriteRule ^ test.php [L]