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.

Databits

Xepher, currently I'm working on a new version of my website. I know that under the kanar website, we were capable of setting up the subdomains such as test.kanar.org, to point to another directory which we called "test.kanar.org" without using things like php.

Now what I want to know, is if it is possible for me to write a server script (.htaccess) file in my public_html directory that would do just that.

I.E.

test.databits.cc would be directed to the public_html/test.databits.cc directory, as www.databits.cc or databits.cc would be directed to the public_html/databits.cc folder.

The only thing I've ever used .htaccess for was to prevent indexing of folders and set 404 error pages. So I'm not even sure if this is possible, but would rather not dabble in anything that could potentially screw something up for me or you.
(\_/)    ~Relakuyae D'Selemae
(o.O)    
(")_(")  [Libre Office] [Chrome]

Xepher

It is doable... what you're looking for is called "ModRewrite, the swiss army knife of URL manipulation." It's a built in part of apache, and so it's already running on the server. You just have to put the proper directives in the .htaccess configs. That's the hard part, as, from the offical documentation...
QuoteThe great thing about mod_rewrite is it gives you all the configurability and flexibility of Sendmail. The downside to mod_rewrite is that it gives you all the configurability and flexibility of Sendmail.

    -- Brian Behlendorf
    Apache Group
Also, like the docs say "it's voodoo... cool voodoo, but still voodoo"

As such, I'm simply going to point you at some useful docs, and then wish you the best of luck.

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Databits

Awesome, I'll look into it. I've been searching for awhile for the method to do this, but just as I was starting to run across the rewrite stuff I checked back here and found this. Thanks!
(\_/)    ~Relakuyae D'Selemae
(o.O)    
(")_(")  [Libre Office] [Chrome]

Databits

I'm realizing I'm not getting any results... it is built into apache, but it's disabled by default. I'm not sure if it's enabled because I get absolutely no results when using any sort of RewriteEngine on code in the .htaccess file. Other things work however, such as Options -Indexes.

EDIT: Actually, I didn't have the DNS being managed by xepher.net, it was being hosted by enic.cc. I've changed it so that the databits.cc domain is managed by your server, but I need the MX record on my domain to be mx.enic.cc to continue to use the email forwarding I have set up.

Only problem is, I was unable to simply list xepher.net as a name server, instead I needed to use xepher.net and ns2.xepher.net (I had ns1.xepher.net in there too, all from a previous configuration).
(\_/)    ~Relakuyae D'Selemae
(o.O)    
(")_(")  [Libre Office] [Chrome]

Xepher

I don't have a config set up for the nameserver to point the site here, but the mailserver somewhere else. If you use the XN nameservers, then everything is going to point here, and all email to that domain will dump into your account here. If you use someone else... well then you need to tell all of those various subdomains to point here as well. So long as the domain name resolves to this server (and is *.databits.cc) it'll go to your account, from which the .htaccess things can be used to send it to a specific site.

Oh, and if you want to use XN for nameservers, it's ns1.xepher.net and ns2.xepher.net. Please don't use JUST "xepher.net"

Databits

Ok, I have enic handling the dns again, I finally got it to work. Turned out that I was using far overcomplicated examples to do what I needed to do. Damn those people and their "examples", I used what I learned in my Linux class instead.


EDIT: Just in case anyone else is looking for a similar solution I'm going to show the code I used and explain things a little bit for you all.

.htaccess
QuoteRewriteEngine On
RewriteCond $1 !/$
RewriteCond $1 !.
RewriteRule ^(.+)$ /$1/ [R=301,L]
RewriteCond %{REQUEST_URI} !^/.*
RewriteCond %{HTTP_HOST} !^www.domainname.com [NC]
RewriteCond %{HTTP_HOST} !^domainname.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+).domainname.com
RewriteCond /home//public_html/%1 -d
RewriteRule (.*) /%1/$1 [L]
What this code essentially does is redirects the document root to another folder based on whatever sub-domain you give. Multiple conditions automatically determine if the folder exists or not, and make sure that it's not a root domain I.E. www.domainname.com or domainname.com.

