Allow URL access, disallow subdirectory access - .htaccess

I have a second website stored in the root of my main website. It's a subdomain with its own URL
I want users to access the second website via a URL eg
www.mysecondwebsite.com
but not via the first website eg
www.myfirstwebsite.com/mysecondwebsite
Can this be done via .htaccess? If so where do I put the .htaccess file: in the root of the main site or the root of the subdirectory/domain?
Thank you.

At the top of the htaccess file in your mysecondwebsite folder, add this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !mysecondwebsite\.com$ [NC]
RewriteRule ^ - [L,R=404]
You can also replace R=404 with F if you'd rather return a 403 Forbidden.

Related

remove subdirectory from url, keep root accessible

I just want to remove the subdirectory from the URL but I still want the root directory to be accessible and even fetch the index.php from root when I go directly to the page.
I am currently using the following code:
RewriteCond %{HTTP_HOST} ^(www\.)?website\.com$
RewriteRule !^backup/ /backup%{REQUEST_URI} [L]
But this treats the subdirectory like its root.
Thanks

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 and rewriting my URLs

I'm trying to organise my websites with .htaccess, and I want to following configuration:
The URL of the main site has to be beta.example.com/portal/
The other sites should be in beta.example.com/othersite/
If users go to beta.example.com, they should be redirected 301 to
beta.example.com/portal/
beta.example.com/portal (without the trailing slash) should redirect to beta.example.com/portal/
/portal/ is not a physical file path on the server. I want all these sites to
be in a folder together, say /html/www/websites/
The folder the portal resides in, should not be located in
/html/www/websites/portal/, but I want to be able to switch the live
site to a new version (in a different folder) on the server, quickly,
invisible, without any downtime. This goes for all sites by the way.
I'm currently working with two or three .htaccess files: one in the root (www.), which directs beta.example.com to the folder /websites/
#RewriteBase / #should I use rewritebase or not?
/RewriteCond %{HTTP_HOST} ^beta.example.com$
RewriteRule (.*) websites/$1 [L,NC]
A .htaccess file is in the folder /websites/, and maybe another one or maybe not in the folder a site resides in.
RewriteBase /websites # or, not...
# rewrite the active site to the folder /portal_v1.0/
RewriteCond %{REQUEST_URI} ^/portal/
RewriteRule (.*) portal_v1.0/$1 [L]
# rewrite the root to /portal/
RewriteRule ^$ http://beta.example.com/portal/ [L,NC]
However, I still can't get this to work exactly the way I want. One problem is what beta.example.com/portal redirects to www.example.com/websites/portal/ , I don't want that. Help! Thanks in advance!

.htaccess - RewriteRule from subfolder to root without redirecting

I'm looking for something in .htaccess to change
subdomain.website.com/page/title
to
website.com/subdomain/title
without redirecting to the website and while keeping the URL in the address bar.
This .htaccess gets called in a subfolder. ublic_html/subdomain. So how do I get back to that folder public_html without using a redirect.
Thanks :)
Here is one way to do it using a proxy, which will leave the URL in the users address bar unchanged.
RewriteCond %{HTTP_HOST} ^subdomain\.website\.com$ [NC]
RewriteRule ^page/title$ http://website.com/subdomain/title [P,NC]

Need to show subdomain as url than the directory path

I have created a subdomain yesterday as www.subdomain.example.com. This has created a directory at the root by the name of subdomain. Now when i open www.subdomain.example.com, it gets redirected to www.example.com/subdomain. How to keep my url from showing the directory path i.e. in address bar it keep showing www.subdomain.example.com , while showing content from the directory subdomain.
I have a shared hosting account at hostgator.
Thanks for the help
In your .htaccess file, turn off the redirect and use mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.example\.com$
RewriteCond %{REQUEST_URI} !^/subdomain
RewriteRule ^(.*)$ /subdomain/$1 [L]
This checks if the requested host is either www.subdomain.example.com or subdomain.example.com makes sure the request doesn't already start with a /subdomain and appends it otherwise. This redirects internally on the server so your browser doesn't actually get redirected (address bar will still say: subdomain.example.com).

Resources