regex with query string containing? - regex

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.

Related

RewriteRule redirect URL with query string in htaccess

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.

Rewrite Rule to append

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.

htaccess RewriteRule for found id in middle of url

I need get objectid from seo url e.g When I use this rule:
RewriteRule ^company/([A-Za-z0-9-]+)$ /lt?openobjectid=$1
evrything is ok when I use example.com/company/12 but if use example.com/company/12/some-text I get server error.
It is because your regex pattern is not matching example.com/company/12/some-text.
You can try this rule:
RewriteRule ^company/([a-z0-9-]+)(?:/.*)?$ lt?openobjectid=$1 [L,QSA,NC]

.htaccess redirect is sending whole string, instead of partial

I partially have my .htaccess rule working. What I have currently is:
#tag to search redirect
RewriteCond %{REQUEST_URI} ^/tag\/*
RewriteRule ^(.*) https://www.testurl.co.uk/search-results?hsf=$1&id=12 [R=301,L]
What is currently happening, is where the $1 is, the entire of tag/* is going in there.
i.e request is tag/test URL generated is
https://www.testurl.co.uk/search-results?hsf=tag/test&id=12
when it should ideally be:
https://www.testurl.co.uk/search-results?hsf=test&id=12
Any help would be greatly appreciated.
Many thanks
You can use this rule:
RewriteRule ^tag/(.+)$ https://www.testurl.co.uk/search-results?hsf=$1&id=12 [R=301,L,QSA]
Pattern ^tag/(.+)$ will capture any value after /tag/ into group #1 and that is being used in $1.
Make sure to clear your browser cache before testing this.

mod_rewrite: How can I rewrite a url with slash?

I can rewrite mydomain.comto www.mydomain.com, that's OK.
But, I just couldn't figure how to rewrite:
http://mydomain.com/great-article to http://www.mydomain.com/great-article.
How can this be done with regex? Any help would be appreciated.
This shall work:
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteRule .* http://www.mydomain.com/$0 [L,R=301,QSA]
I.e. every request to
http://mydomain.com/something
will get redirected to
http://www.mydomain.com/something.
As well as simple requests
http://mydomain.com/
will go to
http://www.mydomain.com/.
And due to Query string append (QSA) - this shall work for such URI
http://mydomain.com/index.php?action=hello&param=world
to get redirected to
http://www.mydomain.com/index.php?action=hello&param=world.
Or am I missing something?