Author Topic: Subdomains using server side .htaccess files  (Read 38800 times)

0 Members and 1 Guest are viewing this topic.

Xepher

  • Techsmith
  • Administrator
  • *****
  • Posts: 4,326
  • Illegitimis non carborundum!
    • View Profile
    • Xepher.net
Re: Subdomains using server side .htaccess files
« Reply #15 on: September 11, 2007, 05:09:28 am »
As much for my own notes as well as to help other people... working code for a .htaccess file in public_html, linking a particular folder to a particular subdomain.

Code: [Select]
RewriteEngine On
RewriteCond %{HTTP_HOST}   (.*)fesworks\.com$ [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{REQUEST_URI}  !^/TCSS/(.*)
RewriteRule ^(.*)         /TCSS/$1 [L]

That makes the fesworks.com domain work as the public_html/TCSS/ folder.

fesworks

  • Administrator
  • *****
  • Posts: 923
    • View Profile
    • http://www.fesworks.com
Re: Subdomains using server side .htaccess files
« Reply #16 on: September 11, 2007, 05:11:04 pm »
Sounds good :D Thanks again :D

Also, hate to bother you again, but can you make "theshifterarchive.com" apart of the Xepher namesevers please? I just bought it this morning :)

jekkal

  • Newbie
  • *
  • Posts: 38
  • Last Resort Volunteer
    • View Profile
    • Last Res0rt - The Cyberpunk Furry Webcomic
Re: Subdomains using server side .htaccess files
« Reply #17 on: September 12, 2007, 03:31:36 am »
Regular Expressions ARE VooDoo.

How else do you explain Computational Linguistics?!

Xepher

  • Techsmith
  • Administrator
  • *****
  • Posts: 4,326
  • Illegitimis non carborundum!
    • View Profile
    • Xepher.net
Re: Subdomains using server side .htaccess files
« Reply #18 on: September 12, 2007, 05:15:04 am »
Fes: Done. Switch the nameservers and it should work.

Jekkal:

