Apache redirect to main URL on Query - linux

i have requirement to redirect Apache on query base parameters for example
https://example.com/?ampostpreserve=01902018
I need to redirect to https://example.com .. I tried with
RewriteEngine On RewriteCond %{QUERY_STRING} ^ampostpreserv$
RewriteRule (.*) https://example.com [R=301,L]
but seem not working ..any solution
Thanks Hem

It seems like you just want to drop the query string? The QSD parameter does exactly that. The below version is for every called URL.
RewriteEngine On
RewriteCond %{QUERY_STRING} .+
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [QSD,L,R=301]
And this is the specific version for example.com and the above query string:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^ampostpreserve
RewriteRule .* https://example.com [QSD,L,R=301]

Related

.htaccess URL redirect for sub domain and query pattern

I need to redirect via .htaccess such an URL:
mydomain.com/?c=*
into
sub.mydomain.com/?c=*
For example:
must be redirected
mydomain.com/?c=123&a=1&b=2
into
sub.mydomain.com/?c=123&a=1&b=2
must not be redirected neither
mydomain.com/folder/?c=123&a=1&b=2
nor
mydomain.com/?cb=123&a=1&b=2
My Apache ver 2.4.29.
Thank you.
You can use the following rule
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^c=.*$ [NC]
RewriteRule ^$ http://sub.domain.com/ [L,R]

Htaccess redirect issue with query string

I have URL domain.com/index.php
I have written this code to redirect to clientarea.php
RewriteEngine On
RewriteRule ^index.php /clientarea.php [R=301]
Now this works except for urls that contain a query string (eg. domain.com/index.php?=something). This will also redirect but I don't want it when there is a query string.
Can anyone tell me how I can do it ?
To prevent rewriting when a query string is there
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php$ /clientarea.php [R=301,L]
RewriteEngine On
RewriteRule ^index.php$ /clientarea.php? [R=301]
$ marks the end of the string in the regex.

HTAccess redirect Based on Query Parameter Fails?

am trying to redirect (HTAccess) a URL based on the existance of a Querystring Paramater "ref". Only redirect when this exists with the prefix folder of "TMP" and page name "domain.html".
The problem is that the redirect is working, but the Querystring isnt being passed on
e.g.
http://www.olddomain.com/TMP/domain.html?ref=website-reference.com
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/TMP/domain\.html$
RewriteCond %{QUERY_STRING} ^ref=([0-9]*)$
RewriteRule ^(.*)$ http://www.newdomain.com/?ref=%1 [R=301,NE,NC,L]
You can use:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^ref=
RewriteRule ^TMP/domain\.html$ http://www.newdomain.com/ [R=301,NE,NC,L]
With the same query string.

Add query string to redirect URL in .htaccess

How can I add s string to the end of a search result using .htaccess?
Original link: www.example.com/?s=search1 redirect to:
www.example.com/?s=search1&post_type=product
or
www.example.com/?s=search2 redirect to:
www.example.com/?s=search2&post_type=product
"search1" and "search2" are variable.
I need this to change a Wordpress search result to a Woocommerce search result.
What a tried:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=([^&]+)
RewriteRule ^/?s=$ http://www.example.com/?s=%1&post_type=product [L,R=301]
Thank you in advance.
Your .htaccess seems fine, except for one thing: the RewriteRule Pattern will be matched against the part of the URL after the hostname and port, and before the query string, so this should do:
RewriteEngine On
RewriteCond %{QUERY_STRING} !post_type=product
RewriteCond %{QUERY_STRING} ^s=([^&]+)
RewriteRule ^$ http://www.example.com/?s=%1&post_type=product [L,R=301]

.htaccess 301 redirect to the same page with no query string

I want to create a condition that if page has parameter in URL like ?print=1 - redirect this page to itself without any querystring.
Example:
I want this page:
http://sos-med.com/en/specific_page.html?print=1&ok=1
tp redirect (301) to the same URL with no Query string:
http://sos-med.com/en/specific_page.html
My code is:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^*print=*$ [NC]
RewriteRule ^(.*)$ %{REQUEST_URI}?
I have no test server, so can you tell me if my code is ok?
The code above is for every page on website. And before implementing that rule I would like to try the redirect for one specific page (see my example).
How to modify the code to work with "specific_page.html" only?
I want only .htaccess solution, not PHP code.
You're close, your %{QUERY_STRING} regex isn't right, and you're missing the 301 redirect flag:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^print=.*$ [NC]
RewriteRule ^(.*)$ %{REQUEST_URI}? [L,R=301]
Try that.
Thanks, and If I want to redirect single specific page: sos-med.com/en/aaa.html?print=1&ok=1 to sos-med.com/en/aaa.html ? –
Then you'd change what the rule matches against:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^print=.*$ [NC]
RewriteRule ^en/aaa.html$ %{REQUEST_URI}? [L,R=301]
Try this one instead :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*&)?print= [NC]
RewriteRule ^(.*)$ /$1? [L,R=301]

Resources