For example, the default root directory located under a username on this server is /home//public_html/. Using my domain as an example, both http://www.databits.cc/ and http://databits.cc/ point to this location.

Now when a user enters something as an alternate sub-domain, or something like subdomain.domainname.com the code above will parse out the subdomain part, check to see if /home//public_html/subdomain/ exists, then if it does redirect the document root to that location instead. As demonstrated using my site, when you goto http://test.databits.cc/ you will get the same thing as if you went to http://databits.cc/test.databits.cc/, http://www.databits.cc/test.databits.cc/, or even http://databits.xepher.net/test.databits.cc/. But more seemlessly to the user (my folder is actually named test.databits.cc, not just test).

Now, to explain some of this code in more detail...

QuoteLINE 1
QuoteRewriteEngine On
The first line for the code is required for the rest to work. It effectively does exactly as it says, turns on the rewrite engine.
QuoteLINES 2-4
QuoteRewriteCond $1 !/$
RewriteCond $1 !.
RewriteRule ^(.+)$ /$1/ [R=301,L]
These lines of code check for and add a trailing slash to the URL if it doesn't exist.
QuoteLINES 5-9
QuoteRewriteCond %{REQUEST_URI} !^/.*
RewriteCond %{HTTP_HOST} !^www.domainname.com [NC]
RewriteCond %{HTTP_HOST} !^domainname.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+).domainname.com
RewriteCond /home//public_html/%1 -d
These lines are the conditions list. All the conditions must be met before the following rewrite will occur. The first and second lines here check to make sure that the domain is not a root domain I.E. domainname.com or www.domainname.com. The following line parses out the subdomain from the front of the domain. The last line checks to see if a folder matching the subdomains name exists.
QuoteLINE 10
QuoteRewriteRule (.*) /%1/$1 [L]
This is the line that does the domain rewrite. If all the previous conditions were met, this line will redirect the document root to the specified subdomain's folder.
With a little revision, this code could be changed along with the directory structure under your public_html folder to prevent users from doing things like accessing subdomains from inside another subdomain. An issue I'm not addressing right now.

NOTE: If anyone is looking to do something similar and runs into issues, I have no problem helping you out. All you gotta do is ask. ;)
(\_/)    ~Relakuyae D'Selemae
(o.O)    
(")_(")  [Libre Office] [Chrome]

fesworks

