Forum Moderators: phranque

Message Too Old, No Replies

How to make root domain .htaccess not affect addon domain?

         

john5000

5:33 am on Oct 30, 2006 (gmt 0)

10+ Year Member



Hello, I'm fairly new to rewrite rules. Any help is greatly appreciated.

I have a main domain, and an addon domain, both are for wordpress blogs.

I would like to prevent the .htaccess from the main domain from affecting the addon domain (so I could have separate rewrite rules for the addon domain).

The way cpanel seems to work is that the addon domain is in a subdirectory (addon_folder/) inside the main domain's directory (public_html/).

---------------------------------

Additionally, what is the best way of preventing addondomain.com/ from being accessible from addon_folder.maindomain.com/ and maindomain.com/addon_folder/

(I was able to create a redirect for maindomain.com/addon_folder/ to maindomain.com/, but that's about it)

--------------------

Below is what my main domain's .htaccess file looks like right now (the top part is for my wordpress permalinks; bhg = name of the addon_folder; and cpanel tells me I have Apache 1.3.37):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule . /index.php [L]
</IfModule>

RedirectMatch permanent ^/bhg/$ [maindomain.com...]

jdMorgan

6:03 am on Oct 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just add a RewriteCond to your main domain's rule:

RewriteCond %{HTTP_HOST} !^(www\.)?addon_domain\.com

where addon_domain should be replaced by the actual domain name, and I'm assuming that both addon_domain.com and www.addon_domain.com exist and resolve to your server.

That RewriteCond will disable the (single) RewriteRule that follows it if the request is for the addon.

If you add more rules to the main domain's .htaccess file later, then you'll need to either add the above condition to each new rule, or create a 'bypass rule' at the top to skip the subsequent rules if the request is for the addon:


RewriteCond %{HTTP_HOST} ^(www\.)?addon_domain\.com
RewriteRule .* - [L]

This tells mod_rewrite to quit processing right here if the requested domain is addon_domain.com or www.addon_domain.com.

An alternative to all of the above is to add


RewriteOptions none

to your mod_rewrite code in the main folder of the addon domain. However, that will disable subdirectories of the addon domain from processing the addon domain's main .htaccess file, unless you specifically enable it by putting

RewriteOptions Inherit

in each and every one of the addon domain's subdirectories' .htaccess files. You'll have to decide which approach is more manageable/maintainable for your sites, now and for the future.

I tried to write the above carefully with respect to domains, subdomains, and subdirectories so read critically.

Jim

john5000

6:39 am on Oct 30, 2006 (gmt 0)

10+ Year Member



Jim, Thanks! I think this code did what I wanted to do:

RewriteCond %{HTTP_HOST} ^(www\.)?addon_domain\.com
RewriteRule .* - [L]

I added it to the top of the main domain's .htaccess file, and then the permalink rules nolonger applied to the addon domain. Then I created a separate .htaccess file (with it's own permalink rules) located in the addon domain's directory, and then the addon domain's permalink rules did apply to the addon domain.

