如何进行内部和外部 .htaccess 重写以完全删除所有 URI 中的所有 QUERY_STRING 元素,并使用相对路径

How to do both internal and external .htacces rewrite to completely remove ALL QUERY_STRING elements from ALL URIs, with relative path

我想删除所有客户端请求查询字符串,无一例外。 我已经尝试了我能找到的一切,以及我所知道的关于正则表达式的一切,但这个任务让我感到困惑。我已经能够删除查询字符串,但现在所有请求都在重写和重定向时将完整文件路径添加到工作目录。

示例:这些中没有 http,因为 Whosebug 不允许我 post URL。

我访问文件:/localhost/testing/dogs/pups.txt 是的,pups.txt 存在并生活在那里。

服务器 returns 这个到浏览器:/localhost/home/user/public_html/testing/dogs/pups.txt

如果我使用附加的查询字符串访问它: /localhost/testing/dogs/pups.txt?bark=woof

我在浏览器上得到了相同的输出: /localhost/home/gost/public_html/testing/dogs/pups.txt

所以我知道查询字符串被修改了,而完整的根路径被添加到超文本地址。

如何告诉 mod_rewrite 保留现有文件的相对路径,以便停止完整文件路径的前置,并准确地使其在内部和外部重写,以便永远不会有查询字符串到达 php-land?

这是当前的 .htaccess 文件。它位于目录 /home/user/public_html/testing 中。在线部署的地方,不可能把它放在根目录下,显而易见的举动会立即解决这个问题。

# 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  (.*) ? [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]

我不能接受我自己两天的回答,所以如果有人想为以后的提问者添加关于 mod_rewrite 的逻辑,请尝试一下。根据巴拿马杰克的建议,这个 .htaccess 文件可以完成这项工作,并且可以回答这个问题。

RewriteEngine On

RewriteCond %{QUERY_STRING} .
RewriteRule (.*) /testing/? [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]

将此 RewriteCond %{QUERY_STRING} ^ 更改为此 RewriteCond %{QUERY_STRING} . 并将目录添加到规则中,因为您不能使用 rewritebase。所以它应该是这样的

RewriteCond %{QUERY_STRING} .
RewriteRule (.*) /testing/? [R=301,L]