Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite: domain > folder redirection with trailing slashes

         

theanorak

5:15 am on Feb 21, 2010 (gmt 0)

10+ Year Member



Hi

I have several domain names which point to a single hosted space. I'd like to use mod_rewrite so that each domain name pulls content from a different folder on the hosting account. I'm most of the way there but have two problems which I'm struggling with. Firstly, the rewrite code, which is duplicated (with necessary amendments) for each domain.

Options +FollowSymLinks
RewriteEngine On

#exampleone.com
RewriteCond %{HTTP_HOST} ^(www\.)?exampleone.com$ [NC]
RewriteCond %{REQUEST_URI} !^/exampleone_folder/(.*)
RewriteRule ^(.*) /exampleone_folder/$1 [L]


This works, but with two caveats.

1) This redirect makes an exception for
http://exampleone.com/exampleone_folder
in that it *doesn't* redirect in that case. I still want the redirect to happen, and I think it's the REQUEST_URI line that needs to change, but nothing I've tried has worked.

2)
http://exampleone.com/subfolder/
redirects how I'd expect it to.
http://exampleone/com/subfolder
(no trailing slash) doesn't work the same way. There's plenty of example code for fixing this in .htaccess files, but I'm struggling to apply that to my redirects.

Any help or suggestions would be wonderful.

jdMorgan

3:58 am on Feb 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The modification in the first line of code below may fix your problem.

But make your life simpler: Host all of your "add-on" domains in a subdirectory -- I'll call it "/add-ons". Then you only need one rule:

Options +FollowSymLinks -MultiViews
RewriteEngine On
#
# Internally rewrite all "add-on" domains to subdirectories under "/add-ons"
RewriteCond %{HTTP_HOST} !^(www\.)?your-main-domain\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+\.([a-z]{3,6}|co\.[a-z]{2}))\.?(:[0-9]+)?$
RewriteCond %{REQUEST_URI} !^/add-ons/
RewriteRule ^(.*)$ /add-ons/%2/$1 [L]

This "hosts" exampleone.com in the subdirectory /add-ons/exampleone.com/
and "hosts" exampletwo.co.uk in the subdirectory /add-ons/exampletwo.co.uk/

The negative-pattern-match RewriteCond examining the REQUEST_URI in both this code and your original serves the critical function of preventing an infinite loop in a .htaccess context.

Without it, for example, your original rule would rewrite exampleone.com/hello.html to /exampleone-folder/hello.html, and then rewrite that result again to /exampleone-folder/exampleone-folder/hello.html, and then to /exampleone-folder/exampleone-folder/exampleone-folder/hello.html, etc. -- continuing until the server gave up and threw a 500-Server Error.

So, the same technique is used in the single-rule solution, with the added benefit of having to check only for that common "/add-ons" URL-path-prefix to prevent the looping, no matter how many domains you host below the "/add-ons" subdirectory.

Jim

theanorak

10:44 am on Feb 22, 2010 (gmt 0)

10+ Year Member



That's a considerably more elegant solution than duplicating code blocks for each new domain -- to say nothing of the loop problem. Thank you!

I do still have one problem, which may or may not be solvable. Assuming that there's an "aboutus" folder in the correct place for an add-on domain,

http://example.com/aboutus/
just works.

http://example.com/aboutus
(no trailing slash) redirects the browser to
http://example.com/add-ons/example.com/aboutus/


It still loads the index file in the "aboutus" folder, but the URI isn't ideal. Is that something that's fixable? Obviously I can make sure that internal links all end in a trailing slash but anything else is out of my control...

jdMorgan

1:07 pm on Feb 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You likely have your rules in the wrong order and/or have mixed directives from multiple modules.

Do not mix mod_alias Redirect/RedirectMatch directives with mod_rewrite directives; If you use mod_rewrite for any redirects or rewrites, use it for all redirects and rewrites.

Order your rules with all external redirects first, in order from most-specific (fewest URLs affected) to least-specific, followed by all internal rewrites, again in order from most- to least-specific.

Note that this applies across all server config files and .htaccess files in any/all subdirectories: In order to avoid exposing internally-rewritten filepaths to clients as URLs, all redirects must execute first, before any internal rewrites can execute.

Jim

theanorak

12:53 am on Feb 23, 2010 (gmt 0)

10+ Year Member



Interesting. I'll have to check with my host to see what rules they have set globally, as the only content in my .htaccess at the moment is your rule above (adjusted with the appropriate specific domain/folder names).

Thanks again.

jdMorgan

2:52 pm on Feb 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Check your "control panel" if you have one. Control panels are scripts that can 'edit' your server config files in a limited fashion, so if you've set any options or ticked any boxes in a control panel, then that may have created code that must be considered when evaluating rule-invocation order. Some control panels will even show you the code (It's generally awfully-written).

When dealing with a site that uses a control panel, I tend to disable (not use) any control panel options unless there's no other way to do what needs to be done. For those things that I can do myself in .htaccess, that's where I do them. In this way, I can strictly control the redirect/rewrite directive use and execution order.

Jim