(Btw, I already have wordpress plugins installed which redirect non-www to www, I'm not sure if that matters or not)

-----------------------

Now what is the best way of preventing addon_folder.maindomain.com/ and maindomain.com/addon_folder/ from being accessible.

I think I would like these URLs to generate the same type of error pages as would kujhdsfgj.maindomain.com and maindomain.com/kujhdsfgj/ where kujhdsfgj = some file that doesn't exist. Is this possible?

jdMorgan

2:27 pm on Oct 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's not possible to generate the same error, since arequest for undefined.example.com will result in an error at the DNS level, a full level above the HTTP response level that the server can generate, whereas example.com/addon does in fact exist in DNS.

However, a good fix is to externally redirect those direct client requests (only!) for the addon subdirectory back to the 'proper' addon domain URL:


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /addon_subdirectory/
RewriteRule ^addon_subdirectory/(.*)$ http://www.addon-domain.com/$1 [R=301,L]

The apparent redundancy in the code is to specifically detect direct requests from the client (browser or robot) for the addon subdirectory. This is necessary, both to prevent an infinite redirection loop, and to preserve the function of the code that rewrites the addon domain to the subdirectory of the main domain. Without the client request check, a simple redirect rule would countermand the currently-installed addon support code, and the two would repeatedly undo each other's last action, resulting in an 'infinite' rewrite/redirect loop, and eventually, a server error.

There's a dependency here on precisely how your host accomplishes the addon-to-subdirectory mapping function. But if anything is going to work, the approach above is it. This code should be added into your main domain's .htaccess file, after the bypass code we discussed previously.

To explain the byzantine regex pattern in the RewriteCond, be aware that what we are matching in the first line is the actual request received from the browser, which will look like this:

GET /addon_subdirectory/somefile.abc HTTP/1.1

or
PROPFIND /addon_subdirectory/otherfile.abc HTTP/1.1

Jim

john5000

2:24 am on Oct 31, 2006 (gmt 0)

10+ Year Member



Ok, Jim I think I read your last post about 15 times and I still only understand about half of it, lol! (but I suspect your explanations are for other readers too). Nevertheless, I added your code under the bypass code as described, but I didn't see any change with any redirects.

------------------

BUT, I discovered that I can create a subdomain redirect from within cpanel. So I created a redirect for addon_subdirectory.maindomain.com/ to go to www.maindomain.com/. This works perfectly. The code generated is below (it was added to the .htaccess in the addon subdirectory):

RewriteEngine on
RewriteCond %{HTTP_HOST} ^addon_subdirectory.maindomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.addon_subdirectory.maindomain.com$
RewriteRule ^(.*)$ [maindomain.com...] [R=301,L]

-------------------

Now I just have one more problem.

For some reason, www.addon_domain.com/addon_subdirectory/ is redirecting to www.main_domain.com

I think this is due to the redirect inside the .htaccess in the main domain's directory, which is now:

RedirectMatch permanent ^/addon_subdirectory/$ [maindomain.com...]

Should this be changed to something like:

RedirectMatch permanent ^maindomain/addon_subdirectory/$ [maindomain.com...]

DisneySubs

8:02 pm on Nov 6, 2006 (gmt 0)

10+ Year Member



Hello!

I'm brand new and I'm having a similiar problem.. My web hosts allows multiple domains in one hosting account, but the .htaccess folder in the root directory for the primary domain is preventing access to any of the subfolders. How can I change this?

Here is my code:


# Protect files
<Files ~ "^(.*)\.(inc¦inc\.php¦tpl¦sql)$">
Order deny,allow
Deny from all
</Files>

# Protect directories
<Files ~ "^(backup¦files¦images¦include¦lang¦libs(/.+)?¦temp(/.+)?¦templates(/.+)?¦javascripts(/.+)?)$">
Order deny,allow
Deny from all
</Files>

# Disable directory browsing
Options -Indexes

# Follow symbolic links in this directory
Options +FollowSymLinks

# Override PHP settings that cannot be changed at runtime
# (If your server supports PHP settings via htaccess you can comment following two lines off)
# php_value register_globals 0
# php_value session.auto_start 0

# Customized error messages
# ( If you are running in a subfolder please add it, example: "directory/index.php?httpstatus=404" )
ErrorDocument 404 index.php?httpstatus=404

# Set the default handler
DirectoryIndex index.php

# URL rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine On

## Details Link Page Rewrite##
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteCond %{REQUEST_FILENAME}!-l
RewriteRule (.*)detail/link-(.*).htm[l]?$ detail.php [QSA,NC]

## Pagination Rewrite
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteCond %{REQUEST_FILENAME}!-l
RewriteRule (.*)page-(\d+)\.htm[l]?(.*)$ $1/?p=$2 [PT,NC]

## Category redirect
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteCond %{REQUEST_FILENAME}!-l
RewriteRule ^(.*)$ index.php [QSA,L]

</IfModule>

Chad

john5000

11:34 pm on Nov 6, 2006 (gmt 0)

10+ Year Member



I can tell you the folloing bit of code that Jim provided me was successful at preventing the code in the root domain's .htaccess from being applied to the addon domains .htaccess file:

RewriteCond %{HTTP_HOST} ^(www\.)?addon_domain\.com
RewriteRule .* - [L]

I just slapped that code at the top of the root domain's .htaccess file, and inputted the appropriate domain name for addon_domain, and it prevented the rewrite rules from applying to the addon domain.

Just a disclaimer... I don't know exactly how all these codes work and I'm basically just copying and pasting as I go... your hosting account may be set up differently than mine.

DisneySubs

12:13 am on Nov 7, 2006 (gmt 0)

10+ Year Member



Where in your .htaccess file did u put that code? Can you show me the code with a real domain in it so I can see how it's supposed to look?

Thanks!

john5000

10:02 am on Nov 7, 2006 (gmt 0)

10+ Year Member



I added that code to the very top of the main domain's .htaccess file because I didn't want any of the subsequent rules to apply to the addon domain.

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule .* - [L]

The above code would be for if example.com was the addon domain.

----------

If you scroll up, you can see Jim (jdMorgan) describe the code as follows:

"... create a 'bypass rule' at the top to skip the subsequent rules if the request is for the addon:

RewriteCond %{HTTP_HOST} ^(www\.)?addon_domain\.com
RewriteRule .* - [L]

This tells mod_rewrite to quit processing right here if the requested domain is addon_domain.com or www.addon_domain.com."