Properly 301 redirect an URL with parameters to a different site - apache

Right now I have an outdated URL that is getting 404'd:
http://mysite.com/category/?tag=blue-example
It needs to be redirected to:
http://subdomain.mysite.com/blue/
This doesn't work:
Redirect 301 /category/?tag=blue-example http://subdomain.mysite.com/blue/
How do I properly rewrite this URL?

The mod_alias does only chech the URI path and not the query (the part after the ?). But you can use mod_rewrite for this:
RewriteEngine on
RewriteCond %{QUERY_STRING} =tag=blue-example
RewriteRule ^category/$ http://subdomain.example.com/blue/? [L,R=301]

Related

htaccess 301 redirect with ? (question mark) in URL not working

This is probably a simple question, but I can not find why the 301 with an ? in the URL is not working. I have done a 301 redirect in the .htacces file but it´s not working. Other 301-redirects are working except for the one with a question mark in the URL.
I want http://www.example.com/?forum=2115543 redirected to http://www.example.com but the simple standard 301-redirect doesn´t work.
Example in .htaccess: Redirect 301 /?forum=2115543 /
You can not match a query string with Redirect (mod_alias). You need to use mod_rewrite.
You can give this a try.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^forum=(.+) [NC]
RewriteRule ^ /? [R=301,L]

Htaccess not working with?

I'm trying to redirect a list of URLs, but any of them that have a ? it isn't working. For example, here's my line of code...
Redirect 301 /calendar-of-events/day/date/2013-03-28/227.html?print=1&tmpl=component https://example.com/index.php/calendar/
This doesn't redirect. However, if I use this...
Redirect 301 /calendar-of-events/day/date/2013-03-28/227.html https://example.com/index.php/calendar/
...or this...
Redirect 301 /calendar-of-events/day/date/2013-03-28/227.html&tmpl=component https://example.com/index.php/calendar/
...and test the URLs out, they redirect correctly.
Any ideas on how to get this working?
You can't match query string using Redirect directive. Use mod_rewrite instead:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^print=1&tmpl=component$ [NC]
RewriteRule ^calendar-of-events/day/date/2013-03-28/227\.html$ https://example.com/index.php/calendar/? [L,R=301]

Apache redirect with GET variables

I would like to redirect all requests to a subfolder on down on a site. So:
http://thesite.com/oldfolder/whatever/anything.php?getvar_stuff
goes to
http://thesite.com/newfolder/
If I use the following .htaccess file:
RedirectMatch 301 /oldfolder http://thesite.com/newfolder/
.. the URL that returns is:
http://thesite.com/newfolder/?getvar_stuff
Don't want the "post_stuff", so I change the rewrite line to:
RedirectMatch 301 /oldfolder http://thesite.com/newfolder/?
.. the URL that returns is:
http://thesite.com/newfolder/?
Which is better, but I'd still love to lose that question mark. Is it possible?
It appears that the best way to do this is not using Redirect, but rather Rewrite:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/oldfolder/
RewriteRule ^(.*)$ http://thesite.com/newfolder/? [R=301,L]

Simple 301 redirect in .htaccess with query string does not work with Redirect directive

I am trying to redirect a single URL in a .htaccess file with Redirect:
Redirect 301 /index2.php?option=com_rss&feed=RSS2.0&no_html=1 /something/somethingelse/
I have a bunch of other similar rules which work using directory structure URLs, but this one refuses to get processed.
Redirect 301 /old/url/ /new/url/
Do I have to do anything special?
Thanks!
With Redirect you can only test for URL paths, or more specifically, URL path prefixes but not for the URL query. But you can do so with mod_rewrite:
RewriteEngine on
RewriteCond %{QUERY_STRING} =option=com_rss&feed=RSS2.0&no_html=1
RewriteRule ^index2\.php$ /something/somethingelse/? [L,R=301]

Htaccess 301 only part of the redirect works

Hi Im moving a site from one domain to another, and I have created the following .htaccess file, but its not working.
*#Options +FollowSymlinks
RewriteEngine On
redirect 301 http://www.el-netshop.dk/pi/Dækkape_UG150_12_lysegrå_5302_.aspx http://www.el-netsalg.dk/pi/Dækkape_UG150_12_lysegrå_5271_.aspx
RewriteCond %{HTTP_HOST} ^el-netshop.dk$ [OR]
RewriteCond %{HTTP_HOST} ^www.el-netshop.dk$
RewriteRule (.)$ http://www.el-netsalg.dk/$1 [R=301,L]
I would like it to work like this.
Have a list of urls where the url is diffent, with more then just the domain. Ex. in the above the from link contains 5302 but to link is 5271.
Then with the rest, I want it to make a normal redirect.
The above code just do (.*)$ http://www.el-netsalg.dk/$1 and ignores the special cases.
What am I doing wrong?
According to the apache docu the syntax is as folows:
Redirect 301 /service http://foo2.bar.com/service
So try:
Redirect 301 /pi/Dækkape_UG150_12_lysegrå_5302_.aspx http://www.el-netsalg.dk/pi/Dækkape_UG150_12_lysegrå_5271_.aspx
without the "http://www.el-netshop.dk" for the old-path paramater.