Forum Moderators: phranque

Message Too Old, No Replies

htaccess, subdomains and urls

         

Mr_Cat

7:05 pm on Nov 12, 2008 (gmt 0)

10+ Year Member



I have a domain and a few subdomains on it. I set up the first sub domain, forum.mydomain.com and it works fine, but subsequent subdomains always come up as www.mydomain.com/subdomain.

My original forum subdomain has its own htaccess file that I remember copying there after adiing some lines to my root htacces file to make sure the www was always added to mydomain.com. (I removed said lines again from the subdomain htaccess copy). When you visit the forum you get [forum.mydomain.com...] in the address bar

Without any htaccess file in my new 'publish' subdomain folder, when I go to [publish.mydomain.com...] it comes up with www.mydomain.com/publish. If I copy in the htaccess from my forum folder then when I try to assess publish.mydomain.com it just brings up www.mydomain.com without even the /publish.

I'm very confused as to why the forum subdomain looks fine in the url and no other subdomains do as such.

Cheers folks

(sorry, I think this a re-occuring question but I can't for the life of me phrase it right in a search! I find a lot of people trying to re-direct things the other way round! what the...)

[edited by: Mr_Cat at 7:39 pm (utc) on Nov. 12, 2008]

jdMorgan

7:53 pm on Nov 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a domain and a few subdomains on it. I set up the first sub domain, forum.mydomain.com and it works fine, but subsequent subdomains always come up as www.mydomain.com/subdomain.

How, precisely, did you "set up" your additional subdomains? Since it can be done in dozens of ways, we need to know what this means.

What do you mean by subdomains always "come up as"? Is this what you're typing, or are you typing the subdomain URL, and then seeing a redirect such that the main-domain-plus-subdomain-subfolder is being 'exposed' to the client (browser)?

Jim

Mr_Cat

10:22 pm on Nov 12, 2008 (gmt 0)

10+ Year Member



well, I've set up my subdomains via c-panel on my server.

If I type forum.mydomain.com into the browser url, which I set up a while ago, it appears in the browser as forum.mydomain.com which is great, but subsequent subdomains I've set up in the same fashion via c-panel can be accessed by typing subdomain.mydomain.com into the browser but the browser url always refreshes to www.mydomain.com/subdomain

I had to fix this a while ago with my forum, but I'm sure I just coppied an htaccess file into the forum folder on my server and it was fine. In this case it seems to just redirect back to my main domain if I do that, and with no htaccess file it works fine but doesn't displey in the browser url in proper subdomain fashion.

I've re-examined my htaccess files within the main www root folder and the forum folder, but they seem the same. I don't understand much about htaccess, but less how there is one in the forum subdomain folder that works fine and none in any other subdomain folders (and when there is it causes a problem).

[edited by: Mr_Cat at 10:30 pm (utc) on Nov. 12, 2008]

jdMorgan

10:59 pm on Nov 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In that case, I'd suggest that it would be easier to discuss the problematic code if you would post the relevant parts of it -- after changing you domain name to "example.com" to comply with our Terms of Service. Do not change the subdomain names, just the root domain.

Thanks,
Jim

Mr_Cat

11:21 pm on Nov 12, 2008 (gmt 0)

10+ Year Member



Well the trouble is I'm not sure what the problematic code is.

Here is what I've got!...

My root htaccess file reads:

RewriteEngine on
# -FrontPage-

IndexIgnore .htaccess */.?* *~ *# */HEADER* */README* */_vti*

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName example.com
AuthUserFile /home/username/public_html/_vti_pvt/service.pwd
AuthGroupFile /home/username/public_html/_vti_pvt/service.grp

# Mr Cats this will put the www where its wanted

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]

# Mr Cats this will serve up my custom 404 error page

ErrorDocument 404 /Errorpages/pnf.html

.....I also tried adding this after some hunting about (but it did nothing):

RewriteEngine on
RewriteCond %{HTTP_HOST} ^publish.example.com [NC]
RewriteRule ^/(.*)$ /publish/$1 [L]

So that's in the www folder, minus the bottom bit that didn't appear to do anything!

This is in my forum.example.com subdomain/forum folder:
RewriteEngine on
# -FrontPage-

