Forum Moderators: phranque

Message Too Old, No Replies

subdirectory to subdomain - what if they're in the same folder?

What if the subdomain/subdirectory share the same folder?

         

john010117

11:38 pm on Oct 27, 2008 (gmt 0)

10+ Year Member



I realize that there have been a lot of discussions on redirecting from subdirectory to subdomain using .htaccess. However, I have not been able to find exactly what I was looking for.

I want to redirect all requests (including the parameters after the URL) for

http://domain.com/sub/
to
http://sub.domain.com/
. The problem is that THEY SHARE THE FOLDER. I've tried multiple solutions, but they all failed. Any ideas?

jdMorgan

11:44 pm on Oct 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's not a problem, but please post your best-effort code as a baseline for discussion.

Jim

john010117

11:49 pm on Oct 27, 2008 (gmt 0)

10+ Year Member



Ok, sure.

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

[edited by: jdMorgan at 12:08 am (utc) on Oct. 28, 2008]
[edit reason] Please use example.com [/edit]

g1smd

11:59 pm on Oct 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Not quite right, but you have the right idea... the rule needs to check which domain it applies to.

HTTP_HOST
is only the domain name, so knock the folder off the end.

I assume you would want it to also redirect for

www.example.com
so change that bit to
^(www\.)?example\.com
too.

You can now place this code in the .htaccess file inside the folder - or you can add

^forum/
to the start of the rule checking and then place it in the .htaccess in the root of the main domain -- your choice.

[edited by: jdMorgan at 12:09 am (utc) on Oct. 28, 2008]
[edit reason] Please use example.com [/edit]

john010117

12:12 am on Oct 28, 2008 (gmt 0)

10+ Year Member



Thanks for the tip - that partially solved the problem. If I visit
http://domain.com/sub/
, it redirects to
http://sub.domain.com/
correctly. However, If I omit the trailing slash (so the URL looks like
http://domain.com/sub
), the redirection redirects to
http://sub.domain.com//home/USERNAME/public_html/sub
, which returns a 404 error. Any ideas?

And as for the www. thing - I already have a .htaccess in my main root that redirects the URL to the non-www version.

john010117

12:15 am on Oct 28, 2008 (gmt 0)

10+ Year Member



NEVERMIND. Solved. Final code:

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

g1smd

12:19 am on Oct 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So you want domain.com/sub/ to redirect but for www.domain.com/sub/ do note that the request will now pass through two redirects - a redirection chain - and that is bad.

So, fix both problems in the one rule when it is www.domain.com/forum/ that is requested. After that, list your "general" www rule that fixes all of the other requests.

[edited by: g1smd at 12:22 am (utc) on Oct. 28, 2008]

g1smd

12:22 am on Oct 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As for your other problem, it looks like you maybe have a rewrite up before the redirect. In that case the internal filepath is being exposed.

Check the other of your rules: Redirects first, from specific to general, and then rewrites last.

Check a variety of both expected and unexpected URLs: both domains, and subdomains, for both www and non-www, and all both with and without trailing slash, and so on, and use Live HTTP Headers for Mozilla Firefox to check that you get the correct responses back.

[edited by: g1smd at 12:34 am (utc) on Oct. 28, 2008]

jdMorgan

12:30 am on Oct 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Again, HTTP_HOST will contain only the hostname and possibly a trailing period and/or port number. It will never (normally) contain any forward slashes. So I don't see how this RewriteCond can ever work:

RewriteCond %{HTTP_HOST} ^example\.co[b]m/[/b] [NC]

Jim

jdMorgan

12:40 am on Oct 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, if requests for the subdomain-URL are internally rewritten to this subdomain-named folder, and you redirect subdomain-name-folder requests back to the subdirectory-URL, then you may get a loop.

The way to prevent that is to check the client request header using a RewriteCond to test %{THE_REQUEST}, and only redirect back to the subdomain URL if it was the client that requested the subdomain-named folder, rather than that URL being requested as a result of the previously-invoked internal rewrite.

This is why I said "it's not a problem" at the outset. However, I didn't say "It's simple"... :)

Jim