homepage Welcome to WebmasterWorld Guest from 107.21.163.227
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Accredited PayPal World Seller

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Rewrite Subdomain to Variable?
sub.domain.com -> www.domain.com/index.php?s=sub
Dolemite




msg:1505890
 4:59 am on Jun 2, 2003 (gmt 0)

I want to do an internal rewrite to rewrite subdomain URLs to www.domain.com URLs with the subdomain as the variable.

I've tried this but I'm getting 500 Internal Server Errors:

RewriteCond %{HTTP_HOST}!^www.$
RewriteCond %{HTTP_HOST}!^domain\.com$
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteRule ^.*$ [domain.com...]

My logic is: when HTTP_HOST is not www.domain.com or domain.com, rewrite subdomain to variable. I realize I can do simpler redirectmatch rewrites/redirects from the .htaccess files in the subdomain directories, but I'd prefer to do it in a more dynamic way since I'm working with a large number of subdomains.

By the way, I'm pretty sure that 2nd RewriteCond is wrong, it might match [sub.domain.com...] as well as [domain.com...] but I've tried it without this line with the same results. Is http:// valid in an HTTP_HOST match? I.E., can I do RewriteCond %{HTTP_HOST}!^http://www.domain.com$?

 

jdMorgan




msg:1505891
 5:34 am on Jun 2, 2003 (gmt 0)

Dolemite,

> I want to do an internal rewrite to rewrite subdomain URLs to www.domain.com URLs with the subdomain as the variable.

See change to RewriteRule below - "No [..."...] needed if internal.

> I've tried this but I'm getting 500 Internal Server Errors:

What kind of error? What does your error log say? Also, are you missing the spaces before "!" characters as shown below?

> By the way, I'm pretty sure that 2nd RewriteCond is wrong, it might match [sub.domain.com...] as well as [domain.com...] but I've tried it without this line with the same results.

No, you have it start-anchored, so it will not match <anything>.domain.com. But you can eliminate it with a small change to current Cond #3 as shown ( "*" changed to "+" )

> Is http:// valid in an HTTP_HOST match? I.E., can I do RewriteCond %{HTTP_HOST}!^http://www.domain.com$?

It is "valid" but it won't match. HTTP_HOST starts with the (sub)domain name, and does not include the method (http).

There was a potentially-fatal bug with your Cond #1, in that "!^www." was end-anchored, so this Cond would never have matched. I have omitted all end-anchors from the Conds, for various reasons.

Try this:

RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
RewriteRule .* /index.php?s=%1 [L]

The first (new) Cond may not be needed, but there are so many "different" server setups, and so many recursive scripts out there that I'm including it "defensively." :)
Try commenting it out after your get the whole thing working, and remove it if nothing breaks.

HTH,
Jim

Dolemite




msg:1505892
 7:04 am on Jun 2, 2003 (gmt 0)

Thanks for replying, Jim.

Unfortunately, that didn't quite work. I also managed to tank the server for about the 20th time. ;) It seems whenever an "incorrect" mod_rewrite rule is invoked on this server, it starts an httpd process that gradually eats up all the memory and CPU time. Since I have shared hosting I don't have permission to just kill the process, I end up waiting until my host catches it. In a way, I don't blame myself, since this has never occurred with my other hosts when I mess around with mod_rewrite...I imagine there's some way prevent this in the apache configuration.

What kind of error? What does your error log say? Also, are you missing the spaces before "!" characters as shown below?

Nope, the spaces are there, they just don't seem to come through in the post. I actually caught the 500 error. I had forgotten these lines in .htaccess:

RewriteEngine on
Options +FollowSymlinks
RewriteBase /

After that it looked like the rewrite was working (I think I had stripped it down to my last 2 lines at that point) but I started getting 404 errors as somehow it wasn't actually finding the file, even though the file most certainly exists. When I look though the raw logs, I see that in the requested rewritten URL the? had been replaced with the ASCII equivalent "%3F".

OK, so anyway, I'm back on the server now, and from my error logs I can tell that using your suggestion, its looking for /home/domain/public_html/subdomain/index.php, which isn't there. I tried changing that last line to:

RewriteRule .* [domain.com...] [L]

Which worked, but was not internal, as you said. I also tried:

RewriteRule .* /home/domain/public_html/index.php?s=%1 [L]

Which set off another infinite loop somehow. My host can't be too happy with me tonight. ;)

If I change the RewriteBase / Options +FollowSymlinks or just delete those lines can I use the full server directory structure to avoid getting into the subdomain directory? I seem to recall that those are there to keep your rewrites inside the public_html directory. Or perhaps something else?

jdMorgan




msg:1505893
 2:51 pm on Jun 2, 2003 (gmt 0)

Dolemite,

> its looking for /home/domain/public_html/subdomain/index.php, which isn't there.

What is the full filepath to index.php, as compared to the above? The "part where it goes wrong" is what needs to be in RewriteBase.

For example, if index.php exists, not at the URL "/home/domain/public_html/subdomain", but rather at the URL www.domain.com/subdomain/index.php, then I think you will need "RewriteBase /home/domain/public.html". Change it, test it, and then see what the 404 error says and use that as a guide.

It sounds to me as if the host alias directives for your account are misconfigured at the server level, but this is only a guess. If you can get it working - "patched" with your .htaccess file, then maybe you can report it to them and tell them the precise symptoms to help them fix it. RewriteBase is one of those things that you really should not have to deal with as a "user" of a shared hosting setup -- IMHO, the hosting service should try to make it transparent for you, and have evidently not done so.

HTH,
Jim

Dolemite




msg:1505894
 9:20 pm on Jun 2, 2003 (gmt 0)

What is the full filepath to index.php, as compared to the above? The "part where it goes wrong" is what needs to be in RewriteBase.

The full path to index.php is:

/home/domain/public_html/index.php

For example, if index.php exists, not at the URL "/home/domain/public_html/subdomain", but rather at the URL www.domain.com/subdomain/index.php, then I think you will need "RewriteBase /home/domain/public.html". Change it, test it, and then see what the 404 error says and use that as a guide.

I don't want to have any files in the /subdomain/ directory, that's the whole purpose of this, to be able to use files in the root (root URL directory, public_html file directory) directory with subdomain variables. I tried both:

RewriteBase /home/domain/public_html

and

RewriteBase /home/domain/public_html/

But in both cases I'm still getting 404 errors looking for the file /home/domain/public_html/subdomain/index.php. I also tried deleting the subdomain directory in case that might have any effect, but it didn't.

Is there any way I can use a full URL or a file path in the RewriteRule and still have an internal rewrite? That would just be too easy, I guess. At least I didn't screw the server over this time. ;)

jdMorgan




msg:1505895
 12:37 am on Jun 3, 2003 (gmt 0)

Dolemite,

Something is interfering with this code. I'd guess it's either another .htaccess rewrite ruleset that redirects from subdomain.yourdomain.com to yourdomain.com/subdomain, or a server alias which does the same thing. You should move this code above any code in .htaccess that does a subdomain rewrite, or have your hosting service remove the alias from httpd.conf and let you do it in .htaccess.

As far as I know, canonical redirects, where the target URL contains a method (such as http:), are external.

HTH,
Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved