Apache redirect with GET variables - apache

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]

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]

Opencart 301 Redirects

Having a problem with redirect in a .htaccess file on an Opencart store.
It seems that any URL with /index.php?_route_= isn't getting redirected.
For example, this works:
redirect /old-url-here http://example.com/new-url?
This doesn't:
redirect /index.php?_route_=some-url.asp http://example.com
Any idea or suggestions as to what might get this to work?
You can't match against the query string using mod_alias' Redirect directive. You'll need to use mod_rewrite, and if you use mod_rewrite, you're probably going to want to stop using mod_alias altogether.
Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} route=some-url\.asp
RewriteRule ^index\.php$ http://example.com/
Another thing is - apart from Jon's answer - that URLs like index.php?_route_=some-keyword are used/created only internally and only in case you have the SEO turned on. In this case, you click on a link with URL like http://yourstore.com/some-keyword and this URL is rewritten into index.php?_route_=some-keyword.
Therefore you shouldn't be creating URLs like that on your own nor try to redirect from them. If you need to redirect, catch the first SEO URL to redirect it.
We do not know where did you put your changes into the .htaccess file, but if you take a close look at this rewrite rule
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
not only you'll find out it is the last rule there, but it also has this L switch which is telling Apache server that if this rule is matched, do a URL rewrite(, attach the query string [QSA] and) stop matching other rules [L] - this is the Last one.
If you need to redirect only a specific URL, do it the same way as redirect for sitemap.xml is done (for example) and place it before the last rewrite rule:
RewriteEngine On
RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
# ...
# your new rule here - while the URL in the link is http://yourstore.com/some-url.asp
RewriteRule ^some-url.aspx$ index.php?route=some-folder/some-controller [L]
# ...
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

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]

Basic URL rewrite with mod_rewrite

I would like to rewrite the URL of one of my pages from
http://www.mydomain.com/some/application/page.html
to
http://www.mydomain.com/apply
I believe this code will work. But in 301 redirects, you often see [R=301,L] or some version of that appended to the end of the rewrite rule - is the code below the best way to perform the redirection and will Google understand it?
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /some/application/page.html?=$1 [L]
I think you want to do this:
The URL that your users see (even google):
http://www.mydomain.com/apply
to
http://www.mydomain.com/some/application/page.html
the internal(actual) URL.
then I would suggest you to go this:
RewriteEngine On
#condition to redirect
RewriteCond %{REQUEST_URI} ^/?apply/?$
RewriteRule ^/?apply/?$ /some/application/page.html [L]
The flag [L] ( Flag L docs ) signifies RewriteEngine to stop rewriting any rules further. This will not perform a permanent redirection.
You don't need a [R=301] flag here. 301 is for permanent redirection. Use the short URL everywhere.
To my knowledge, you will need [R=301,L]to do a redirect properly. Apache2 by default will use a 302 redirect so if it is a permanent redirect you should force R=301, as you noted. The documentation for RewriteRule is unclear if [L] alone will always perform a 301 redirect. Be safe, tell apache exactly what to do :-).

Properly 301 redirect an URL with parameters to a different site

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]