I am currently working on a Rewrite Rule where I need to append certain text into the redirected URL.
The URL that that I want to type into the browser is
http://testwebsite.com/search/?q=SEARCH_STRING
I want this redirected to
http://testwebsite.com/search/SEARCH_STRING/
Basically the SEARCH_STRING needs to be taken from infront of ?= and put after /search/
The current rule that I have is malfunctioning:
RewriteRule ^.*\/search\/\?q=(.*) /#!/search/$1/ [R=301,L,NC,NE]
Any idea how I can fix this ?
You need to capture the query string in a RewriteCond, it's not part of the string your RewriteRule implicitly matches against
RewriteCond %{QUERY_STRING} ^q=(.+)
RewriteRule ^/search/$ /search/%1? [R=301,L,NC,NE]
The trailing ? deletes the existing query string.
Related
I've no experience with regex, and the redirect rule I'm trying to put in my .htaccess file for a WordPress site is having no effect.
I want to redirect:
https://example.com/example/?person=name
to
https://example.com/example/people/name
From reading, I figure my rule ought to be:
RewriteRule \?person=(.*) https://example.com/example/people/$1 [R=301,L]
What am I missing/doing wrong?
From reading, I figure my rule ought to be:
RewriteRule \?person=(.*) https://example.com/example/people/$1 [R=301,L]
You can't match the query string part of the URL using the RewriteRule pattern. The RewriteRule pattern matches against the requested URL-path only. To match the query string you need to use a condition and check the QUERY_STRING server variable.
For example:
RewriteCond %{QUERY_STRING} ^person=([^&]*)
RewriteRule ^example/$ /example/people/%1 [QSD,R=302,L]
This needs to go before the existing WordPress directives.
This matches the URL-path exactly as stated in your question, ie. /example/.
%1 (as opposed to $1) is a backreference to the last matched condition, ie. the value of the person URL parameter, that occurs as the first URL parameter.
The QSD (Query String Discard) flag (Apache 2.4+) is required to remove the query string from the target URL. If you are still on Apache 2.2 then append an empty query string (ie. append a ?) to the end of the subsitution string instead.
I am trying to redirect the following URL:
url/efx.aspx?xxxxxxx
to url/car-audio/efx-hardware/amp-install-kits
However it is redirecting whatever contains efx.aspx with the letters without the ? sign. I was wondering how I can fix this?
for example it is redirecting the following:
domain.com/efx.aspxlsdkjfhlasdf
but it is not redirecting
domain.com/efx.aspx?lsdkjfhlasdf
here is the .htaccess rule I wrote. how can I correct it?
RewriteCond %{REQUEST_URI} /efx.aspx[^/]+$
RewriteRule (.*) /car-audio/efx-hardware/amp-install-kits [R,L]
You can use this rule:
RewriteRule ^efx\.aspx$ /car-audio/efx-hardware/amp-install-kits? [R=301,NC,L]
Query string is not part of REQUEST_URI hence [^/]+ after efx.aspx fails your rule.
Also ? at the end of target URI removes any existing query string.
What I would need is to redirect with htaccess an url that has already some google analytics parameters in it, and redirect it adding the language parameter at the end of the url. I tried this with no luck so far but I know its wrong at some point:
RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^random?$1 http://www.domain.com/random?$1&language=french [R=301,QSA]
I'm trying with $1, I don't know if this is correct, the intention of which is to include all the parameters like utm_source=1&utm_medium=2, after this I would need to include the language parameter, so redirected url should look like http://www.domain.com/random?utm_source=1&utm_medium=2&language=french.
What is the right way to achieve what I need?
Thank you in advance.
Back-References
In your rule, $1 is a back-reference to a capture group that doesn't yet exist.
Try this instead:
RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteCond %{QUERY_STRING} !^.*language=french
RewriteRule ^(random.*) $1?language=french [R=301,QSA,L]
At the beginning ^ of the url path, (random.*) matches random and the rest of the url. (So we are only rewriting urls that start with random... is that what you want?)
We rewrite with the back-reference $1, followed by the query string ?language=french
The QSA flag ensures that existing query-string parameters are added.
I try to do a 301 redirect with .htaccess.
The issue:
/?view=products&id=12345 -> /8831
there is no relation between the old and the new address.
For some reason
Redirect 301 /?view=products&id=12345 /8831
doesn't work. If I remove the question mark, it works without question mark.
i tried also:
RewriteCond %{QUERY_STRING} view=products&id=12345
RewriteRule .*$ /8831 [L,R=301]
but it redirects me to /8831?view=products&id=12345, which is not good for me. I don't need the query string in the new url-
RewriteCond %{QUERY_STRING} view=products&id=12345
RewriteRule .*$ /8831? [L,R=301]
The ending ? will prevent the original query parameters from being appended, unless you also give the [QSA] flag again.
From the manual:
Note: Query String
The Pattern will not be matched against the query string. Instead, you must use a RewriteCond with the %{QUERY_STRING} variable. You can, however, create URLs in the substitution string, containing a query string part. Simply use a question mark inside the substitution string, to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine a new query string with an old one, use the [QSA] flag.
I'm working on some rewrite rules, and for some reason a regexp I'm not expecting to pass (and does pass not on any of my regexp testers) is passing in mod_rewrite.
The URL in question is:
http://url.com/api/projects.json?division=aa
And the rewrite rule is:
RewriteEngine On
RewriteBase /
RewriteRule ^api\/([^.?#/%\s]+)\.([^#?\s]+)$ api.php?type=$1&format=$2 [NC,L]
Because the second capture is immediately followed by $ I'd expect that URL to fail because of the query string, but it seems to accept just fine and pass the two parameters to GET.
Any thoughts?
Note: Query String
The Pattern will not be matched
against the query string. Instead, you
must use a RewriteCond with the
%{QUERY_STRING} variable.
Snip from the bottom of the docs