How to stop htaccess rewrite rule carrying over query string - .htaccess

I am setting up some redirects. I want to redirect the following URL:
/cms/index.php?cat_id=2
to the following URL:
/flash-chromatography
The rule I currently have is as follows:
RewriteCond %{QUERY_STRING} ^cat_id=2$ [NC]
RewriteRule ^cms/index\.php$ /flash-chromatography [L,R=301]
This rule is almost perfect apart from it redirect the URL to the following:
/flash-chromatography?cat_id=2
So you see my problem is it has kept the ?cat_id=2 part when I don't want it to.
How do I stop it keeping this bit?

Just add ? at the end of rewritten URL:
RewriteCond %{QUERY_STRING} ^cat_id=2$ [NC]
RewriteRule ^cms/index\.php$ /flash-chromatography? [L,R=301]

Related

RewriteRule in htaccess does not work correct

I have to rewrite old URLs like this:
Old URL:
http://www.domain.de/index.php?id=189&ext[stars]=PvVYdx58CfBpEIcUsdDvuE4fsvnwHw
New URL:
http://www.domain.de/sub1/sub2/stars/PvVYdx58CfBpEIcUsdDvuE4fsvnwHw
So I built following rule in my .htaccess file:
RewriteCond %{QUERY_STRING} ^id=([0-9]+)&ext\[stars\]=([A-Za-z0-9-]+)$
RewriteRule ^(.*)$ http://www.domain.de/sub1/sub2/stars/%2? [L,R=301]
but it does not work, the redirect happens to http://www.domain.de/sub1/sub2 without adding the parameter.
Any hints how to get this working?

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 remove query string and then redirect

I need to redirect a url based on query string and need to remove the query string on redirect.
Source url: http://www.example.com/?id=15&L=1&link=androidapp
Target url: http://www.example.com/test.php&id=15&L=1
'?id=' is a dynamic parameter. It is changed everytime.
I wrote following condition. It redirects, but I didn't get the desired target url.
RewriteCond %{QUERY_STRING} link=androidapp
RewriteRule ^(.*)$ http://www.example.com/test.php? [R=301,L]
Please help me.
You can use this .htaccess:
RewriteCond %{QUERY_STRING} id=(\d+)&L=(\d+)&link=androidapp [NC]
RewriteRule ^ test.php?id=%1&L=%2 [R=301,L]

301 redirect specific URLS if ?noRedirect not in URL

I'm running a wordpress installation and want to move specific feeds off-site. I've already got most of the technology down, but here's the problem. I want the following URL:
http://www.csicon.net/g/feed
to redirect to
http://feed.mesr.it/g
But if the URL comes in like this:
http://www.csicon.net/g/feed?noRedirect
I don't want it to redirect but load the original. Any thoughts?
The .htaccess file on http://www.csicon.net/ would contain:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^\/g\/feed$ http://feed.mesr.it/g [L,R=301]
Note: It has not been tested, but you get the idea.
Later Edit: Also, this can be done in PHP.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)csicon\.net$ [NC]
RewriteCond %{QUERY_STRING} !(^|&)noRedirect [NC]
RewriteRule ^([^/]+)/feed/?$ http://feed.mesr.it/$1 [L,NC,R=302]

.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