I’m assuming any-url
is literally any URL-path like foo
or foo/bar/baz/something
, but not nothing.
Try the following:
# Redirect "/customers/em/customers/any-url" to "/customers/any-url"
RewriteRule ^(customers)/em/customers/(.+) /$1/$2 [R=301,L]
The $1
backreference contains “customers” (saves repetition) from the RewriteRule
pattern. And the $2
backreference contains the any-url
part. Any query string that might be present on the request is passed through unchanged.
With regards to the URL /any-url/map/?str=Av+street+Fight,+19&cit=Alabama&sta=San+Paolo
, you’ve not actually stated whether the query string is literal or variable – I’m assuming the query string is literal and must be matched exactly as written. In this case you would need to do something like the following:
# Redirect "/any-url/map/?str=<value>" to "/customers/any-url"
RewriteCond %{QUERY_STRING} =str=Av+street+Fight,+19&cit=Alabama&sta=San+Paolo
RewriteRule ^(.+)/map/$ /customers/$1 [QSD,R=301,L]
The =
prefix-operator on the CondPattern (2nd argument of the RewriteCond
directive) makes it a lexicographical string comparison (not a regex), so the string is matched literally.
The QSD
flag on the RewriteRule
directive is necessary to discard the query string from the initial request.
If, however, the str
URL parameter should accept any non-empty value then change the above CondPattern to read: ^str=.
.
These redirects need to go at the top of .htaccess
file, before your existing directives (the WordPress front-controller), otherwise they are likely to not get processed.
You should also test first with 302 (temporary) redirects and only change to a 301 (permanent) redirect once you have fully tested that they work as intended. 301s are cached persistently by the browser so can make testing problematic.
Ensure the browser cache is cleared before testing.
Reference: