HTAccess redirect Based on Query Parameter Fails? - .htaccess

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.

Related

Redirect a wild card URL to another custom URL

I need to redirect a URL with parameters to a custom URL in htaccess.
example.com/folder?misc-params-that-can-change
should redirect to
my.customlink.com
I don't know how to write the wildcard for the parameters that start with the question mark.
This is where I'm at now:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder [NC]
RwriteRule ^folder?(.*)$ http://www.google.com/$1 [R=301,L]
You may use this rule:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder/?$ [NC]
RewriteCond %{QUERY_STRING} .
RewriteRule ^ http://www.google.com/? [R=301,NC,L]
You cannot match query string in the pattern of RewriteRule, need to use %{QUERY_STRING} variable in RewriteCond construct for this purpose.
? after target URL is used to discard previous query string.

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.

How to modify the URL and one Query Var at the same time with .htaccess

I need to redirect a URL path and modify just one query var at the same time.
I've tried this but it doesn't work:
RewriteCond %{QUERY_STRING} ^(.*)id=(.*)$
RewriteRule /check/report /report?%1order_id%2
Old URL is:
/check/report/?id=5914f5&CC=91D36579
New URL needs to be:
/report/?order_id=5914f5&CC=91D36579
You can use this rule in DocumentRoot/.htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^id=([^&]+) [NC]
RewriteRule ^check/report/?$ /report?order_id=%1 [L,NC,R=302]

.htaccess URL redirection appending query string with target URL

I am new to .htaccess redirect.
I have a source URL with too many parameters query string.
Source
http://localhost/se/karriar/?utm_source=A_candy_box&utm_medium=Printed_media&
utm_term=2013&utm_content=Want_a_fresh_start_to_your_career%3F&
utm_campaign=Mint_pastille_campaign
Target: http://www.sample-website.com
When I tried to call source URL its getting redirected along with query string as like below.
http://www.sample-website.com?utm_source=A_candy_box&utm_medium=Printed_media&
utm_term=2013&utm_content=Want_a_fresh_start_to_your_career%3F&
utm_campaign=Mint_pastille_campaign
I have written the below script in .htaccess file.
RewriteCond %{HTTP_HOST} ^(www.)?localhost$ [NC]
RewriteCond %{QUERY_STRING} ^utm_source=A_candy_box&utm_medium=Printed_media&utm_term=2013&utm_content=Want_a_fresh_start_to_your_career%3F&utm_campaign=Mint_pastille_campaign$ [NC]
RewriteRule ^se/karriar(.*)$ http://www.sample-website.com [R=301,L,NC] `
I would like to not appending query strings in the targeted URL. Can you any one help me?
Replace your .htaccess with this
RewriteCond %{HTTP_HOST} ^(www.)?localhost$ [NC]
RewriteCond %{QUERY_STRING} ^utm_source=A_candy_box&utm_medium=Printed_media&utm_term=2013&utm_content=Want_a_fresh_start_to_your_career%3F&utm_campaign=Mint_pastille_campaign$ [NC]
RewriteRule ^se/karriar(.*)$ http://www.sample-website.com? [R=301,L,NC]
Append ? after your target domain.

.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