htaccess affecting addon domain - apache

I have hosted a few sites on a Hostgator server. I have an htaccess for the main domain, limiting the access from a certain set of IPs. the main domain is in the root folder.
I have another website in a sub-folder which is being affected by the main htaccess file for ip block. I want that the ip block only goes for the main domain and none of the addon domains.
Please advice. I have read a lot of similar threads but the solution mentioned hasn't worked. The solution I found was adding the following code to the root htaccess file -
RewriteCond %{HTTP_HOST} ^(www.)?addon_domain.com
Rewriterule .* - [L]
I did replace the addon_domain with the actual domain with the correct TLD. When I add this, I get a 500 error.

It may have been the simplest of solution but for some reason, I couldn't find the answer online. One of my friends suggested to just use allow from all in the htaccess of the addon domain and it now works. :)

Related

htaccess redirect folder to new domain index only

I have looked and looked and cant find the answer, I would greatly appreciate your help!!
I designed a website in a folder on a dummy domain, and forgot to add "noindex" and now its indexed, I need to redirect all pages in that folder to the index of the new domain.
example:
http: //dummysite/clientsfolder/
(I had to put space here because I can't post 2 links)
redirect to http://clientsnewdomain.com
all the code I have found redirects to http ://clientsnewdomain.com/clients folder, whether I place it in the /clientsfolder or the http://dummysite/
and then this results in a 404 page. Got into a mess here.
Also which is better to use to avoid this issue in the first place?
in
or a robots.txt?
Use that, in your /clientsfolder/.htaccess:
RewriteEngine on
RewriteRule ^(.*)$ http://clientsnewdomain.com/$1 [R=301,L]
OR in the root .htaccess:
RewriteRule ^clientsfolder/(.*)$ http://clientsnewdomain.com/$1 [NC,R=301,L]
The best place to avoid this issue in the first place is to use robots.txt.
But I prefer to use a folder protected by a password.

Site Redirection with htaccess creates an infinite loop

Currently we have a number of sites hosted in one GoDaddy account. Each site is inside a separate folder and their respective domains are bound to those folders. The problem is that our main site is in the root of the host and our primary domain is linked to this root. The problem with this setup is that if for example, one of my other sites is in a folder called "secondsite", I can reach the website by going to www.secondsite.com (which is fine) but ALSO by going to www.mainsite.com/secondsite, which we absolutely not want.
The idea is to move all the files of the main site to a folder of their own (let's call it "mainsite"). When I talked to GoDaddy they told me to do a 301 redirect to that folder. I have never worked with .htaccess but I looked up how to redirect and found that I needed to write this:
Redirect 301 / http://mainsite.com/mainsite
However if I do that when I try to access the website I get infinite redirection: http://mainsite.com/mainsitemainsitemainsitemainsitemainsitemainsitemainsitemainsitemainsitemainsitemainsitemainsite
I've looked online and tried to use other solutions, like
RedirectPermanent / http://mainsite.com/mainsite
but the effect is the same.
Not sure what I'm doing wrong.
When using Redirect, you're linking path-nodes together, So:
Redirect / /abc/
means, anything starting with / will go to /abc/, e.g.:
/ -> /abc/
/foo -> /abc/foo
/1/2/3/4/5 -> /abc/1/2/3/4/5
And thus, since you're redirecting back to the same host, the / captures everything and you've got an infinite loop.
Try using either RedirectMatch:
RedirectMatch 301 ^/(?!mainsite)(.*)$ /mainsite/$1
or us mod_rewrite:
RewriteCond %{HTTP_HOST} ^(www\.)?mainsite\.com$ [NC]
RewriteRule ^(?!mainsite)(.*)$ /mainsite/$1 [L,R=301]

Ignore rewriting a directory is not working properly

I have a weird situation where a rule is working on a different domain with the same host but it won't work on a specific domain.
I have 2x Bluehost accounts, they are a standard cheap shared hosting provider.
On account #1 I have a single hosted account which has 4 domain names. 1 domain is the primary domain (loads off the /public_html/ directory of the account) and 3 add-on domains which are just folders inside of /public_html/.
On account #1 I have this in my .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-d
That is located directly under where I ignore rewriting some file extensions and after that I have a bunch of url rewrite rules which are all working.
On account #1 this all works fine.
On account #2 I have the same exact setup but the add-on domains are throwing error 500s when accessing them, it also does not add error.log entries in the folder of the files and I tested the domain without rewriting and it works fine. They are accessible through standard urls (www.example.com) so it's not like it's a sub-domain or anything.
This add-on domain in this case is very basic. It's just a single page domain with no rewriting rules. If I delete the .htaccess from the primary domain then the sub domain works.
Well, that was quick. In the add-on domain I did NOT have an .htaccess file. I figured this would be ok because I wasn't doing any re-writing.
Apparently it does not work like that. I added an .htaccess to that add-on domain's folder and added this on top like usual:
RewriteEngine On
Now both domains are working as intended.

mod_rewrite to absolute path in .htaccess - turning up 404

I want to map a number of directories in a URL:
www.example.com/manual
www.example.com/login
to directories outside the web root.
My web root is
/www/htdocs/customername/site
the manual I want to redirect to is in
/www/customer/some_other_dir/manual
In mod_alias, this would be equal to
Alias /manual /www/customer/some_other_dir/manual
but as I have access only to .htaccess, I can't use Alias, so I have to use mod_rewrite.
What I have got right now after this question is the following:
RewriteRule ^manual(/(.*))?$ /www/htdocs/customername/manual/$2 [L]
this works in the sense that requests are recognized and redirected properly, but I get a 404 that looks like this (note the absolute path):
The requested URL /www/htdocs/customername/manual/resourcename.htm
was not found on this server.
However, I have checked with PHP: echo file_exists(...) and that file definitely exists.
why would this be? According to the mod_rewrite docs, this is possible, even in a .htaccess file. I understand that when doing mod_rewrite in .htaccess, there will be an automated prefix, but not to absolute paths, will it?
It shouldn't be a rights problem either: It's not in the web root, but within the FTP tree to which only one user, the main FTP account, has access.
I can change the web root in the control panel anytime, but I want this to work the way I described.
This is shared hosting, so I have no access to the error logs.
I just checked, this is not a wrongful 301 redirection, just an internal rewrite.
In .htaccess, you cannot rewrite to files outside the wwwroot.
You need to have a symbolic link within the webroot that points to the location of the manual.
Then in your .htaccess you need the line:
Options +SymLinksIfOwnerMatch
or maybe a little more blindly
Options +FollowSymlinks
Then you can
RewriteRule ^manual(/(.*))?$ /www/htdocs/customername/site/manual/$2 [L]
where manual under site is a link to /www/customer/some_other_dir/manual
You create the symlink on the command line with:
ln -s /www/htdocs/customername/site/manual /www/customer/some_other_dir/manual
But I imagine you're on shared hosting without shell access, so look into creating symbolic links within CPanel,Webmin, or whatever your admin interface is. There are php/cgi scripts that do it as well. Of course, you're still limited to the permissions that the host has given you. If they don't allow you to follow symlinks as a policy, you cannot override that within your .htaccess.
AFAIK mod_rewrite works at the 'protocol' level (meaning on the wire HTTP). So I suspect you are getting HTTP 302 with your directory path in the location.
So I'm afraid you might be stuck unless.. your hosting lets you follow symbolic links; so you can link to that location (assuming you have shell access or this is possible using FTP or your control panel) under your current document root.
Edit: It actually mentions URL-file phase hook in the docs so now I suspect the directory directives aren't allowing enough permissions.
This tells you what you need to know.
The requested URL /www/htdocs/customername/manual/resourcename.htm
was not found on this server.
It interprets RewriteRule ^manual(/(.*))?$ /www/htdocs/customername/manual/$2 [L] to mean rewrite example.com/manual/ as if it were example.com/www/htdocs/customername/manual/.
Try
RewriteRule ^manual(/(.*))?$ /customername/manual/$2 [L]
instead.

Mod Rewrite: How do you rewrite out of the URL's home directory?

I have a directory structure on our VPS like this:
/www
/site_one
/site_two
And going to www.siteone.com brings you to /www/site_one/ on the server. I'd like to use mod_rewrite to point a request for www.siteone.com/thing/ to the directory /www/site_two/thing.
I've tried a basic rewrite like:
RewriteRule ^page.html$ /www/site_two/new_page.html
but / refers to /www/site_one/
Is there a way to get it to serve the page from the directory I'd like?
EDIT
To answer the questions below:
#Ignacio I'm not sure if I left anything important out. Brand new to mod_rewrite.
#outis: Yes both sites are virutal hosts. www.site_one.com is mapped to /www/site_one/ and www.site_two.com is mapped ot /www/site_two
Give a full URL, which implicitly redirects:
RewriteCond %{HTTP_HOST} (.*\.)site_one\.com$
RewriteRule ^/?(thing(/.*)?) http://%1site_two.com/$1
To achieve this without redirection, the documents in /www/site_two must be accessible via URLs in the site_one.com domain; a symlink from /www/site_one/site_two to /www/site_two might do it.