301 redirection remove parameters - .htaccess

I want to redirect this url :
http://loremipsum.com/en/videos?tmpl=component&print=1&page=
to
http://loremipsum.com/videos/all-videos/
So I write this 301 redirection :
RewriteCond %{QUERY_STRING} ^tmpl=component&print=1&page=$
RewriteRule ^en/videos$ /videos/all-videos/ [L,R=301]
It's redirect but , the url keeps all parameters :
http://loremipsum.com/videos/all-videos/?tmpl=component&print=1&page=
What I want is to remove these parameters.
Thanks for your help !

If you're using Apache ver 2.4 or later then you can use QSD to discard original query string:
RewriteCond %{QUERY_STRING} ^tmpl=component&print=1&page=$ [NC]
RewriteRule ^en/videos/?$ /videos/toutes-les-videos/ [L,NC,R=301,QSD]
On older Apache versions, you can a trailing ? in the target for the same effect:
RewriteCond %{QUERY_STRING} ^tmpl=component&print=1&page=$ [NC]
RewriteRule ^en/videos/?$ /videos/toutes-les-videos/? [L,NC,R=301]

Related

Apache redirect to main URL on Query

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]

.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]

Rewrite rule 301 htaccess

Via .htaccess, I would like to create an automatic 301 from an old URL to a new url:
An example old url is: http://www.example.com/test.html?s=2&ss=3
I would like that to be automatically redirected to: http://www.example.com/test.html
If you want to match this specific URL and query parameters then you can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=2&ss=3^ [NC]
RewriteRule ^test\.html$ %{REQUEST_URI}? [L,R=302]
If you want to use this query string with any URI then use:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=2&ss=3^ [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R=302]
? after %{REQUEST_URI} is needed to strip off any query string.

301 Redirect Get Parameter URL with htaccess

I need to 301 redirect an old url that contained a get parameter in the url.
I need to 301 the URL:
http://www.website.com/choose?cat=womens
to this URL:
http://www.website.com/womens
I have searched and tried without it working:
RewriteCond %{QUERY_STRING} cat=womens
RewriteRule ^choose\.php$ /womens [L,R=301]
Where am I going wrong?
You're almost correct, just 2 issues:
.php wasn't there in your original URI after choose as per the question
You need to add ? in target to strip original query string
You can use:
RewriteCond %{QUERY_STRING} (?:^|&)cat=([^&]+) [NC]
RewriteRule ^choose(?:\.php)?$ /%1? [L,R=301,NC]
Try these:
RewriteCond %{QUERY_STRING} ^cat=womens$
RewriteRule ^choose$ http://www.website.com/womens? [R=301,L]

.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