IndexIgnore .htaccess */.?* *~ *# */HEADER* */README* */_vti*

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName example.com
AuthUserFile /home/username/public_html/_vti_pvt/service.pwd
AuthGroupFile /home/username/public_html/_vti_pvt/service.grp

RewriteEngine off

...that's all fine, but my new publish.example.com subdomain has no htaccess file in it (if it need or is supposed to have one) and when I copy the forum one above to the 'publish' folder/subdomain then I get this problem that it just seems to redirect to www.example.com and won't find the subdomain at all.

That's all the code involved as far as I can tell, and I think I'm going about it in the right fashion, but it's new to me.

[edited by: jdMorgan at 4:14 am (utc) on Nov. 13, 2008]
[edit reason] example.com [/edit]

jdMorgan

12:18 am on Nov 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What this code will do depends on how your subdomain is "mapped" to the subdirectory. The usual situation is that adding a subdomain using a control panel will create a subdirectory for that subdomain, and put a RewriteRule in conf.d or httpd.conf that rewrites requests for the subdomain directly to that subdomain-subdirectory.

However, in that case, then your main domain's root directory .htaccess file won't be executed for subdomain requests, and there is no way that it could interfere with those subdomain requests.

That doesn't seem to be the case here, so we'll try something with your existing code. If it doesn't work, then I'll ask that you comment out all the mod_rewrite code, and see if you still have a problem with the subdomain; That will quickly answer the question as to whether the subdomain subdirectory is subject to the main domain's .htaccess file.

But first, try these modifications:


RewriteEngine on
#
# Externally redirect requests for other than "www" or "publish" subdomains to "www" subdomain
# (Note that this rule is 'forgiving' -- Some case errors, FQDNs, & appended ports are handled later)
RewriteCond %{HTTP_HOST} !^(www¦publish)\.example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Externally redirect all "www" subdomain variations to canonical "www" subdomain
# (This fixes case errors, FQDN, and appended port numbers)
# If loose match
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
# but not exact match
RewriteCond %{HTTP_HOST} !^www\.example\.com$
# redirect to canonical www subdomain
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Externally redirect all "publish" subdomain variations to canonical "publish" subdomain
# (Same as the rule above, but for publish.example.com)
RewriteCond %{HTTP_HOST} ^publish\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^publish\.example\.com$
RewriteRule (.*) http://publish.example.com/$1 [R=301,L]
#
# Inrernally rewrite requests for "publish" subdomain to "/publish" subfolder if not previously done
RewriteCond %{HTTP_HOST} ^publish\.example\.com$
RewriteCond $1 !^publish/
RewriteRule (.*) /publish/$1 [L]

Important: Change the broken pipe "¦" character in the first RewriteCond to a solid pipe character before use; Posting on this forum modifies the pipe characters.

Again, if this doesn't work we're going to need to do some simple testing to see whether your subdomain is subject to your main domain's .htaccess file or not.

Jim

Mr_Cat

1:14 pm on Nov 13, 2008 (gmt 0)

10+ Year Member



Right, my new root htaccess file reads:

RewriteEngine on
# -FrontPage-

IndexIgnore .htaccess */.?* *~ *# */HEADER* */README* */_vti*

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName example.com
AuthUserFile /home/username/public_html/_vti_pvt/service.pwd
AuthGroupFile /home/username/public_html/_vti_pvt/service.grp

# Mr Cats this will put the www where its wanted

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]

# Mr Cats this will serve up my custom 404 error page

ErrorDocument 404 /Errorpages/pnf.html

RewriteEngine on
#
# Externally redirect requests for other than "www" or "publish" subdomains to "www" subdomain
# (Note that this rule is 'forgiving' -- Some case errors, FQDNs, & appended ports are handled later)
RewriteCond %{HTTP_HOST} !^(www¦publish)\.example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Externally redirect all "www" subdomain variations to canonical "www" subdomain
# (This fixes case errors, FQDN, and appended port numbers)
# If loose match
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
# but not exact match
RewriteCond %{HTTP_HOST} !^www\.example\.com$
# redirect to canonical www subdomain
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Externally redirect all "publish" subdomain variations to canonical "publish" subdomain
# (Same as the rule above, but for publish.example.com)
RewriteCond %{HTTP_HOST} ^publish\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^publish\.example\.com$
RewriteRule (.*) [publish.example.com...] [R=301,L]
#
# Inrernally rewrite requests for "publish" subdomain to "/publish" subfolder if not previously done
RewriteCond %{HTTP_HOST} ^publish\.example\.com$
RewriteCond $1 !^publish/
RewriteRule (.*) /publish/$1 [L]

