Redirect multilevel subdomain to a folder .htaccess - .htaccess

I have a subdomain meet.example.com that points to the app folder and I want api.meet.example.com to point to the api folder.
The folder structure is like this
public_html - root folder
app
api
Then this is the rule that I have in my .htaccess I have in the root folder. The first rule works fine and opens the content from the app folder, but the second rule (api.meet.example.com) is not opening the content from the api folder.
RewriteCond %{HTTP_HOST} ^meet\.example\.com$
RewriteCond %{REQUEST_URI} !^/app/
RewriteRule (.*) /app/$1 [L]
RewriteCond %{HTTP_HOST} ^api\.meet.example\.com$
RewriteCond %{REQUEST_URI} !^/api/
RewriteRule (.*) /api/$1 [L]
What I am doing wrong here?
SOLUTION
Thanks for #anubhava question about the VirtualHost config I just realized that I did redirect both domains from one hosting to another, but didn't add both domains to the new hosting just the meet.example.com, assuming that as api.meet.example.com is actually a subdomain of the already added then I will not need to add it.
Adding api.meet.example.com as a domain to the new hosting fixed the issue.

Related

Rewrite Directory Based on Subdomain

I have a folder export that is accessible to all my subdomains:
/export/sub1/...
/export/sub2/...
/export/sub3/...
Right now, regardless of what subdomain you're on, you can see all of the content by changing the directory in the url.
It's not a security issue, but more of a canonicalization concern, but I'd like to use an .htaccess file to rewrite the folders so people see a modified path that matches up with their subdomain:
sub1.domain.tld/export/... is served from /export/sub1/...
sub2.domain.tld/export/... is served from /export/sub2/...
sub3.domain.tld/export/... is served from /export/sub3/...
How can I do this?
You can use this generic rule in site root .htaccess:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+) [NC]
RewriteRule ^/?export/(.*)$ /export/%1/$1 [L,NC]

.htaccess rewrite and subdomains connected to subdirectories

I have installed wordpress to my public_html/ folder on my host. Now, I want to help my friend to get his own wp blog. I wanted to add a folder public_html/zoran/ where I wanted to install another wp and to add subdomain zoran.example.com to call that folder.
Problem is that .htaccess from public_html/ (my wp installation) is messing .htaccess inside public_html/zoran/ folder. I googled a lot, and figured out that this is impossible to have one .htaccess rules in root folder and other in subfolder (mod rewrite).
I cannot access my cPanel for couple of days now, and it will be unavailable for a few days, I recieved mail about that.
Now, I am at this point. I have moved my wp into public_html/www/ and I stored .htaccess file into public_html/ with following content:
# Rewriting conditions
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) http://www.example.com/ [R,L]
# Identify subdomain as %1
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
# Check if %1 is directory
RewriteCond %1 !-d
# rewrite the request to the subdomain's subfolder
RewriteRule (.*) %1/$1 [L]
Above code redirects example.com to www.example.com, and also, it opens my wp, stored in public_html/www/. But, when I try to go to zoran.example.com, I get 500 error, and that folder has just index.html file in it.
What is happening? How come that www folder works, and folder zoran does not work on same principle?!

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

How to not redirect a single folder on a website....in htacess

I have my website in a subfolder from the main root.
www.domain.com ---redirects to---> www.domain.com/Folder1/
using:
RewriteEngine on
RewriteCond %{SERVER_NAME} domain.com [OR]
RewriteCond %{SERVER_NAME} www.domain.com
RewriteRule ^(.*)$ http://www.domain.com/Folder1/ [R=301]
this is working fine...
I also have a administrative WebApp located on Folder2. Folder2 users will always enter the direct URL to Folder2.
http://www.domain.com/Folder2/Version1/
It is at the same structure level as Folder1.
Problem is that when I try to access my webapp, its not working anymore and redirecting it to the Folder1 website.
Question: What htaccess code do I need to add to keep my webapp (folder2) working by not redirecting it to Folder1, while still making all regular users go to Folder1.
Customer facing Website: www.domain.com/Folder1/
Internal Admin Website: www.domain.com/Folder2/Version1/
In Folder2 you should add the following line to .htaccess
RewriteRule .* - [L]
This rewrite rule takes precedence over the .htaccess in the parent directory.

simple subdomain set up for local development (htaccess?)

