News:

The anti-spam plugins have stopped being effective. Registration is back to requiring approval. After registering, you must ALSO email me with your username, so that I can manually approve your account.

Main Menu

Subdomains using server side .htaccess files

Started by Databits, August 31, 2005, 08:44:33 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Xepher

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.


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

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 :)

www.PSIwebcomic.com
www.TheShifterArchive.com
www.ArdraComic.com
www.WebcomicBeacon.com

jekkal

Regular Expressions ARE VooDoo.

How else do you explain Computational Linguistics?!

Xepher

Fes: Done. Switch the nameservers and it should work.

Jekkal:

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

fesworks


www.PSIwebcomic.com
www.TheShifterArchive.com
www.ArdraComic.com
www.WebcomicBeacon.com

Miluette

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

#21
It's 3am, and I'm drunk, and I'm trying this from memory and with minimal testing, but something like this might work...


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...


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/

Miluette

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:

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

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:

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

Miluette

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

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

This is what my main .htaccess file looks like right now:


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

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

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

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...

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.)