简体   繁体   English

如果使用GET参数则重写URL

[英]URL Rewrite if GET parameter

I want to redirect/RewriteRule a Url when there is a special parameter in it for example /post/path/?code=1 redirect to /post/path/ 当其中有一个特殊参数时,例如/ post / path /?code = 1重定向到/ post / path /,我想将URL重定向/ RewriteRule。

I dont know, but this here dont work 我不知道,但这在这里不起作用

RewriteEngine on
RewriteBase / 
RewriteRule ^.*code=1.*$ /post/path/ [L]

if I run http://domain.com/post/path/?code=4/oWTYGaoOsyts there will be nothing change. 如果我运行http://domain.com/post/path/?code=4/oWTYGaoOsyts,则不会有任何变化。

[REQUEST_URI] => /post/path/?code=1/oWTYGaoOsyts
[SCRIPT_NAME] => /index.php
[PHP_SELF] => /index.php

For searching a parameter in the URL, you need to use RewriteCond and %{QUERY_STRING}: 要在URL中搜索参数,您需要使用RewriteCond和%{QUERY_STRING}:

RewriteCond %{QUERY_STRING} ^.*code=1.*$
RewriteRule .* /post/path/ [L]

From the documentation : 文档中

Syntax: RewriteRule Pattern Substitution [flags] 语法:RewriteRule模式替换[标志]

In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that led the server to the current RewriteRule (eg "app1/index.html" or "index.html" depending on where the directives are defined). 在Directory和htaccess上下文中,在删除导致服务器到达当前RewriteRule的前缀(例如,“ app1 / index.html”或“ index.html”,具体取决于指令所在的位置)之后,Pattern将首先与文件系统路径进行匹配。定义)。

If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively. 如果希望与主机名,端口或查询字符串匹配,请分别使用RewriteCond和%{HTTP_HOST},%{SERVER_PORT}或%{QUERY_STRING}变量。

Because you're searching for code=1 and you pass code=4 因为您正在搜索code=1并且您通过code=4

This should work with given input 这应该在给定的输入下工作

RewriteRule ^.*code=4.*$ /post/path/ [L]

Note that it can match unintended urls like code=40 or somecode=4 请注意,它可以匹配意外的网址,例如code=40somecode=4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM