regex - 使用 RewriteCond %{QUERY_STRING} 而不将查询字符串附加到新地址

标签 regex .htaccess mod-rewrite

我尝试使用 .htaccess 进行 301 重定向。

问题:

/?view=products&id=12345 -> /8831

旧地址和新地址之间没有任何关系。

由于某种原因

Redirect 301 /?view=products&id=12345 /8831

不起作用。如果我删除问号,它就可以在没有问号的情况下工作。

我也尝试过:

RewriteCond %{QUERY_STRING} view=products&id=12345
RewriteRule .*$ /8831 [L,R=301]

但它会将我重定向到 /8831?view=products&id=12345,这对我来说不好。我不需要新网址中的查询字符串-

最佳答案

RewriteCond %{QUERY_STRING} view=products&id=12345 
RewriteRule .*$ /8831? [L,R=301]

结尾的 ? 将阻止附加原始查询参数,除非您再次给出 [QSA] 标志。

来自the manual :

Note: Query String The Pattern will not be matched against the query string. Instead, you must use a RewriteCond with the %{QUERY_STRING} variable. You can, however, create URLs in the substitution string, containing a query string part. Simply use a question mark inside the substitution string, to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine a new query string with an old one, use the [QSA] flag.

关于regex - 使用 RewriteCond %{QUERY_STRING} 而不将查询字符串附加到新地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15277182/

相关文章:

.htaccess - 如何写入 htaccess 文件

linux - .htaccess 文件在 apache 上不起作用

regex - 匹配备份文件 (`*~` )

php - Laravel 内页未加载 -- htaccess 问题

regex - 使用 grep 和 sed 时必须屏蔽哪些字符?

php - htaccess 中的自定义 404 错误处理程序不适用于不存在的 ".php"文件

.htaccess - 拒绝时重定向 |访问

php - 删除带有 mod_rewrite 和 SEO 问题的 .php 扩展名

PHP - 将 Youtube URL 转换为嵌入 URL

javascript - 如何从 html 字符串中获取 head 和 body 标签作为字符串?