htaccess redirect query string and discard - .htaccess

Having trouble with redirecting a dynamic URL. This is what I want to accomplish:
Redirect 301 /content/index.php?id=423 http://www.domain.com/new-page/
I tried this
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=423$
RewriteRule ^content/index\.php$ http://www.domain.com/new-page [L,R=301]
but had no luck. Thank you!
ps/
I lost some para
work with this code
RewriteRule ^([^.]+)/([A-Za-z]*)-([^.]+)-([0-9]+).html$ $1/$4-$2-$3.html [L,R=301]

You need to specify the query in the replacement URL. Otherwise the original query is taken:
RewriteCond %{QUERY_STRING} ^id=423$
RewriteRule ^content/index\.php$ http://example.com/new-page? [L,R=301]
And if you want to preserve other URL arguments, try this:
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=423&*([^&].*)?$
RewriteRule ^content/index\.php$ http://example.com/new-page?%1%3 [L,R=301]

Related

Cut off parameter from redirect in htaccess

This silent redirect in htaccess:
RewriteCond %{QUERY_STRING} ^pic_id=(\d+)$ [NC]
RewriteRule ^album_showpage\.php$ /gallery/image_page.php?image_id=%1 [L]
redirect a url that looks like:
example.com/album_showpage.php?pic_id=1906
to:
example.com/gallery/image_page.php?image_id=1906
that works great. But when the URL has a parameter like:
album_showpage.php?pic_id=1906&mode=prev
or
album_showpage.php?pic_id=1906&mode=next
the redirect wont work.
Question: How to cut of any parameter after pic_id=1906
thank you
You need match against the rest of the query string.
RewriteCond %{QUERY_STRING} ^pic_id=(\d+)(&.*)?$ [NC]
RewriteRule ^album_showpage\.php$ /gallery/image_page.php?image_id=%1%2 [L]
If you don't want the mode=prev stuff to be included in the rule's target, then you can simply remove the $ instead of attempting to match against it:
RewriteCond %{QUERY_STRING} ^pic_id=(\d+) [NC]
RewriteRule ^album_showpage\.php$ /gallery/image_page.php?image_id=%1 [L]

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]

Moving using 301 in htaccess using GET variables

What am I doing wrong?
I have
http://www.site.com/shop/viewmy/?ASIN=234FhK3
http://www.site.com/shop/viewmy/?ASIN=423ZH3
Trying to convert to
http://www.site.com/shopping/234FhK3/moved
http://www.site.com/shopping/423ZH3/moved
I'm going to change the "moved" part with an internal redirect after I get some info about the product 234FhK3 , a variable that changes.
I have tried
RewriteCond %{QUERY_STRING} ASIN=([^&]+) [NC]
RewriteRule ^[^/]+/(shopping/)/?$ $1.(/moved)? [L,R=301,NC]
I'm not too good with htaccess rewrites.
Try this:
RewriteCond %{QUERY_STRING} ASIN=([^&]+) [NC]
RewriteRule ^shop/viewmy/$ /shopping/%1/moved/? [L,R=301,NC]

how to do a htaccess 301 redirect with query strings?

i have some links that i need to redirect, like:
/pass-help.php?action=1&email=test#test.com to /about/help
or
/contests/contest-a?testa=1&testb=1 /user/index/contest
i need a way to find the action or the email in the links above because they can be anything.
maybe something like /pass-help.php?action=(\?.+)&email=(\?.+) to /about/help
but im not sure how to write the query string.
any ideas?
if so, please explain a bit so i can understand what is happening.
thanks
If the query strings don't matter, you can just ignore them:
via mod_alias:
Redirect 301 /pass-help.php /about/help?
Redirect 301 /contests/contest-a /user/index/contest?
via mod_rewrite:
RewriteRule ^/?pass-help\.php$ /about/help? [L,R=301]
RewriteRule ^/?contests/contest-a /user/index/contest? [L,R=301]
Note that in the mod_alias case, there will be a stary ? at the end. That's needed in order to get rid of the query string.
If the query strings need to have some param names, but you don't care about the values:
via mod_rewrite:
RewriteCond %{QUERY_STRING} (^|&)action=
RewriteCond %{QUERY)STRING{ (^|&)email=
RewriteRule ^/?pass-help\.php$ /about/help? [L,R=301]
RewriteCond %{QUERY_STRING} (^|&)testa=
RewriteCond %{QUERY_STRING} (^|&)testb=
RewriteRule ^/?contests/contest-a /user/index/contest? [L,R=301]
If the query strings need to have some param names with EXACTLY some value:
RewriteCond %{QUERY_STRING} (^|&)action=1
RewriteCond %{QUERY)STRING{ (^|&)email=test#test.com
RewriteRule ^/?pass-help\.php$ /about/help? [L,R=301]
RewriteCond %{QUERY_STRING} (^|&)testa=1
RewriteCond %{QUERY_STRING} (^|&)testb=1
RewriteRule ^/?contests/contest-a /user/index/contest? [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