I have a URL format of
http://www.domain.com/?text
For any instance of this URL I'd like to redirect to a 3rd party URL but can't figure out how to match the supposed querystring.
RewriteCond %{QUERY_STRING} ^/text(.*)$
RewriteRule http://www.google.com [R=301,L]
Doesn't work
Your attempt is fairly close. Add /? in target to strip off this query string:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^/qipco
RewriteRule ^ http://www.google.com/? [R=301,L]
Related
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.
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.
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]
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]
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]