1

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]
6
  • Where are you putting that code? Do the URL parameters vary? Is name always an internal URL parameter? Or could it also appear at the start of the query string? Commented Jul 27, 2017 at 20:08
  • I putting that code to .htaccess. Yes, URL parameters are vary. Third party API give me back these parameters. I am not sure if it will not to be changed in the future. Commented Jul 28, 2017 at 10:26
  • But where is the .htaccess file? I would have assumed it was the .htaccess file in the document root, however, your code suggests it is in the /shake subdirectory? This might be intentional, however, it could also be an error? Commented Jul 28, 2017 at 10:47
  • WordPress root directory Commented Jul 28, 2017 at 14:52
  • 1
    In example.com/shake Commented Jul 28, 2017 at 15:51

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

How can I replace multiple paremeters?
@EscapeNetscape That would really depend on the requirements. If the multiple parameters are on the same request you'd need to recursively rewrite the URL first before triggering an external redirect (to avoid multiple redirects). This would warrant its own question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.