Redirect subdomain using .htaccess - .htaccess

I have a EC2 instance and host several sites on the server. I can redirect different domains to their specific folders but I cannot seem to redirect subdomains.
Here is my code so far for www.domain1.com which directs to "folder1" and retains the "www.domain1.com" address in the address bar - This is working fine:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} domain1.com
RewriteCond %{REQUEST_URI} !^/folder1
RewriteRule ^(.*)$ folder1/$1 [L]
However I cannot find a solution for sub-domains - I've tried many solutions similar to this - this does not work:
RewriteCond %{HTTP_HOST} sub.domain1.com
RewriteCond %{REQUEST_URI} !^/folder2
RewriteRule ^(.*)$ folder2/$1 [L]
I want http://sub.domain1.com to point to "folder2" but retain sub.domain1.com in the address bar. One thing I want to avoid is using my very long EC2 instance static address "http://ec2-12-34-567-890.compute-1.amazonaws.com/" in the code if this is possible.
Thanks

Try changing the host part to have boundaries:
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
and
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
The first rule is matching against the host sub.domain1.com because it contains a domain1.com

Related

htaccess HTTPS redirect excluding HTTPS subdomains

I know it has been asked many times here, but can't find the right configuration for my setup.
Requirements:
http://www.example.com & https://www.example.com (& example.com) have to redirect to https://test.example.com . (Notice it is a subdomain)
https://sub_1.example.com is to be EXCLUDED and not redirected. (Notice the subdomain has an 'underscore')
What I Have:
RewriteEngine On
RewriteBase /
# EXCLUDE following sub-domains.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^sub_1\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^test\.example\.com$ [NC]
# REDIRECT to
RewriteRule (.*) https://test.example.com/$1 [R=301,L]
>> ISSUE:
My https://sub_1.example.com is in fact an API that feeds a mobile app. When I use the above code, the mobile fails. So it seems that the sub-domain is not properly excluded and protected from the re-direct.
Would appreciate some help in cleaning it up.
FIXED !!
Needed to add an exception before the existing rules, since I needed to exclude https://sub_1.example.com/api
RewriteCond %{HTTP_HOST} ^sub_1\.example\.com [NC]
RewriteRule ^api - [L]

Wild card URL rewrite of subdomain to subdirectory

I am in a situation where an user can create his blog in a subdomain.
Users will create blogs and enter the address he wants like say,
abcd.domain.com Then my php code creates a directory called abcd.
To view the blog user will type in abcd.domain.com in his browser and I want a .htaccess code which will rewrite the url and open the files inside the domain.com/abcd
But for the user the url in the browser should stay abcd.domain.com
Currently I am trying this code
RewriteCond %{HTTP_HOST} ^test\.domain\.com$
RewriteCond %{REQUEST_URI} !^test/
RewriteRule ^(.*)$ /test/$1 [L,QSA]
But this gives me 404 even though I have a file test.html inside the test folder and trying to view that page.
Also in this situation I will have to manually make change to the .htaccess file for URL rewrite. What I want to know is if it is possible to have a wild card subdomain redirect to the respective directory.
You can use:
RewriteCond %{HTTP_HOST} ^test\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^(.*)$ /test/$1 [L,QSA]
REQUEST_URI with leading /.
With wild card subdomain:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/%1/
RewriteRule ^(.*)$ /%1/$1 [L,QSA]
Note that it takes more than a rewrite rule to have wildcard subdomains. Just fyi.
You need to have created a wildcard DNS record for subdomains and also tell apache to use any subdomain request by having a ServerAlias of *.domain.com in the apache config.
Then try your rule this way and see if it works for you.
RewriteCond %{HTTP_HOST} ^((?!www).+)\.domain\.com$ [NC]
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule ^(.*)$ /%1/$1 [L,QSA]

Redirect to other domain, but keep original domain, except one folder

