Forum Moderators: phranque
I've successfully set up a sub domain (courtesy of all here) with the following code:
RewriteCond %{HTTP_HOST} ^sub\.domain\.com
RewriteCond %{REQUEST_URI}!^/_sub/?
RewriteRule ^(.*)$ /_sub/$1 [L] What I'm now trying to do is stop people coming in directly on the sub-directory.
I tried the following 3 lines (immediately following the above - which I guess is irrelevant?) and it seems to have no impact.
RewriteCond %{HTTP_HOST} ^.*domain\.com
RewriteCond %{REQUEST_URI} ^/_sub/
RewriteRule ^/_sub/(.*)$ http://sub.domain.com/$1 [L] (Arguably I don't need the first line at all? as I only have one top level domain.)
Any ideas as to either what's wrong with the above (which will help me to learn) or an alternative.
Many thanks, Sam.
You can try this:
RewriteCond %{HTTP_HOST} ^sub\.domain\.com
RewriteCond %{REQUEST_URI} !^/_sub/?
RewriteRule ^(.*)$ /_sub/$1 [L]
#
RewriteCond %{HTTP_HOST} ^sub\.domain\.com
RewriteRule ^_sub - [F]
Due to the vagaries of mod_rewrite and the way that it is often re-run for inexplicable reasons, this may not work. If you have problems with it, you could also try using the [NS] (no subrequest) flag on the second rule, but I've never had much luck with it; Either it's not documented clearly, not implemented correctly, or I just don't understand it (the exact circumstances under which mod_rewrite would detect a subrequest are only generally described).
RewriteCond %{HTTP_HOST} ^sub\.domain\.com
RewriteCond %{REQUEST_URI} !^/_sub/?
RewriteRule ^(.*)$ /_sub/$1 [L]
#
RewriteCond %{HTTP_HOST} ^sub\.domain\.com
RewriteRule ^_sub - [NS,F]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com
RewriteCond %{REQUEST_URI} !^/_sub/?
RewriteRule ^(.*)$ /_sub/$1 [E=subdred:yes,L]
#
RewriteCond %{HTTP_HOST} ^sub\.domain\.com
RewriteCOnd %{ENV:subdred} !^yes$
RewriteRule ^_sub - [F]
If none of these work, then the best approach may be to simply make sure that your sudomain-support subdirectories are never "exposed" in the user's address bar or to a robot, and live with a few curiosity-seekers.
If you try these, please post your test results.
Jim
Thanks for this... I'm actually after something other than what you've suggested but I thought I'd test out your suggestions anyway for the learning and also I'm currently working with an empty site so if it brakes - so what... and it's about time I gave something back too :)
For the sake of simplicity I have the following basic stuff in the htacess (and nothing else other than the specifics shown for each test):
Options +FollowSymLinks
RewriteEngine on
# remove www (if it's there)
RewriteCond %{HTTP_HOST} ^www
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L] Your suggestions:
1) With the following:
RewriteCond %{HTTP_HOST} ^sub\.domain\.com
RewriteCond %{REQUEST_URI}!^/_sub/?
(note there is a space before the! but it's being stripped in the post!)
RewriteRule ^(.*)$ /_sub/$1 [L]
#
RewriteCond %{HTTP_HOST} ^sub\.domain\.com
RewriteRule ^_sub - [F] ...results...
url=domain.com = home page
url=sub.domain.com = 403 (which I guess is the recursion)
url=sub.domain.com/_sub = 403 (ditto)
url=domain.com/_sub = takes me to domain.com/_sub (not what I wanted)
url=foo.domain.com/_sub = takes me to foo.domain.com/_sub (not what I wanted)
(I have a wildcard sub domain set up and there is no foo sub domain)
2) your second suggestion results in exactly the same as 1A above
3) your third suggestion also results in exactly the same as 1A above
(At least they are all consistent!)
1B) So I changed the second condition group to a 'not' as follows:
RewriteCond %{HTTP_HOST} ^sub\.domain\.com
RewriteCond %{REQUEST_URI}!^/_sub/?
RewriteRule ^(.*)$ /_sub/$1 [L]
#
RewriteCond %{HTTP_HOST}!^sub\.domain\.com
RewriteRule ^_sub - [F] ...results...
url=domain.com = home page
url=sub.domain.com = sub domain home page
url=sub.domain.com/_sub = takes me to sub.domain.com/_sub (not what I wanted)
url=domain.com/_sub = 403
url=foo.domain.com/_sub = 403
...but... what I originally wanted was to send the punter to the correct place - not to 403 them so...
4) I changed 1B to the following:
RewriteCond %{HTTP_HOST} ^sub\.domain\.com
RewriteCond %{REQUEST_URI}!^/_sub/?
RewriteRule ^(.*)$ /_sub/$1 [L]
#
RewriteCond %{HTTP_HOST}!^sub\.domain\.com
RewriteRule ^_sub/(.*)$ http://sub.domain.com/$1 [R=301,L] ...results...
url=domain.com = home page
url=sub.domain.com = sub domain pages
url=sub.domain.com/_sub = takes me to sub.domain.com/_sub (not what I wanted)
url=domain.com/_sub = sub domain pages
url=foo.domain.com/_sub = sub domain pages
(and specific pages within _sub etc are also working!)
Nearly there!
The only problem I have remaining therefore is when they request the _sub directory under the sub.domain it gives it to them (even though it's not really there).
I've tried all sorts of things to fix this and the results range from looping (which I kind of understand as _sub is being rewritten as _sub is being rewritten as _sub) to 500s :(
Why am I doing this? Because I have php code which occasionally crashes displaying the full path relative to /home and I'd rather keep the whole thing neat and tidy - I don't mind people knowing it's there but I don't want confusion caused by being able to access it as above sub.domain,com/_sub.
I'll try a few more and think a bit more but this is where I'm at.
Regards, Sam.
url=domain.com = home page
url=sub.domain.com = loops
url=sub.domain.com/_sub = loops
url=domain.com/_sub = loops
url=foo.domain.com/_sub = foo.domain.com/_sub (not what I expected)
I'm still working on it - I'll get back to you.
I have reached that point :(
So the best fit is:
RewriteCond %{HTTP_HOST} ^sub\.domain\.com
RewriteCond %{REQUEST_URI}!^/_sub/?
RewriteRule ^(.*)$ /_sub/$1 [L]
#
RewriteCond %{HTTP_HOST}!^sub\.domain\.com
RewriteCond %{REQUEST_URI} ^/_sub/?
RewriteRule ^_sub/(.*)$ http://sub.domain.com/$1 [R=301,L] The only undesirable result in this is that [sub.domain.com...] appears to find a directory that does not in fact exist (ie. sub.domain.com/_sub).
Any change to the above rules breaks the other (desirable) results.
I have certainly learnt a hell of a lot more about mod rewrite!
Well, what *should* happen if I request "sub.domain.com/_sub"?
(You could add a third ruleset to catch that case.)
Also, the second ruleset can be shortened, unless I'm missing some subtlety:
RewriteCond %{HTTP_HOST}!^sub\.domain\.com
RewriteRule ^_sub/(.*)$ http://sub.domain.com/$1 [R=301,L]
Jim
Well, what *should* happen if I request "sub.domain.com/_sub"?
The same as every other request for _sub... that is [sub.domain.com...]
(You could add a third ruleset to catch that case.)
What would that rule set be (pleeeease)? I've tried all sorts of things including the following:
RewriteCond %{HTTP_HOST} ^sub\.domain\.com
RewriteCond %{REQUEST_URI} ^/_sub/?
RewriteRule ^_sub/(.*)$ http://sub.domain.com/$1 [R=301,L] But this causes all tests (on the sub domain) to loop (exit, start again, exit type looping - not internal). I think I know where this is happening (exits group 1, starts again, exits group 3, starts again, exits group 1 etc) but I do not have the right terminology to describe it.
(I tried various other tricks like putting .htaccess files in the _sub directory, [S=x], [N], [C] all to no avail :()
Also, the second ruleset can be shortened, unless I'm missing some subtlety:RewriteCond %{HTTP_HOST}!^sub\.domain\.com
RewriteRule ^_sub/(.*)$ [sub.domain.com...] [R=301,L](The second RewriteCond pattern is functionally identical to the RewriteRule pattern, and so can be omitted.)
Sam