Forum Moderators: phranque

Message Too Old, No Replies

.htaccess and Subdomains

         

bozoka45

6:51 am on Jan 11, 2005 (gmt 0)

10+ Year Member



Hello, I need a tid bit of help with .htaccess.

Right now I'm using this code to restrict users from going to [mydomain.com...] as I want them to enter through [sub.mydomain.com....]


RewriteEngine On
RewriteCond %{HTTP_HOST}!sub.mydomain.com [NC]
RewriteRule .* /index.php

I'd like to redirect a user to the page they are trying to reach through the "invalid" method.

For example:

When a user tries to access through an outside link:

[mydomain.com...]

I'd like to redirect them to that file, but through this method:

[sub.mydomain.com...]

I can do this with PHP, but I really don't know .htaccess. Can anyone help me with this or point me to some understandable .htaccess documentation?

Thanks.

jdMorgan

1:03 am on Jan 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



bozoka45,

Welcome to WebmasterWorld!


RewriteEngine On
RewriteCond %{THE_REQUEST} !http://sub\.mydomain\.com [NC]
RewriteRule ^([^/]+)/(.*)$ http://$1.mydomain.com/$2 [R=301,L]

This will check the original request (not including any rewrites you've already done). If the request did not include the subdomain "sub", then the rule will redirect from somedomain.com/xyz/file to xyz.mydomain.com/file.

You may want to add more code to validate that "xyz" *is* a valid subdomain before redirecting. You may also want to exclude this rewriting if the "www" subdomain is requested.

See "back-references" in the RewriteRule documentation (see links in our forum charter).

Jim

bozoka45

11:22 pm on Jan 15, 2005 (gmt 0)

10+ Year Member



jd, Thanks for the reply :)

Although it doesn't seem to be working. I put the code into both the htaccess file in my public_html as well as each of the subdomain directories (chaning sub to the actual subdomain's name). And it just doesn't do anything.

valder

12:09 am on Jan 16, 2005 (gmt 0)

10+ Year Member



First, as I can see, the .htaccess file shouldn't be placed several places, only in the subdomain catalog.
If you place it in the document root, it would rewrite any requests to mydomain.com, no?

I'm sorry, I may be a bit too tired right now to give a helpful answer, but perhaps this thread [webmasterworld.com] about subdomains would be worth checking out?

(didn't get a response from that guy yet though, but that should probably indicate it works :) ..or that things take time)

-Eivind

jdMorgan

12:41 am on Jan 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



bozoka45,

Yes, sorry -- must have been tired. This will probably work better:


RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^sub\. [NC]
RewriteCond %{THE_REQUEST} ^(A-Z){3,6}\ /sub
RewriteRule ^([^/]+)/(.*)$ http://$1.mydomain.com/$2 [R=301,L]

Jim

bozoka45

2:23 am on Jan 19, 2005 (gmt 0)

10+ Year Member



That doesn't seem to work either. Just gives me my pages, and since I use $_SERVER['DOCUMENT_ROOT'] everywhere (the root is different if its a subdomain), it just messes stuff up.