I am trying to set up a website, domain
examplesproject.co.uk
with a subdomain which is for the moment called
sub.examplesproject.co.uk .
I am with Bluehost and so I have set up the subdomain and I have got the same document root both for examplesproject.co.uk and sub.examplesproject.co.uk but I want content for
sub.examplesproject.co.uk
to actually be located at
examplesproject.co.uk/sub .
So why didn't I set up the document root at examplesproject.co.uk/sub for the sub-domain? Because then, in local development I would need to treat the two domains as completely separate and that would mean no relative urls which seemed silly given that the subdomain folder is just tantalisingly inside the main domain.
However, if someone browsed to sub.examplesproject.co.uk they would get the same content as examplesproject.co.uk, which I don't want, so I set up htaccess rewrite in the root folder like so:
#rewite sub-domain to sub directory
RewriteCond %{HTTP_HOST} ^[www\.]*sub.examplesproject.co.uk [NC]
RewriteCond %{REQUEST_URI} !^/sub/.*
RewriteRule ^(.*) /sub/$1 [L]
That works, however if you browse to examplesproject.co.uk/sub you can still see the content and I don't want to have two locations for the same content. However if I rewrite this sub-directory to show the sub-domain in the browser address, then I create a loop where it keeps feeding round.
As another side-point, I want my main site to be forced to use www, so I also have the following in my root htaccess..
#force add www on main domain
RewriteCond %{HTTP_HOST} ^examplesproject.co.uk$
RewriteRule ^(.*)$ http://www.examplesproject.co.uk$1 [R=301,L]
However, I want to force the sub domain not to have a www infront i.e. http://sub.examplesproject.co.uk and NOT http://www.sub.examplesproject.co.uk. To do this I am trying the following but it doesn't seem to work for other directories within the sub-domain (if that makes sense). Anyhows this is the code which I put in the sub directory (ie at examplesproject.co.uk/sub):
#force remove www on sub-domain
RewriteCond %{HTTP_HOST} ^www.sub.examplesproject.co.uk [NC]
RewriteRule ^(.*)$ http://sub.examplesproject.co.uk/$1 [L,R=301]
Thought I'd mention in case it affects things.
So, my question is, how do I achieve a nice set-up where:
I can use relative URLs for developing and implementing my subdomain.
browsing to http://sub.examplesproject.co.uk shows the content of http://www.examplesproject.co.uk/sub
browsing to http://www.examplesproject.co.uk/sub doesn't duplicate the sub-domain by showing the content (for SEO purposes)
and also
Main domain examplesproject.co.uk is forced to use www - http://www.examplesproject.co.uk.
Sub domain sub.examplesproject.co.uk is force NOT to use www - http://sub.examplesproject.co.uk.
If anyone can help, I would be really grateful. By the way, locally I have set up virtual hosts http://examplesproject and http://sub.examplesproject using wamp and hosts file to replicate the online behaviour.
Thanks alot for reading. Answers/suggestions welcome.
Sorry about that Tim Post! I have put the content in this time! Nice one for looking at this. Hope that this helps someone.
This is the solution that worked for me (thanks to Jim (jdMorgan) at webmasterworld for this - http://www.webmasterworld.com/apache/4254301.htm)..
Put all of these rules, in this order, into the root .htaccess:
# Externally redirect direct client requests for test subdomain subdirectory paths to the test subdomain
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /sub/([^\ ]*)\ HTTP/
RewriteRule ^sub/(.*)$ http://sub.examplesproject\.co\.uk [R=301,L]
#
# Externally redirect all non-canonical, non-blank, non-test-subdomain hostname requests to canonical "www" main domain
RewriteCond %{HTTP_HOST} !^(www\.examplesproject\.co\.uk)?$
RewriteCond %{HTTP_HOST} !^([^.:]+\.)*sub\.([^.:]+\.)*examplesproject\.co\.uk [NC]
RewriteRule ^(.*)$ http://www.examplesproject.co.uk$1 [R=301,L]
#
# Externally redirect non-canonical subdomain hostname requests to canonical test subdomain
RewriteCond %{HTTP_HOST} ^([^.:]+\.)*sub\.([^.:]+\.)*examplesproject\.co\.uk [NC]
RewriteCond %{HTTP_HOST} !^sub\.examplesproject\.co\.uk$
RewriteRule ^(.*)$ http://sub.examplesproject.co.uk/$1 [R=301,L]
#
# Internally rewrite sub-domain requests to subdirectory path
RewriteCond %{HTTP_HOST} ^sub\.examplesproject\.co\.uk$
RewriteCond $1 !^sub/
RewriteRule ^(.*)$ /sub/$1 [L]
Checking THE_REQUEST in the now-first rule prevents the infinite redirection loop problem you encountered.
Note that exact hostnames are now enforced due to the very-careful use of case-sensitivity and anchoring.
Nice one aiit!

Resources