I have a site that collects multiple dealers. Some of them have their own domains, so for them I need to redirect to my domain, but keep the typed domain in the address bar.
This one did that trick for me: Redirect to other domain but keep typed domain
But the problem is that the redirecting is to a subfolder on my server, so mydealer.com will point to mydomain.com/dealers/mydealer, which causes all my static files to not load. They are located in a folder called /static/.
So is there a way to have mydealer.com/anything/ to point to mydomain.com/dealers/mydealer, but have mydealer.com/static/ to point to mydomain.com/static/?
You can add a condition to check if /static/ is requested
RewriteEngine On
# for "static" part of dealers
RewriteCond %{HTTP_HOST} !^(www\.)?(domain\.com)$ [NC]
RewriteCond %{REQUEST_URI} ^/static/ [NC]
RewriteRule ^(.*)$ http://%1/$1 [P]
# for dynamic part of dealers (/dealers/domain-dealer/*)
RewriteCond %{HTTP_HOST} !^(?:www\.)?(domain\.com)$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+) [NC]
RewriteRule ^(.*)$ http://%1/dealers/%2/$1 [P,NC]

Redirecting Subdirectory to Subdomain with htaccess

I'm relatively new to using .htaccess, and have never done any coding besides what I've read online. I'm using Bluehost, and I'd like to redirect my blog subdirectory to a subdomain. Example: I'd like to redirect www.example.com/blog to blog.example.com.
I already have code in place to always add www. to the beginning of my blog address in the root folder, but I don't know how to accomplish the above redirect by using code in .htaccess. Any help would be appreciated!
A lot of web hosts today provide an easy implemention for subdomain creation in their administration panels. You just need to to go there, choose you subdomain name, and then point it to a directory in your tree.
If you can't, then it will be a little more complicated (You will need to resolve that subdomain to your server ip, configure some virtual hosts ... etc) and you may not have enough privileges to do that (unless you are on a dedicated server).
Edit 2
To redirect requests to www.example.com/blog to blog.example.com, try this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^blog/(.*)$ http://blog.example.com/$1 [L,QSA,R=301]
RewriteCond %{HTTP_HOST} ^blog\.example\.com$
RewriteCond %{REQUEST_URI} !^blog/
RewriteRule ^(.*)$ /blog/$1 [L,QSA]
I wanted to add my two cents,
1) to answer the question above, this rewrite should fix it:
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^/blog$ http://blog.example.com [R=302,L]
2) but, I think this is not enough by itself, you also need to change DNS, so that blog.example.com is pointed at the right server, and this can be done by a cname similar to this:
blog.example.com CNAME example.com TTL 1080
(not exactly how it will look, but use your DNS webinterface to set this up).
Have you tried this one?
RewriteEngine on
RewriteBase /
RewriteRule ^/blog/(.*)$ http://blog.subdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^check.domain.info$
RewriteCond %{REQUEST_URI} !^/check/
RewriteRule (.*) /check/$1
To redirect subdomain1 and subdomain2 and directory3 to a directory with HTTPS://, I use the following code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain1.example.com [OR]
RewriteCond %{HTTP_HOST} ^subdomain2.example.com [OR]
RewriteCond %{HTTP_HOST} ^example\.com/subdomain3 [NC]
RewriteRule ^(.*)$ https://example.com/subdirectory/$1 [R=301,L]

Optimize htaccess Wildcard Subdomain Code

I have Wildcard Subdomains working perfectly on my server, but would like to optimize the .htaccess code to make it more portable. I'm not sure how to do it.
I would like to replace the specific domain name and extension (DOMAIN and .com in the code below) with a regex match so that when using the wildcard code for other domains it is easy to port. So it should be a drop in replacement for domain.com and domain2.net. Here's the code I use:
# Wildcard Subdomain
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+)\.DOMAIN\.com [NC]
RewriteCond %2 !^www$ [NC]
RewriteRule ^(.*)$ http://DOMAIN.com/$1?a=%2 [R=301,QSA,L]
It's a little different than the usual version you see, because I've added the exception for the www. + subdomain condition. It many cases people enter www.sub.domain.com which breaks on the server. So I check for the www. and remove it before redirecting.
Thanks for your help!
The way you currently have it:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www)\.(.*)\.(.*)\.(.*) [NC]
RewriteCond %2 !^www$ [NC]
RewriteRule ^(.*)$ http://%3.%4/$1?a=%2 [R=301,QSA,L]
If you just want to strip the www from the domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www)\.(.*)\.(.*)\.(.*) [NC]
RewriteRule ^(.*)$ http://%2.%3.%4/$1 [R=301,QSA,L]

Resources