basically the above combined!

Now when I go to [publish.example.com...] the address bar displeys just that. Just what I was after, however, the page displeys this;

Moved Permanently

The document has moved here.

Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.
Apache/2.2.9 (Unix) mod_ssl/2.2.9 OpenSSL/0.9.7a DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 mod_perl/2.0.4 Perl/v5.8.8 Server at publish.example.com Port 80

Oops. Half way there tho :) I wasn't sure to leave my initial 'add the www' code in or not so I just left it?

My [forum.example.com...] subdomain seems uneffected, it has its own htaccess as mentioned, I'm still confused as to how the two subdomains seem to behave defferently tho they were both set up in exactly the same way initially.

[edited by: Mr_Cat at 1:34 pm (utc) on Nov. 13, 2008]

jdMorgan

1:50 pm on Nov 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Delete your original first rule. The code I posted replaces all of your Rewriterules.

Temporarily rename or delete the .htaccess file in the subdomain folder -- We need a simple test environment, so one .htaccess file at a time, please.

You will need a "home page" in the new subdomain-subdirectory. Put a simple HTML "hello world" index.html page in there if you don't already have one.

You will also need a custom 403 page in the subdomain-subdirectory, if you intend to use custom error documents.

Jim

Mr_Cat

2:48 pm on Nov 13, 2008 (gmt 0)

10+ Year Member



Right! the new root htacces reads:

# Mr Cats this will serve up my custom 404 error page

ErrorDocument 404 /Errorpages/pnf.html

RewriteEngine on
#
# Externally redirect requests for other than "www" or "publish" subdomains to "www" subdomain
# (Note that this rule is 'forgiving' -- Some case errors, FQDNs, & appended ports are handled later)
RewriteCond %{HTTP_HOST} !^(www¦publish)\.example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Externally redirect all "www" subdomain variations to canonical "www" subdomain
# (This fixes case errors, FQDN, and appended port numbers)
# If loose match
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
# but not exact match
RewriteCond %{HTTP_HOST} !^www\.example\.com$
# redirect to canonical www subdomain
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Externally redirect all "publish" subdomain variations to canonical "publish" subdomain
# (Same as the rule above, but for publish.example.com)
RewriteCond %{HTTP_HOST} ^publish\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^publish\.example\.com$
RewriteRule (.*) [publish.example.com...] [R=301,L]
#
# Inrernally rewrite requests for "publish" subdomain to "/publish" subfolder if not previously done
RewriteCond %{HTTP_HOST} ^publish\.example\.com$
RewriteCond $1 !^publish/
RewriteRule (.*) /publish/$1 [L]

(left my error page bit in?)

now my forum displeys in the address bar as http://www.example.com/forum (I deleted its htaccess file)
and I get the same problem as when I coppied an htaccess to my publish folder before
i.e. when I put [publish.example.com...] into the address bar, or click a test link to the same address it just refreshes to http://www.example.com - won't find the subdomain atall again

(I also created 'Errorpages/pnf.html' in both 'forum' and 'publish' folders in the www root)

[edited by: Mr_Cat at 2:50 pm (utc) on Nov. 13, 2008]

jdMorgan

4:44 pm on Nov 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, you're going to need to contact your host and ask them why this happens, since the code above will not redirect your valid subdomains to www.example.com. You've likely got some code at the server config level that is doing this redirect, and if you're on shared hosting, then only your host will have access to change it.

You might first want to dig through your control panel, just to make sure there isn't some option to send all unrecognized subdomains to "www." If there is, turn it off, and then investigate why it didn't recognize the new subdomain.

Jim

[edited by: jdMorgan at 4:44 pm (utc) on Nov. 13, 2008]

Mr_Cat

8:45 pm on Nov 13, 2008 (gmt 0)

10+ Year Member



Cheers, I scanned through my control panel and found all the redirect and subdomain settings. There were a few redirects listed, but I found when I edited the htaccess file the redirects on the control panel updated to reflect the changes I made. Nothing seemed out of place there.

Thanks for your help.