Can't get apache mod_rewrites working when my site is in a subdirectory of public_html

100 Views Asked by At

I use a hostmonster account to host several websites. Each site lives in a subdirectory of my public_html folder in order to keep me sane when administering the sites. So for example my primary domain lives in public_html/joshorndorff.com

I have set up my .htaccess file in my public_html folder to redirect traffic into the appropriate subdirectory as suggested by https://my.hostmonster.com/cgi/help/347

# .htaccess main domain to subdirectory redirect
RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www.)?joshorndorff.com$
RewriteCond %{REQUEST_URI} !^/joshorndorff.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /joshorndorff.com/$1

RewriteCond %{HTTP_HOST} ^(www.)?joshorndorff.com$
RewriteRule ^(/)?$ joshorndorff.com/index.php [L]

Then in my subdirectory I use the drupal default .htaccess as uncomment the lines that redirect to the www. subdomain. The relevant lines from the subdirectory are:

RewriteCond %{HTTP_HOST} ^joshorndorff\.com$ [NC]
RewriteRule ^(.*)$ http://www.joshorndorff.com/$1 [L,R=301]

This all works fine, but there are two problems I still have.

  1. When I type www.joshorndorff.com/joshorndorff.com/index.php no rewriting happens and the domain name and subdirectory (which is named the same as the domain name) both show up in the address bar

  2. When I enter my static ip address (67.20.112.212) in the address bar I am not redirected to my site, but rather I see the index.html that I put in public_html for testing purposes/

I've read the excellent tutorial on mod_rewrite at http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/ but still can't get either of the two issues fixed. I would love any suggestions or working code, but even more would love to understand why RewriteRule ^joshorndorff.com/ / [NC]

does not do what I expect.

Thanks so much! -Josh Orndorff

1

There are 1 best solutions below

0
On

In the examples I provided below I replaced your domain with example.com.

# 1

The way you have the rewrite condition/rule written this is expected behavior. To prevent it from happening, you need to add another condition/rule set that matches the specific subdirectory and rewrites it. This must be added before your other rule within the Drupal .htaccess file in your subdirectory.

Otherwise, you're basically telling it to match the domain name, and pass everything else as your first variable: $1, including the subdirectory.

Here is an example using example.com/example.com/index.php:

RewriteCond %{HTTP_HOST} ^example\.com\/example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

# 2

Your second issue, browsing by IP, is happening because your rewrite rules only matches your domain name.

If you want to redirect the IP to your site's domain name, you would need match the host, in your case the IP address, then rewrite to your domain:

RewriteCond %{HTTP_HOST} !^(.*)$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

If you truly want browsing by IP to work, you could do something like this:

RewriteCond %{HTTP_HOST} !^(.*)$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

Please note that in addressing #2, you would have to adapt the rules I showed in #1 for use with IP address, and still match the subdirectory. Additionally, my examples aren't exact replacements for your rules, but should show enough for you to adapt it fairly easily. You might be able to just add your URI and file conditions to #1, but I did not test it specifically.