0

I've the following in .htaccess:

RedirectMatch 301 /Search/[my_value]/m(.*) /Search?city=[my_value]

[my_value] is always the same in both URLs. How can I also copy [my_value] from the first URL to the other URL?

2 Answers2

0

Just use the placeholder reference as documented in the alias module:

RedirectMatch 301 ^/?Search/([^/]+)/m(.*)$ /Search?city=$1

Or you can instead use a simple rewriting rule as offered by the rewriting module:

RewriteEngine on
RewriteRule ^/?Search/([^/]+)/m(.*)$ /Search?city=$1 [R=301]

Both approaches can be used in the http servers host configuration, or, if really required, in dynamic configuration files (.htaccess style files). You should however definitely prefer the first option.


You really should start to read the documentation of the tools you use. Your question is answered in there:

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • Thanks for your answer. Another question. I often see `$1` in `.htaccess` code. Can I use `$1` multiple times? Or should I only use it once (for a single code) in an `.htaccess` file? –  Mar 16 '17 at 09:11
  • That depends on what you want to do, there is no general answer. As said above: why don't you simply read the documentation? That is what it has been written for: to answer such questions. That documentation is of excellent quality and comes with great examples. – arkascha Mar 16 '17 at 09:12
0

RedirectMatch uses Regular Expressions

This directive is equivalent to Redirect, but makes use of regular expressions, instead of simple prefix matching.

The supplied regular expression is matched against the URL-path, and if it matches, the server will substitute any parenthesized matches into the given string and use it as a filename.

This means, to capture part of the request, you must put it in parenthesis (...), and then use this as $1, $2, etc. in the target URL.


As @arkascha already has shown, to use [my_value], you capture it with (.+?) and add it in the target as city=$1. The second part m(.*)$ could be used as $2. But if you don't need it in the target, you can just remove the parenthesis and say m.*$, e.g.

RedirectMatch ^/Search/(.+?)/m.*$ /Search?city=$1

When everything works as it should, you may change the status code to 301. Never test with 301.

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198