简体   繁体   中英

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/

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.

[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}:

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

From the documentation :

Syntax: RewriteRule Pattern Substitution [flags]

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).

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.

Because you're searching for code=1 and you pass 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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