htaccess redirect How to redirect subfolder to subdomain and subfolder - apache

I moved a portion of the site to a subdomain. All answers I find here are without keeping the folder structure in mind.
So
http://mywebsite.com/projects
Has become
http://topic.mywebsite.com/projects
So what I have now is:
RedirectMatch 301 ^/projects/(.*)$ http://topic.mywebsite.com/projects/$1
But this creates an infinite loop.
So I tried
RedirectMatch 301 http://mywebsite.com/projects/(.*)$ http://topic.mywebsite.com/projects/$1
So it does not get into a loop on the subdomain, but this does not work at all.
Any idea?

You can use mod_rewrite and check that it only runs for the main site.
RewriteCond %{HTTP_HOST} =mywebsite.com
RewriteRule ^projects/(.*)$ http://topic.mywebsite.com/projects/$1 [R=301,L]

Related

htaccess subpage on subdomain to subpage on other subdomain

I have trouble finding out the right approach to make a 301 redirect from one subdomains page to another subdomains page.
For instance:
subpage1.mysite.com/contact
needs to be redirected to
subpage2.mysite.com/kontakt
(i have a few hundred of these)
I have a lot of 301 redirects in my .htaccess so far, but they only redirect from the maindomain mysite.com/page.
I cant make a 301 like this, right?
It does not work..
301
Redirect 301 http://subpage1.mysite.com/contact http://subpage2.mysite.com/kontakt
I only can access the primary htaccess file on the main domain (mysite.com)
Read some about RewriteCond but it seems they take all of my subpages and redirect them to the other subdomain. That is not exactly suitable for me, as the url's have changed names and are not identical on the subdomains.
I've figured it out by using this.
Taking the subdomain as RewriteCond
#Consultancy redirects
RewriteCond %{HTTP_HOST} ^consultancy.madebydelta.com [nc]
RewriteRule ^about/$ http://testlab.madebydelta.com/om-os/ [R=301,NC,L]
RewriteRule ^industries/$ http://testlab.madebydelta.com/cases/ [R=301,NC,L]

.htaccess RedirectMatch getting 404 for example.com/choice/

I am trying to do a .htaccess redirect and I have it working, except for one of my cases.
When I have this rule:
RedirectMatch "^/choice$" "/choice/home.html"
The address: www.example.com/choice redirects to www.example.com/choice/home.html.
In contrast when I have this rule:
RedirectMatch "^/choice/$" "/choice/home.html"
And give enter the address: www.example.com/choice/
I get a 404.
I tried this modification of the rule:
RedirectMatch "^/choice\/$" "/choice/home.html"
Recycled the server, but I'm still getting 404.
This is the condition I have set:
RewriteCond %{REQUEST_URI} ^/choice/*
My goal is to have it redirect to the same page whether the final / is present in the address or not. Anyone have any insight? Thanks!
RedirectMatch and RewriteCond have nothing to do with one another. The first is part of mod_alias and the second is part of mod_rewrite - these are two separate modules.
You can accomplish your redirect by using the following:
RewriteEngine on
RewriteRule ^choice/?$ /choice/home.html [R=302,L]
Change 302 to 301 to make the redirect permanent (cached).

Directory Redirect to sub-folder with same name

I am trying to redirect directory (.htaccess) to subdirectory with same name. I am getting redirection loop error.
RedirectMatch 301 /abc http://www.example.com/lite/abc/
You must delimit your pattern otherwise it will match abc everytime.
With RedirectMatch
RedirectMatch 301 ^/abc.*$ http://www.example.com/lite/abc/
Or with mod_rewrite
RewriteEngine On
RewriteRule ^abc.*$ /lite/abc/ [R=301,L]
Note: you'll need to clear your browser's cache before trying this code. Actually, your old rule is now in cache.

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]