Redirect parent domain to subfolder - .htaccess

I know there are a lot of similar questions, still I need your help.
My main site (maindomain.com) is in the root directory of the server public_html.
I have created a subdomain example.maindomain.com and it is in the sub-folder public_html/example
I parked the domain example.net and I need it to point to public_html/example and I need to create the rewrite rules in .htaccess
Still, I maindomain.com to work normally, not redirect from it.
I tried but no success:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.net$
RewriteRule ^(.*)$ /example/$1 [L]

Related

301 Redirect root without cascading new root directory to all other 301 redirects

I have successfully redirected the root of my site to a sub-directory in another site:
#Redirect Root to subdirectory on example.com
RewriteEngine On
RewriteRule ^$ http://example.com/subdirectory [L]
I'm running into trouble with all of my other redirects. The sub-directory of the new root (/subdirectory) is getting inserted into my redirects before each path, which is resulting in broken redirects.
Example:
Redirect 301 /contact-us/ https://www.example.com/company/contact.shtml
tries to point to
https://www.example.com/subdirectorycompany/contact.shtml
In a nutshell, I can either get the root working, or I can get the redirects working -- but not both. Can anyone please lend some insight?
I have read a bunch of posts, which is how I have gotten as far as I did, but I could use some help, please and thank you.
Don't mix Redirect directive and RewriteRule as they are from different Apache modules and are invoked at different times.
It is better to have all your rules like this:
RewriteEngine On
# all specific 301 redirects go here
RewriteRule ^contact-us/?$ https://example.com/company/contact.shtml [L,NC,R=301]
#Redirect Root to subdirectory on example.com
RewriteRule ^$ http://example.com/subdirectory [L,R=301]

.htaccess redirect "old-domain" (with subfolders) to "new-domain" (keep subfolders)

I have a hosting packet with domain: olddomain.com.
In cpanel "olddomain.com" is the main domain.
In cpanel i added another domain "newdomain.eu", by used "Aliases" process.
i am looking for a solution to redirect the olddomain.com to newdomain.eu
but not only the main domain but, and the subdirectories.
olddomain.com/something to newdomain.eu/something or olddomain.com/something2 to newdomain.eu/something2
i tried this in .htaccess:
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain.com$
RewriteRule (.*)$ http://newdomain.eu/$1 [R=301,L]
But olny the olddomain.com redirect to newdomain.eu when i try a domain with subfolder stay as is:
olddomain.com/something stay olddomain.com/something
any idea?

Redirect all sub-domain to parent domain except one

I am able to access my website with random sub-domains like this
abcd.theonlytutorials.com
abc.theonlytutorials.com
etc.theonlytutorials.com/otherpage/otherpage
I have one real sub-domain where my blog is
blog.theonlytutorials.com
Is there any possible way to redirect all my fake sub-domains to root domain except the 'blog' one?
Try adding these rules to the htaccess file in your document root, preferably above any rules that you may already have in there:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+)\.theonlytutorials\.com$ [NC]
RewriteCond %1 !^(blog|www)$ [NC]
RewriteRule ^(.*)$ http://theonlytutorials.com/$1 [L,R=301]
You may look into following URL:
Redirect sub domain to main domain

How can I redirect subdomain to folder while main domain points to another folder?

My very dear Stackoverflow community,
I have the following redirection problem and after several unsuccessful attempts I come here in search of enlightenment. My problem is the following. I have a domain, let's call it 'www.mydomain.com', and my 'public_html' directory has two folders as follows:
public_html
public_html/my_app/
public_html/my_other_app/
First, I would like that when typing the URL 'www.mydomain.com', I get redirected to the contents of folder 'my_app', while keeping the same URL. In fact this I have already accomplished, so whenever I type 'www.mydomain.com' I get redirected to 'www.mydomain.com/index.php', which actually corresponds to the 'public_html/myapp/index.php' script under 'myapp'.
Now I want to have a subdomain called 'other.mydomain.com', which has to redirect to contents of the 'my_other_app' folder, but I do not know how to make .htaccess work for this and at the same time work for the first case also.
So this is basically, the main domain redirects to one folder, and a subdomain redirects to another folder, and both folders are located under the public_html directory
Any hints more than welcome.
For your reference I post below my current .htaccess file:
RewriteEngine On
# redirect to www prefix
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# if start with www and no https then redirect
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# rewrite URL to trim folder
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^$ /login [L,R=301]
RewriteRule ^(.*)$ test/$1 [L]
This actually works for my main domain, it also rewrites the url to https. I need to add something in here in order to process separately the 'other.mydomain.com' and redirect to the '/my_other_app/' subfolder
what you need is a vhost (virtual host) per app. In the vhost, you will define the vhosts root directory, which will point to either of your sub directories.
There is IP based vhosts (one IP address per subdomain) or name based vhosts (the vhost is chosen based on the HTTP host header that all modern browser send).
But there is too much to say about vhosts to write it all here, just read the apache documentation here:
http://httpd.apache.org/docs/2.2/vhosts/
I think with pure .htaccess files, you can't do that (I might be wrong). Normally you would add vhosts in the main apache config. Based on your hosting, this may not be possible. Talk to you hosting provider in that case.
Marc

.htaccess redirect main domain but not sub subfolder

I was wondering how to exclude a subfolder from .htaccess redirect.
I have an .htaccess file in the root of my old domain and I have the following in it:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
This of course redirects any traffic from the old domain to the new one. But I want to exclude a directory from this rule. Meaning www.olddomain.com/media/videos should not redirect to www.newdomain.com/media/videos
The reason I want the exclusion is because I'm hosting static files such as videos on a shared web server (old domain) and I'm keeping everything else the site needs on the new VPS server (new domain).
Just put RewriteCond %{REQUEST_URI} !^/media/videos above your RewriteRule. It checks wether the URL starts with /media/videos and if it does so, the RewriteRule will not be met.

Resources