(From http://xkcd.com/114/ )

fesworks

  • Administrator
  • *****
  • Posts: 923
    • View Profile
    • http://www.fesworks.com
Re: Subdomains using server side .htaccess files
« Reply #19 on: September 12, 2007, 01:49:57 pm »
Awesome! Thanks much! :D

Miluette

  • Sr. Member
  • ****
  • Posts: 332
  • C:
    • View Profile
    • Miluette dot com
Re: Subdomains using server side .htaccess files
« Reply #20 on: April 13, 2008, 05:14:11 am »
Old topic REVIVAL!

So today I've been trying to figure out how to do subdomains for my folders, since apparently it's not as easy as creating one at godaddy and typing in the URL of your desired folder. Grr! Things are never that simple!

For example, I would like

http://senshuu.com/ai/

to also show up like

http://ai.senshuu.com

I'd also like to have maybe more than one subdomain leading to the same folder. They would remain the same as you navigate the site too, like ai.senshuu.com/art.php would still show up that way instead of reverting, if I'm wording it right.

Everything I've found so far (like THIS) hasn't really worked, but I might have gone about it the wrong way--evidently all the code has to be in the htaccess file in the root folder?

By the way, I just happily used this thread to link a folder to my new domain name. Yay! Now -this- should be simple, but it's a little opposite...

One other thing: Whenever anyone goes to my domain name, like senshuu.com/ai/ for instance, and leaves off the final forward-slash, the url always changes back to the Xepher subdomain address, in this case millennium.xepher.net/ai, which is kind of awkward since people don't always put in those last forwardslashes.

I know why this happens now, but need to know how to fix it. I'm gonna paste what Xepher said here to remember:

"This is again, something you can fix yourself with the same sort of mod_rewrite stuff I mentioned above. I, unfortunately, don't have an example of doing this specifically, but I know it's possible. You can basically make your own custom rule to tell the server to look for folders and add the slash, but extend it so it remembers which domain people used the first time."

I'm getting into some pretty complex code stuff. I never thought I'd even have to touch it. But, I like having things a certain way. <3

Preemptive thanks!
And wasn't it you who told me,
"The sun would always chase the day"?

Xepher

  • Techsmith
  • Administrator
  • *****
  • Posts: 4,326
  • Illegitimis non carborundum!
    • View Profile
    • Xepher.net
Re: Subdomains using server side .htaccess files
« Reply #21 on: April 13, 2008, 07:58:07 am »
It's 3am, and I'm drunk, and I'm trying this from memory and with minimal testing, but something like this might work...

Code: [Select]
RewriteCond    %{REQUEST_FILENAME}  -d
RewriteRule    ^(.+[^/])$           http://%{HTTP_HOST}/$1/  [R]

Which should, I believe, check the requested filename, and if it's a directory, send the user to a new URL with the trailing slash added, using whatever host/domain name they used to start with.

Subdomains going to other folders should be just like the example at the top of page 2 here, except you extend it to the subdomain level...

Code: [Select]
RewriteCond %{HTTP_HOST}   ai\.senshuu\.com$ [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{REQUEST_URI}  !^/ai/(.*)
RewriteRule ^(.*)         /ai/$1 [L]

That "[L]" part (the "last rule" flag) may need to be removed depending on the order of things in your .htaccess file. It basically says "when this line gets used, stop any further URL rewrites" so if you've got a bunch of stuff in your .htaccess file, you don't want that until the last line usually. (Or at least the last line that should be logically used from any given entry point... if that makes sense.)


On another note... why do I only seem motivated/able to do code/technical problems when drunk on a weekend? :-P



EDIT: I know it's pretty technical, but most everything I know (except regular expression in general), I learned from the official apache documentation. http://httpd.apache.org/docs/2.2/rewrite/
« Last Edit: April 13, 2008, 08:01:36 am by Xepher »

Miluette

  • Sr. Member
  • ****
  • Posts: 332
  • C:
    • View Profile
    • Miluette dot com
Re: Subdomains using server side .htaccess files
« Reply #22 on: April 13, 2008, 08:51:47 am »
Wahahaha, it works, it all works... beautiful. Thank you so much. ;x;

There are only two questions remaining for me!

(1) How you make a subdomain redirect to a file--in this case, senshuu.com/main.php (which I want to be millennium.senshuu.com ... really weird how I set it up the first go-round, in retrospect), if that's possible.
(2) The following didn't quite work, although it might be my fatigue playing into my understanding of things right now:

Code: [Select]
RewriteCond %{HTTP_HOST}   ai\.jessicacantlope\.com$ [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{REQUEST_URI}  !^/ai/(.*)
RewriteRule ^(.*)         /ai/$1

When I put that in it just redirects to jessicacantlope.com when I thought it would go back to the other Ai site... unless it's the way I have the stuff with Jessicacantlope.com, yeah that probably has something to do with it! lol I'm out of it.

But I'm in such a coding mood. It always happens late night when I should be doing other things. (Not drunk though... alcohol doesn't like me.)
And wasn't it you who told me,
"The sun would always chase the day"?

Xepher

  • Techsmith
  • Administrator
  • *****
  • Posts: 4,326
  • Illegitimis non carborundum!
    • View Profile
    • Xepher.net
Re: Subdomains using server side .htaccess files
« Reply #23 on: April 13, 2008, 09:20:07 am »
Okay... even drunker now, so take with lots of salt... but I think you've probably got an [L] flag somewhere in the jessicacantlope.com stuff, so it's hitting the first thing in the file and ignoring the rest. Keep in mind, with regular expressions the "(.*)" thing is a wild card, and matches anything, so if you have "(.*)jessicacantlope\.com" and "ai\.jessicacantlope\.com" in different rulesets, you need to put the more exact/specific AI one first, else the other one (with the wildcard) will match either case. Alternately, you may try getting rid of the [L] flag on the wildcard one, see if it passes on through and matches the next like you'd expect. I'm not exactly sure on the rules of precedence for this stuff, but... well, the general problem is just something like that.


As for a subdomain going to a specific file... that's just a specific case of the same directory level redirect we've already done.

Try:
Code: [Select]
RewriteCond %{HTTP_HOST}   millennium\.senshuu\.com$ [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{REQUEST_URI}  !^/main.php
RewriteRule ^/         /main.php

Miluette

  • Sr. Member
  • ****
  • Posts: 332
  • C:
    • View Profile
    • Miluette dot com
Re: Subdomains using server side .htaccess files
« Reply #24 on: April 13, 2008, 09:50:20 am »
So I rearranged things with my first issue and then the address went to my main 404 page...lol. Then I realized it must be trying to go to /portfolio/ai/, which doesn't exist.
With all this fancy coding I'm not sure exactly how to fix it. I just tried and got a 500 error, lol.

Also, the main.php subdomain redirect doesn't work. I hope it's not another order issue.

Wow, I've spent more time on this than I thought today (along the way I've been cleaning up code here and there). I need to sleep or something lol.
And wasn't it you who told me,
"The sun would always chase the day"?

Xepher

  • Techsmith
  • Administrator
  • *****
  • Posts: 4,326
  • Illegitimis non carborundum!
    • View Profile
    • Xepher.net
Re: Subdomains using server side .htaccess files
« Reply #25 on: April 13, 2008, 12:14:17 pm »
All the snippits I've given you work by themselves and alone... I've never tried putting them all together though, so I'm not sure how they work together. You may just have to play with it a bit, but if you can't figure it out, I'll try and give it a go next time I have some time (and am sober... right now it's saturday night/sunday morning and OMG 7am.)

Miluette

  • Sr. Member
  • ****
  • Posts: 332
  • C:
    • View Profile
    • Miluette dot com
Re: Subdomains using server side .htaccess files
« Reply #26 on: April 13, 2008, 10:37:27 pm »
This is what my main .htaccess file looks like right now:

Code: [Select]
ErrorDocument 404 http://www.senshuu.com/404.php

RewriteCond    %{REQUEST_FILENAME}  -d
RewriteRule    ^(.+[^/])$           http://%{HTTP_HOST}/$1/  [R]

RewriteCond %{HTTP_HOST}   ai\.jessicacantlope\.com$ [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{REQUEST_URI}  !^/ai/(.*)
RewriteRule ^(.*)         /ai/$1

RewriteEngine On
RewriteCond %{HTTP_HOST}   (.*)jessicacantlope\.com$ [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{REQUEST_URI}  !^/portfolio/(.*)
RewriteRule ^(.*)         /portfolio/$1 [L]

RewriteCond %{HTTP_HOST}   millennium\.senshuu\.com$ [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{REQUEST_URI}  !^/main.php
RewriteRule ^/         /main.php

RewriteCond %{HTTP_HOST}   ai\.senshuu\.com$ [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{REQUEST_URI}  !^/ai/(.*)
RewriteRule ^(.*)         /ai/$1

RewriteCond %{HTTP_HOST}   lf\.senshuu\.com$ [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{REQUEST_URI}  !^/lf/(.*)
RewriteRule ^(.*)         /lf/$1

RewriteCond %{HTTP_HOST}   lovefeast\.senshuu\.com$ [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{REQUEST_URI}  !^/lf/(.*)
RewriteRule ^(.*)         /lf/$1

RewriteCond %{HTTP_HOST}   portfolio\.senshuu\.com$ [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{REQUEST_URI}  !^/portfolio/(.*)
RewriteRule ^(.*)         /portfolio/$1

RewriteCond %{HTTP_HOST}   portfolio\.jessicacantlope\.com$ [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{REQUEST_URI}  !^/portfolio/(.*)
RewriteRule ^(.*)         /portfolio/$1 [L]

They all work except for the two I mentioned, ai.jessicacantlope.com and millennium.senshuu.com. D:
And wasn't it you who told me,
"The sun would always chase the day"?

Xepher

  • Techsmith
  • Administrator
  • *****
  • Posts: 4,326
  • Illegitimis non carborundum!
    • View Profile
    • Xepher.net
Re: Subdomains using server side .htaccess files
« Reply #27 on: April 14, 2008, 02:33:05 am »
I edited your .htaccess file to fix things, as I needed to test, since I wasn't really sure what to do.

Basically, it was partly like I said, the [L] flags and the order. I had to change the wildcard jessica... entry to be ^(www\.)?jessicacantlope\.com$ instead of the (.*) bit... which means it'll match only for an optional www. and won't match/rewrite the ai. or other subdomain rules. For millenium I just had to change the last line from "^/" to "^$" meaning an empty string, rather than a slash. Other those those, I just reordered things so they make a bit more logical sense. Test and make sure it all works though.

Miluette

  • Sr. Member
  • ****
  • Posts: 332
  • C:
    • View Profile
    • Miluette dot com
Re: Subdomains using server side .htaccess files
« Reply #28 on: April 14, 2008, 11:36:40 pm »
Oh neat, everything works now! Thanks for all your help *_*
One thing though. Now things relating to jessicacantlope.com revert to the xepher address when you leave off the final slash again. Not that it'll be a big deal since it'll be two pages at most (unless somebody uses ai.jessicacantlope.com...). I'm just real super-picky about this kind of thing, lol.

<3
And wasn't it you who told me,
"The sun would always chase the day"?

Xepher

  • Techsmith
  • Administrator
  • *****
  • Posts: 4,326
  • Illegitimis non carborundum!
    • View Profile
    • Xepher.net
Re: Subdomains using server side .htaccess files
« Reply #29 on: April 15, 2008, 04:04:26 pm »
Oh wow... that took me WAY too long to figure out. What I didn't realize was that original bit to add the trailing slash... well it was using REQUEST_FILENAME and checking if it's a directory. That means it has to be a real (unremaped) file, and that wouldn't exist for any of your remapped subdomains, so it was going through and remapping the other stuff first, even though it was at the top of the file AND had a "L" flag on it. Eventually I figured out the code below...

Code: [Select]
RewriteCond    %{REQUEST_URI}  !(.+)\.(.+)$
RewriteCond    %{REQUEST_URI}  !(.+)/$
RewriteRule    ^(.+)$           http://%{HTTP_HOST}/$1/  [L,R]

What that's gonna do is check that there's no dot/period in the URL (as that would indicate it's a file) and that it doesn't end with a slash... THEN it'll redirect, adding the slash, and to the same hostname it got the request under. It's not perfect though, if you have regular files that don't have a "dot" in their filename (or directories that do) then it'll break. If that's a problem well... then it gets a lot more complicated again. For now, I think it's working, and the changes are already made in your .htaccess file (since that's where I did my testing.)