Michal Rybar
Michal Rybar

Reputation: 21

.htaccess replace url parameter "name"

I have a URL that will be something like

http://example.com/shake/pokladna/?id=35601-JILOSRO&name=Jilo%20s.r.o.&address=Hrušková%202194&city=Sokolov&zipcode=35601

I want to redirect that address to these (I need to replace "name" with "name_gls")

http://example.com/shake/pokladna/?id=35601-JILOSRO&name_gls=Jilo%20s.r.o.&address=Hrušková%202194&city=Sokolov&zipcode=35601

Is this code ok?

RewriteCond %{QUERY_STRING}  (.*)&?name=(.*)? [NC]
RewriteRule ^pokladna/?$  $0?%1name_gls=%2 [R=301,L]

Upvotes: 1

Views: 730

Answers (1)

MrWhite
MrWhite

Reputation: 45829

RewriteCond %{QUERY_STRING}  (.*)&?name=(.*)? [NC]
RewriteRule ^pokladna/?$  $0?%1name_gls=%2 [R=301,L]

This is close, but not quite correct. As stated, these directives are in the WordPress .htaccess file located at /shake/.htaccess. These directives need to go before any existing WP directives, if not already.

The RewriteRule substitution requires a slash prefix to make a valid external redirect in a per-directory .htaccess file. As it stands, the $0 backreference excludes the slash prefix, so results in a relative path substitution - in which case the directory-prefix is added back, resulting in an invalid redirect.

Another potential problem is the optional & in the CondPattern, ie. the &? in (.*)&?name=(.*)?. On the given example URL, this will work OK, however, since the preceding (.*) is greedy, this will also match any query string parameter name that just ends in "name". For example, a query string of the form id=123&anothername=foo would be changed to id=123&anothername_gls=foo - which I doubt is desirable. A way around this is to use alternation, eg. (^|.+&) to match either the start of the query string or a preceding URL param (and param delimiter).

I would also question the use of the NC flag on the RewriteCond directive - should this really be a case-insensitive match?

And you've made the trailing slash on the URL-path optional. Is this really optional? If it is then you should instead make sure this is canonicalised in the substitution. In the code below, I assume the trailing slash is mandatory, since it is present in your example.

With your specific example you will also need the NE (noescape) flag to prevent the %-encoded spaces (ie. %20) in the query string being doubly encoded.

So, bringing these points together, try something like the following instead:

RewriteCond %{QUERY_STRING} (^|.+&)name=(.*)
RewriteRule ^pokladna/$  /$0?%1name_gls=%2 [NE,R,L]

Note that this is a temporary (302) redirect. Change the R to R=301 only when you are sure it's working OK. Permanent redirects can make testing problematic, as they are cached hard by the browser.

Consequently, you will need to make sure the browser cache is cleared before testing.

And, as mentioned above, these must go before the existing WordPress directives (specifically, before the front controller).

Upvotes: 1

Related Questions