Automatically redirect subdomain to folder path? - .htaccess

Is it possible to automatically redirect subdomains to a folder (structure)? I want to change the setup of my site from subdomain to folder and there a lot of redirects to be done, so i was wondering if there is any automatic solution.
What i want is i.e.:
subfolder1.domain.com
to be redirected to (internal redirect, same top-level domain) www.domain.com/folder/subfolder1
Where 'subfolder1' is relative (not static).
So is there a general htaccess code i can use?
Thanks

You can put this code in your htaccess (which has to be in document root folder)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www\.).+?)\.(domain\.com)$ [NC]
RewriteRule ^(.*)$ http://www.%2/folder/%1/$1 [R=301,L]
Note: by redirect in your question, i understood it as an external redirect. If that's not the case, i'll update my answer
EDIT (internal rewrite)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^((?!www\.).+?)\.domain\.com$ [NC]
RewriteRule ^((?!folder/).*)$ /folder/%1/$1 [L]

Related

Subdomain redirection w/ c-panel, Wordpress problem

I use GoDaddy c-panel subdomain redirect. It creates a directory
example.com/sub.example.com with htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.sub\.example\.com$
RewriteRule ^/?$ "http\:\/\/www\.example\.com\/My_Target_Wordpress_File/" [R=301,L]
This usually works. Visitors to
sub.example.com
are sent to http://www. example.com/My_Target_Wordpress_File/
But for one subdomain this does not work. Visitors are sent to another file, previously used for redirects. I have deleted and recreated the faulty subdomain.
Where is the problem? How do I make this redirect work?
I tried rewrites in the document root but to no avail.

Redirect Multiple URL within folder to other domain

I have some files placed under a particular folder in my old domain:
http://www.olddmain.com/subfolder/example1.html
I want to redirect all files under this folder to a new domain.
Example:
http://www.newdomain.com/subfolder/example1.html
How do I do this without losing Ranking of the pages?
Maybe you need this
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
from "Redirecting to a different domain" in Redirecting a Web Folder Directory to another Directory in htaccess
In the olddomain.com/subfolder/.htaccess file you just need a single mod_alias Redirect directive to redirect all files to newdomain.com/subfolder/<file>:
Redirect 302 /subfolder http://www.newdomain.com/subfolder
Change the 302 (temporary) to 301 (permanent) only when you are sure it's working OK. (301s are cached hard by the browser, so can make testing problematic.)
However, if olddomain.com and newdomain.com point to the same place then you will need to use mod_rewrite and include a condition that specifically checks for the host being requested, otherwise you'll get a redirect loop. For example, in the same olddomain.com/subfolder/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule ^ http://www.newdomain.com%{REQUEST_URI} [R=302,L]
This redirects any request for olddomain.com/subfolder/<anything> to http://www.newdomain.com/subfolder/<anything>.
Again, change 302 to 301 when you are sure it's working OK.

Redirect main domain to subdomain, but keep a subdirectory

Redirecting domain to subdomain seems easy using
RewriteCond %{HTTP_HOST} www.example.com
RewriteRule (.*) http://sub.example.com/$1 [R=301,L]
but how can I keep a single subfolder working without redirecting to subdomain?
so www.example.com/a-page AND example.com/any-page(.html or not)
will be redirected to sub.example.com/any-page(.html or not)
BUT I want a subfolder (just a single one) www.example.com/music/any-page(.html or not) AND example.com/music/any-page(.html or not) to work like that, to not be redirected (add an exception for it)
Note:
(.html or not) means that posts end in .html, pages don't (Wordpress) . not sure if it matters tho.
thank you
You can try something like this to not redirect if it's the subdirectory otherwise redirect all other requests.
RewriteCond %{REQUEST_URI} !^/subdir/.*$
RewriteCond %{REQUEST_URI} !^/subdir$
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^(.*) http://sub.example.com/$1 [R=301,L]

Redirect only the start page to extern site all sub categories should stay

I have a problem were I need to redirect www.mydomain.com to an external url www.otherdomain.com but I need all the subpages and urls to stay as they are (not get redirected).
Example www.mydomain.com/testpage should not get redirected. Is this possible?
And if so can anyone guide me what I need to write in my htaccess file for it to work.
The following .htaccess will do the trick:
RewriteEngine on
RewriteCond %{HTTP_HOST} www.\mydomain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://www.otherdomain.com/ [L,R=302]
It will redirect only the base domain (root), all subfolders and other URLs will stay.
Reference:
https://serverfault.com/questions/58762/how-to-redirect-root-and-only-root-via-htaccess

htaccess redirect subfolder of certain domain

I have a multi-subdomain multi-lingual website setup and I need to redirect a language subfolder of a certain domain. eg. au.domain.com/us/request_uri to au.domain.com/en/request_uri. This rule needs to be ignored on other domains. All domains are run off the same codebase and all use the one htaccess file (drupal install with domain access module). This shouldn't affect how the htaccess rule is set though.
Here is one way to do it:
RewriteEngine On
RewriteRule ^us/(.*)$ http://au.domain.com/en/$1 [L,R=301]
Redirects:
http://au.domain.com/us/anything to
http://au.domain.com/en/anything/
UPDATED
RewriteEngine On
RewriteRule http://au.domain.com/us/^(.*)$ http://au.domain.com/en/$1 [L,R=301]
Only http://au.domain.com/us/anything will be redirected to
http://au.domain.com/en/anything/
OR
RewriteEngine On
RewriteCond %{HTTP_HOST} ^au.domain\.com$ [NC]
RewriteRule ^us/(.*)$ http://au.domain.com/en/$1 [L,R=301]
Hope this helps.

Resources