$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>url rewriting - Mod_rewrite delete parameter in 301 Redirect|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

url rewriting - Mod_rewrite delete parameter in 301 Redirect

matteradmin8PV0评论

How would I go about rewriting:

/?aff=number

to:


I want to remove all instances on aff=x regardless of how many other parameters in the URL.

How would I go about rewriting:

http://www.example/?aff=number

to:

http://www.example

I want to remove all instances on aff=x regardless of how many other parameters in the URL.

Share Improve this question edited Aug 24, 2017 at 13:43 MrWhite 3,8911 gold badge20 silver badges23 bronze badges asked Aug 24, 2017 at 13:07 DaimkoDaimko 431 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

I want to remove all instances on aff=x regardless of how many other parameters in the URL.

Assuming you want to keep any other URL parameters (in any order) and simply remove the aff=number parameter, then you can do something like the following using mod_rewrite in the root .htaccess file:

RewriteCond %{QUERY_STRING} ^(.*&|)aff=\d+(?:&(.*)|)$
RewriteRule (.*) /$1?%1%2 [R=302,L]

This will need to go before the existing WordPress directives.

This assumes that if the aff parameter is present it will have a value of only digits and at least 1 digit.

The $1 is a backreference to the captured RewriteRule pattern. So, this will handle any URL-path, not just the document root, as in your example.

%1 and %2 are backreferences to the captured groups in the previous CondPattern. %1 is before the parameter to be removed and %2 is after. Both are optional.

Change the 302 (temporary) redirect to 301 (permanent) only after you have confirmed this is working OK (if this is intended to be permanent).


UPDATE#1: Query string contains %-encoded characters

If any of the other URL parameters (that you want to keep) contain URL-encoded (%-encoded) values then you will need to add the NE (noescape) flag to the RewriteRule directive to prevent these characters from being doubly encoded in the substitution. For example:

RewriteRule (.*) /$1?%1%2 [NE,R=302,L]

The QUERY_STRING server variable is not %-decoded by Apache. Normally, mod_rewrite URL-encodes the substitution before sending the response back to the client. But this will doubly encode any already-encoded characters that we have captured from the query string. The NE flag prevents this from happening.


UPDATE#2: URL parameter value to be removed could be anything

The question specifically dealt with removing a URL parameter called aff that contained a "number". Generally, you should be as specific as possible when dealing with regex, however, if the URL parameter to be removed can contain anything then modify the CondPattern to be something like one of the following

  • ^(.*&|)aff=.*?(?:&(.*)|)$ - a non-greedy match of 0 or more characters.
  • ^(.*&|)aff=[^&]*(?:&(.*)|)$ - matches anything except & (URL parameter delimiter) 0 or more times.

The important point in both of these regex is that the value being matched is not captured, ie. must not be surrounded in plain parentheses.

In summary:

RewriteCond %{QUERY_STRING} ^(.*&|)aff=[^&]*(?:&(.*)|)$
RewriteRule (.*) /$1?%1%2 [NE,R=302,L]
Post a comment

comment list (0)

  1. No comments so far