How to hide subfolder's subfolder from htaccess? - .htaccess

I've done something in my .htacces to remove www and /example.com/ subfolder.
It worked fine, but when I tried to access the real subfolder, it redirects me to full url
The valid urls I want are;
example.com
example.com/app
example.com/admin
If i hit example.com it works fine,but when I hit example.com/app, it redirects to example.com/example.com/app
I've tried this way, but still doesn't work
this is my .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteRule !^example.com/ /example.com%{REQUEST_URI} [L]
btw, my folder structure
public_html
└ .htaccess
└ example.com
└ app
└ admin

Related

Redirect to other domain except one subdirectory which needs to be redirected to a subdomain

I need to redirect a domain to other domain. However, one subfolder needs to be redirected to a subdomain. For example:
http://old.com/archives/ -> http://new.com/archives/
http://old.com/blog/archives/123 -> http://blog.new.com/archives/123
I added the following in .htaccess under the root folder of the old domain:
#Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/blog/(.*)$ http://blog.new.com/$1 [R=301,L]
RewriteRule ^(.*)$ http://new.com/$1 [R=301,L]
It works for all URLs except the subdomain blog.
For most URLs with the subdomain blog, it redirects to http://new.com/.
In this case, how can I edit it?
Try with :
RewriteEngine on
RewriteRule ^blog/(.*)$ http://blog.new.com/$1 [R=301,L]
RewriteRule ^(.*)$ http://new.com/$1 [R=301,L]
Because htaccess RewriteRule left url does not begin with /

301 REdirect not working for subdirectories

I have a directory /mydirectory and all its contents that's giving Errors 404 even with REdirect rules in place, when I try to redirect it to the root of www.anotherdomain.com. I don't have matching files or subdirectories in www.anotherdomain.com, so I just want it to move to the root. Right now what's working is if both www. and non-www versions of /mydirectory Redirect perfectly, but any subdirectory like /mydirectory/anysubdirectory displays a 404 not Found Error. I'm trying to fix that ... I did this in an .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^mydirectory/$ http://www.anotherdomain.com/ [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^mydirectory/$ http://www.anotherdomain.com/ [R=301,NC,L]

exclude main www domain from cpanel wildcard subdomain

I have added a subdomain "*" like *.mydomain.com to my cPanel account. with a directory name subdomainsystem.
Now every subdomain works fine and loading script from subdomainsystem directory and if I go to www.mydomain.com it was showing main root script (main site). But after few days if I go to www.mydomain.com, it is being considered as a subdomain as well.
Here is my .htaccess in root:
RewriteCond %{HTTP_HOST} !^mydomain.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).mydomain.com [NC]
RewriteRule ^([a-z0-9-]+)/$ index.php?subdomain=%2&menu=$1 [QSA]
Thanks.
You need to tweak regex to disallow www also before main domain:
RewriteCond %{HTTP_HOST} !^(?:www\.)?mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.mydomain\.com$ [NC]
RewriteRule ^([a-z0-9-]+)/?$ index.php?subdomain=%2&menu=$1 [L,QSA]

Non-www to www rewrite htaccess on shared hosting

I have a shared hosting plan so my website is located in: /public_html/example.com/
The .htaccess is located in the root folder /public_html/
I want to redirect example.com to www.example.com
so far, I've used
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
When I use my browser on example.com/test/ it directs to www.example.com/example.com/test/
How can I fix that?

Prevent access of sub-/addondomain via 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]

Resources