Htaccess 301 redirect with query string params - .htaccess

This is so elementary that you have to ask why it is so complex to implement.
Simply need to redirect one url to another on the same domain, the url contains query string aparams though:
I tried:
RewriteRule ^article.php?section=exclusive&id=543 articles/view/4639 [R=301,L]
I get page cannot be found - the redirect is not happening. I have other re-directs in the same htaccess which work. The query string params are causing problems, and escaping them also does not help.
Note the id's of the two urls are not the same and therefore I don't want a clever re-write rule which converts from one url to another with the query strings mapped to new params. I just want a fast and dirty mapping from A to B.
UPDATE
I tried this it redirects but it is adding my old query string to the end of the new url. How can I prevent that?
RewriteCond %{REQUEST_URI} ^/article\.php$
RewriteCond %{QUERY_STRING} ^section=exclusive
RewriteRule ^$ http://www.onlinegooner.com/articles/view/3000 [R=301,L]
Input url is: mydomain.com/article.php?section=exclusive

You may use this rule:
RewriteCond %{QUERY_STRING} ^section=exclusive [NC]
RewriteRule ^article\.php$ /articles/view/3000? [R=301,L,NC]
? in the target will strip off previous query string.

Related

Redirect a URL in .htaccess, but keep the query string

I want to redirect a URL in .htaccess, but want to keep the (dynamic) parameters from the query string at the end of the URL (e.g. ?id=1660, ?id=1661, etc.)
E.g.
https://mywebsite.example/service/viewinvoice.php?id=1660
I want to redirect it to:
https://mywebsite.example/whmcs-bridge/?ccce=viewinvoice.php?id=1660
So basically: https://mywebsite.example/service/viewinvoice.php?id=... needs to be redirected to https://mywebsite.example/whmcs-bridge/?ccce=viewinvoice.php?id=...
I tried this below, without any success
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)/service/viewinvoice.php?id= [NC]
RewriteRule ^/?$ /whmcs-bridge/?ccce=viewinvoice.php [L,R=301]
I think this is not the right solution.
Does someone has suggestions?
You need to use the QSA (Query String Append) flag on your rewrite rule. From the documentation:
When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.
From your example URLs, you don't need to match the query string in a rewrite condition. You are matching the URL path which is done as the first part of the rewrite rule itself.
Your rule should be:
RewriteEngine On
RewriteRule ^/?service/viewinvoice\.php$ /whmcs-bridge/?ccce=viewinvoice.php [L,R=301,QSA]

htaccess dynamically rewrite

example:
https://www.mydomain.de/myproduct/?___store=french&___from_store=german
to
https://www.mydomain.de/myproduct/
I have a store and want to dynamically permanent rewrite storeviews of hundrets of urls to one part of the same url.
And this for many URLs at ones redirecting via 301 to the product part
Can you help me?
You can use this rule to strip off the query string:
RewriteCond %{QUERY_STRING} ___store=
RewriteCond %{QUERY_STRING} ___from_store=
RewriteRule (.*) $1? [R=301,QSD,L]
It checks if the query string containing ___store= and ___from_store=, then redirect to the same URL permanently without the query string.

htaccess redirect url by adding parameters

Need Guidance of 301 redirecting Url through htaccess matching specific format
Existing URL
http://www.example.com/index.php?option=com_toys&limitstart=20
Proposed URL
http://www.example.com/index.php?option=com_toys&view=list&Itemid=2&limitstart=20
Here limitstart may change to 0,20,40,60 to even 1000 and thus should remain same in the new proposed url too
Can anyone advise on to redirect using htaccess of above
You need to match query string using RewriteCond using a regex to capture both parameters:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(option=com_toys)&(limitstart=\d+)$ [NC]
RewriteRule ^/?index\.php$ %{REQUEST_URI}?%1&view=list&Itemid=2&%2 [L,NC,R=301]

Rewrite URL'S with a question mark “?”

I have a few url that have appeared in google search console with "?" and under scores "_"in the URL. I have tried many ways to redirect them but i have failed. I believe it has some thing to do with using the %{QUERY_STRING}
this is the URL that i need redirecting from
repairs-blog?journal_blog_tag=iPhone
to
blog?journal_blog_tag=iPhone
Maybe someone could write the solution?
When a query string is involved, you use RewriteCond with %{QUERY_STRING}, e.g.
RewriteCond %{QUERY_STRING} journal_blog_tag=
and then rewrite with RewriteRule
RewriteRule ^repairs-blog$ /blog [L]
Because you don't change the query string, there's nothing more to do.
When you want to rewrite this URL, no matter what query string is given, you can also omit the RewriteCond and just use the RewriteRule.
If you want to redirect instead of rewrite, e.g. change the URL in the client, you add the R flag
RewriteRule ^repairs-blog$ /blog [R,L]

htaccess rewrite querystring and remove empty value

first, sorry for my bad English.
I try to rewrite url generated from Form Get and redirect that.
my url is like this:
http://www.mysite.com/properties?action=search&agreement=for-rent&category=my-category&type=&zone=my-zone&city=my-city
and I have this .htaccess configured:
11. RewriteCond %{QUERY_STRING} ^action=(?:[a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)$
12. RewriteRule (.*) %{REQUEST_URI}/%1/%2/%3/%4/%5/? [R=301,L]
So basically all my request are direct to index.php.
21. RewriteCond %{REQUEST_URI} !index\.php|resources|hidden
22. RewriteRule ^(.*)$ index.php/$1 [L]
All works, but the problem is when I have an empty value in query string, the rule add double slash and the above url (for example whit &type=&zone=my-zone... type have empty value) will translate like that:
http://www.mysite.com/for-rent/my-category//my-zone/my-city/
The question is: How can i remove in .htaccess the double slash generated if i have one or more empty value in query string?
Thanks
Easiest is to do another redirect (not real pretty as it requires two 301's).
RewriteCond %{THE_REQUEST} //
RewriteRule .* $0 [R=301,L]
The fun part is that when the url is loaded with a double slash in it, mod_rewrite will automatically remove this. So as you can see above you'll just have to rewrite the url to itself, kind of.

Resources