Prevent access of sub-/addondomain via htaccess - .htaccess

I have 2 domains: main.com and addon.net
On my shared hosting account I create an addon-domain foraddon.net which automatically creates a folder in the main-domain's directory as well as a subdomain.
I want to change the accessability of the addon domain via the maindomain:
http://addon.main.com
http://main.com/addon.net/
Now both serve the index.html from addon.net
Both URLs should result in a "404 - not found" error.
What I have right now on main.com/.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?main.com$ [NC]
RewriteCond %{REQUEST_URI} ^/addon.net/(.*)$
RewriteRule ^(.*)$ 404.html [L]
And in addon.net/.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.main.com$ [OR]
RewriteRule ^(.*)$ http://www.main.com/ [R=301,L]
And now everything redirects to main.com:
http://main.com/addon.net/ redirects to http://www.main.com
http://addon.main.com redirects to http://www.main.com
http://addon.net redirects to http://www.main.com
My question: which rules should I add to which .htaccess-file in order to get the desired results:
addon.main.com redirecting to main.com/404.html
main.com/addon.net redirecting to main.com/404.html
addon.net serving addon.net/index.html

If you want to redirect access from anything except addon.net (and redirect the other requests to a 404 page), all you need to do is use these lines in the .htaccess file inside addon.net :
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?addon\.net$ [NC]
RewriteRule - /404.html [L]

Related

How can I exclude one URL from redirection in OpenLiteSpeed

My application webroot is like : /home/username/appname/public_html
I have created a subfolder under public_html and installed the wordpress there.
Like: /home/username/appname/public_html/subfolder
we used the below redirection code because we want to access the application like https://example.com/subfolder
RewriteEngine on
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://example.com/br/$1 [L,R=301,NC]
RewriteRule ^/?(.*)$ http://example.com/br/$1 [L,R=301]
so when user will access main domain example.com so req will be redirected to example.com/subfolder/
but now we want to access example.com/file.txt
but the problem is due to redirection it also redirecting
so how can I exclude this url example.com/file.txt from redirection to example.com/subfolder/file.txt
Please help us.
I tried to add these config but did not work for me
RewriteCond %{REQUEST_URI} !^/file\.txt$
OR
RewriteCond %{REQUEST_URI} !^example\.com\/file\.txt$

.htaccess rewriterule does not redirect

My website is located in a subfolder (gng2) of my public_html folder. I used the second part (##2) of the below .htaccess file to rewrite the url to add the gng2 subfolder to the URI. It works fine, the app loads when I enter my url to the browser.
Now I added the first part (##1)to redirect any requests where the url contains the subfolder as well. I want www.staging.gonativeguide.com/gng2/en to be redirected to www.staging.gonativeguide.com/en. However this does not work: the first url does not get redirected to the second. Checking the below code on htaccessTester, it says it is correct and it should redirect.
My websited is hosted by a shared hosting service and I think the web server is nginx. Any idea why the redirect does not work?
RewriteEngine On
## 1. redirect request when it contains the gng2 subfolder.
RewriteCond %{HTTP_HOST} ^staging.gonativeguide.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.staging.gonativeguide.com$
RewriteCond %{REQUEST_URI} gng2/
RewriteRule gng2/(.*) www.staging.gonativeguide.com/$1 [R=301,L]
## 2. rewriting url to add the gng2 subfolder containing the app.
RewriteCond %{HTTP_HOST} ^staging.gonativeguide.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.staging.gonativeguide.com$
RewriteCond %{REQUEST_URI} !gng2/
RewriteRule (.*) /gng2/$1 [L]
There are a number of issues with your current attempt. So I sketched an alternatate version which simplifies the rules and enhances robustness:
RewriteEngine On
## 1. redirect request when it contains the gng2 subfolder.
RewriteCond %{HTTP_HOST} ^(www\.)?staging\.gonativeguide\.com$
RewriteRule ^/?gng2/(.*)$ https://www.staging.gonativeguide.com/$1 [R=301]
## 2. redirect request that do not contain the "www" prefix in the host name.
RewriteCond %{HTTP_HOST} ^staging\.gonativeguide\.com$
RewriteRule ^ https://www.staging.gonativeguide.com%{REQUEST_URI} [R=301]
## 3. rewriting url to add the gng2 subfolder containing the app.
RewriteCond %{HTTP_HOST} ^(www\.)?staging\.gonativeguide\.com$
RewriteCond %{REQUEST_URI} !^/gng2/
RewriteRule ^ /gng2%{REQUEST_URI} [END]
You need to make absolutely sure that you are not looking at cached results when testing. So always test using a fresh anonymous browser window and use "deep reloads" (CTRL-F5 typically) instead of just reloading.

Redirect to homepage if file or directory doesn't exist using .htaccess

I am Hosting a domain on free hosting site 000webhost, I want to redirect all files or folder that doesn't exist to my homepage. The Code, I have used is:
RewriteEngine on
rewritecond %{http_host} ^domain\.com$ [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,l]
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.html
It works, but the url doesn't change to homepage. Like if I am trying to open www.domain.com/blaa it redirects me to home page but the URL is still www.domain.com/blaa. I want it to be changed to www.domain.com.
If I am trying to open a valid directory, which exist in my root folder, it also redirects to homepage. Like if I am trying to open www.domain.com/folder, it should result in files and directories in that folder, instead of that it redirects me to homepage and web address is still same like www.domain.com/folder.
Also I want to keep using the above 2 rules to open website with WWW and index.html with www.domain.com
Please help me.
Thanks in advance.
As per your questions's title, to redirect to homepage if the request is not for a file or dir, you can use :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ / [L]
You can also use ErrorDocument directive to rewrite non-existent requests to homepage
ErrorDocument 404 /

.htaccess rewriterule giving a 500 error

I'm using virtual subdomains to catch all traffic to my site and using the subdomain as an identifier in an app. All traffic to a non-subdomain URL should use index.html; but I'm not having any luck getting the redirect to send subdomain traffic to the correct page. So far I've got:
suPHP_ConfigPath /var/sites/w/domain.org/
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.org$ [NC]
RewriteCond %{HTTP_HOST} !^domain.org$ [NC]
RewriteRule ^(.*)$ /sub.html
The index page loads on a domain URL, but a subdomain URL gets a 500 error, instead of redirecting to http://sub.domain.org/sub.html (note, it's important that I keep the subdomain in the redirected URL).
Can anyone point me in the right direction?
Try including a condition to check if the URI is already /sub.html
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.org$ [NC]
RewriteCond %{HTTP_HOST} !^domain.org$ [NC]
RewriteCond $1 !^sub\.html
RewriteRule ^(.*)$ /sub.html

2 domains, htaccess redirect one to index.html but not the other

I'm working on a site that has 2 domains pointing to the same public_html folder.
The server server index.html by default. My code uses htaccess to route all calls to index.php but I only want this to happen for the development URL for now and not the live URL.
I tried routing live.com and www.live.com to www.live/index.html - but I get a redirect loop as I don't know how to make it break if index.html is in the URL.
Can anyone help?
I tried:
# Disable live domain
RewriteCond %{HTTP_HOST} ^live.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.live.com$
RewriteRule (.*)$ http://www.live.com/index.html [L]
Thanks!
# Disable live domain
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?live\.com$
RewriteCond $1 !^index\.html
RewriteRule ^(.*)$ http://www.live.com/index.html [L]

Resources