How to block addon domain directory access - .htaccess

I have a primary domain in a hosting. Let's say that domain is example.com
Then I added a new domain on that hosting. Let's say the new domain is addressed at example2.com
So in cPanel generate:
/home/index.php
/home/example2.com/dir/
If someone accesses my site like this: example.com/example2.com/
Then the displayed is my site that is located at example2.com
How to block access example.com/example2.com/ and example.com/example2.com/dir/file/test.html
But if the site example2.com is accessed directly does not raise a problem?
I hope there is a solution using .htaccess or something like that.

You can try using an .htaccess
# Redirect example.com/example2
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^example2\.com$ "https\:\/\/example\.com\/" [R=301,L]
OR
# Return a 403 response for example.com/example2
RewriteEngine On
RewriteRule ^example2\.com$ - [R=403,NC,L]

Related

htaccess file - root and website folder htaccess file both have different code. URL does not work without the www

I have the following domain that does not work without the www.
wwww.mydomain.com works
mydomain.com = does not work
The root htaccess file (primary domain is something else) automatically seemed to have this code in it.
# php -- END cPanel-generated handler, do not edit
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$
RewriteRule ^/?$ "https\:\/\/www\.mydomain\.io\/mydomain" [R=301,L]
I added this code in the website's own htaccess file.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
What am I doing wrong?
Currently mydomain.com goes to something quite strange (it duplicates the domain name): and has an error
https://www.example.com/example.com/
Error on page
Not Found
The requested URL was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Can I delete the code which says "do not edit". Which one is working and why/how? A fix with an explanation would be appreciated.
The issue was basically resolved by:
Adding the rewrite engine rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Deleting the forwarding in the DNS management console and ONLY leaving in the forwarding in the addondomains section. So, in the cpanel - addon domains, create forwarding and delete the forwarding in the DNS section. This was the key part.
There was an additional folder outside of the public_html for this domain and that was deleted as well.
Finally, adding these rules to force https was useful:
https://www.hostinger.co.uk/tutorials/ssl/forcing-https
Also noting the helpful comment: "Which web server are you using? Look through your web server configuration files. Your problem is that you have a redirect from HTTP to HTTPS and the path URL is configured wrong."

How can I stop processing of non-existent domains?

Our website is allowing any prefix/subdomain before the domain.
So if our site is www.domain.com, then the server is allowing;
www.anything.domain.com, where 'anything' can be literally anything, and it displays whatever is on the page that actually exists.
So, www.anything.domain.com/something.php displays the content that should only be accessible via www.domain.com/something.php.
Is there any way using .htaccess to stop this from happening, or redirect it to the version that does actually exist?
Or does this need to be done on the server?
Does anyone know why this is being allowed?
Ideally, this should be configured in server configuration files (also, you can configure DNS to simply not resolve unwanted hostnames, but that is for another question probably).
If you don't have access to server configuration, you can do it in .htaccess:
# to block access if any domain except example.com or www.example.com was used
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteCond %{HTTP_HOST} !=example.com
RewriteRule ^ - [F]
or
# if any domain except example.com or www.example.com was used,
# redirect the request to www.example
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteCond %{HTTP_HOST} !=example.com
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301]

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

Redirect www.example.com to example.com

I've a domain hosted on bluehost with iLister CMS installed in, the problem which I'm facing is http://www.advett.com/admin isn't responding because I registered advett.com/admin in the site_url field of iLister license.
How to redirect all the request for www.advett.com and www.advett.com/admin to advett.com and advett.com/admin respectively?
I know it can be done with .htaccess but I've not been able to find a solution till now.
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.advett\.com [NC]
RewriteRule ^(.*)$ http://advett.com/$1 [L,R=301]

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