So I just download the Unix version ( http://httpd.apache.org/download.cgi )  and I change the .htaccess configs through that? or is it already on the site somewhere because you said it's preinstalled on the server?

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

Xepher

WHAT? No no no... you don't have to download an entire webserver! (Or anything for that matter.) You just have to create a file named .htaccess  in your public_html folder with the proper rules. It's just a plain text config file, that's all. Make it with anything from notepad to nano.

Try this code.
RewriteCond %{HTTP_HOST}   ^.*.fesandernst.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         /home/fesworks/public_html/fesandernst/$1 [L]

RewriteCond %{HTTP_HOST}   ^.*.fesworks.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         /home/fesworks/public_html/fesworks/$1 [L]
That make sense? It'll look for the domainname, and if it matches, it'll go to a subdirectory (transparently to the user.) I think that's what you want. Code isn't actually tested, so if it doesn't work, let me know and I can take a look at it in action and try and tweak it.

See this for some other examples. http://httpd.apache.org/docs/2.0/rewrite/rewrite_guide.html

fesworks

On this thread you gave me a different code:
https://xepher.net/forum/viewtopic.php?id=455&p=2

it had an "R" in there with the "L"

Well, I'll give these a shot.

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

Xepher

The "R" does a redirect... that is sends the browser a message saying "this moved to this new address, go there" without the R, it does it behind the scenes, so to speak, and serves the new file, even though the browser still thinks it's at the old address. The "R" thing you'd do if you want something like "fesworks.xepher.net" to always go to just "fesworks.com" so everyone gets used to going to the same place. On the other hand, hosting a SEPERATE subdomain, you probably want it to look like to seperate websites, not tell the browser "I know you asked for ardra.com, but go to http://fesworks.com/adra/ to find what you're looking for." Make sense? (Note that when I gave you the code in that other thread, I thought you were doing something quite different than what you're asking here.)

fesworks

Yea, in the other thread I ended up going through all my pages and changing the links from "home.php" to "index.php"

Now I am going to try and get "fesworks.com" to point to http://fesworks.xepher.net/TCSS.htm for the review site, but I'm going to try the code it with the domain I already have here before I ask to have "fesworks.com" moved here too... just to make sure I get the coding all right.

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

Xepher

Okay, well, if you get something in place and it's not working, let me know precisely what's supposed to redirect to what, and I can take a look at it if you want.

fesworks

well thank you. I hate to seem like a wet baby coder about things lately, but I'll try this out on my own first in the next few weeks :D

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

fesworks

I never did try what you suggested... mostly because I never got "fesworks.com" moved to Xepher... it still redirects from Yahoo.

I'd like to make "fesworks.com" point to my account just like you have "fesandernst.com" point to my account.

I'm also planning on getting a domain for that Jenny Everywhere archive I've told you about. I may have to use the version that's on my fesworks.xepher account. I have a co-project with Strip Foght coming soon.



So let me get this straight... if I get "http://theshifterarchive.com" (may or may not be the only, or final domain) on the xepher nameservers, it will go to http://fesworks.xepher.net .... and if I want it to point to, say, http://fesworks.xepher.net/SAP instead, I'd have to input this code you mentioned into the .htaccess of my public_html folder (the base folder right?)?

Also, if that's true, would it work in the same manner if it were the base of the subfolder?

like right now, http://fesworks.xepher.net is also fesandernst.com.... and http://fesworks.xepher.net/SAP is the same as http://fesandernst.com/SAP

and so on for all files and folders.

using this .htaccess code, would it act the same way for the sub folder?

Like I think you are saying that http://fesworks.xepher.net/SAP will become http://theshifterarchive.com .... right?

well would http://fesworks.xepher.net/SAP/STORIES also be http://theshifterarchive.com/STORIES as well? and so on for each file and subfolder of /SAP/ ?


basically I'm seeing if I can host it proper on my account now, and move things over to a new account (if my application for it gets approved of course) without loss to permalinks?

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

Xepher

Okay... I think I get what you said, and you seem to be correct. You should be able to effectively do any of what you want using the rewrite stuff. To put it (hopefully) simply... the rewrite stuff let's you just set a rule to say "when you see this change it to that" but in really complicated ways. Thus, it's no problem to take every case of "http://siteb.com/XXXX/stuff" and change it to "http://sitea.com/stuff" Well, that's not the best example, the better way of viewing it is to say, when you see a thing starting with "http://siteb.com/" serve it from "/home/fesworks/public_html/siteb/" which means that "http://siteb.com/stuff/things" serves the files from "/home/fesworks/public_html/siteb/stuff/things" which is ALSO the same as "http://fesworks.com/siteb/stuff/things" etc.

Like I said earlier, you can make anything work however you want... it's just a matter of  how complicated the rules get. Also, don't feel bad about the confusion. mod_rewrite uses regular expressions... possibly the most complicated thing in computing/coding. I mean, even the official documentation admits it's voodoo!

Quote
(From http://httpd.apache.org/docs/2.0/rewrite/ )

"The great thing about mod_rewrite is it gives you all the configurability and flexibility of Sendmail. The downside to mod_rewrite is that it gives you all the configurability and flexibility of Sendmail.''
-- Brian Behlendorf, Apache Group

"Despite the tons of examples and docs, mod_rewrite is voodoo. Damned cool voodoo, but still voodoo. ''
-- Brian Moore, bem@news.cmc.net

Welcome to mod_rewrite, the Swiss Army Knife of URL manipulation!