Forum Moderators: phranque

Message Too Old, No Replies

Arbitrary Subdomain used as Script Parameter - Help

         

Aric

4:43 pm on May 23, 2005 (gmt 0)

10+ Year Member



Ok, after searching and trying examples from the forum, I am at a loss.

I want [subdomain.domain.com(...] to point to [(www).domain.com...]

i.e. I want *any* subdomain (besides "www" or a nonexistent one) typed in/linked to to be used as the sole script parameter for a db lookup.

This is my server, so I have full access to everything. Wildcards are on for subdomains for this domain. About 50 domains/sites are hosted (all mine, not a hosting company or anything).

Here is the code in my .htaccess of the root document directory for the domain I am working with:

Options +FollowSymLinks
RewriteEngine ON
RewriteCond %{HTTP_HOST}!^www\.
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
RewriteRule /.* [domain.com...] [L]

I have Canonical names turned on in httpd.conf

All I get is a 500 error. Here is the line from the error_log:

[Mon May 23 12:29:17 2005] [error] [client *.*.*.*] mod_rewrite: maximum number of internal redirects reached. Assuming configuration error. Use 'RewriteOptions MaxRedirects' to increase the limit if neccessary.

I do not see 'RewriteOptions MaxRedirects' in the config.

In httpd.conf i have this for this VirtualHost:
....
ServerName www.domain.com
ServerAlias *.domain.com
....

What am I missing here? This is getting extremely frustrating!

Aric

4:48 pm on May 23, 2005 (gmt 0)

10+ Year Member



Oops, the actual rewrite code that gives the 500 error is this:

Options +FollowSymLinks
RewriteEngine ON
RewriteCond %{HTTP_HOST}!^www\.
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
RewriteRule .* [domain.com...] [L]

When I try with a beginning slash on the RewriteRule pattern, it just goes to [subdomain.domain.com...] without any sort of redirect.

jdMorgan

5:02 pm on May 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is nothing in your code to prevent reviews.php from being rewritten after the redirect. You'll need to exclude it in order to avoid the redirection loop. Also, I doubt you want en external redirect, so the syntax would be:

RewriteRule !^/reviews\.php$ /reviews.php?site=%1 [L]

Jim

Aric

5:08 pm on May 23, 2005 (gmt 0)

10+ Year Member



Thanks for the reply Jim.

I am not concerned with review.php being called in the address bar itself (whether typed in or linked to).

My only concern is that the subdomain becomes a parameter for the script and it seems no matter what I try (including your example as a desperate attempt), I get a 500 internal error.

Perhaps your reply went over my head...

bird

5:08 pm on May 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On the first line, add a space character between the %{HTTP_HOST} and !^www\..

Having those two terms written together is a syntax error and will trigger a 500 result code.

Aric

5:13 pm on May 23, 2005 (gmt 0)

10+ Year Member



There is a space, not sure why it posted without one...

bird

5:33 pm on May 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You also can't include the http://www.domain.com in the target for an internal redirect. Try something like this:


RewriteEngine ON
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
RewriteRule .* /reviews.php?site=%1 [L]

(the missing space appears to be a bug in BestBBS. It eats space characters in front of a "!".)

jdMorgan

5:47 pm on May 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Spaces are removed on purpose, as are multiple exclamation points, etc.

Again, bird's code will loop unless an exclusion for reviews.php is added. For the sake of a simple exmlanation, you may consider mod_rewrite in an .htaccess context to be recursive; Any new URL you rewrite to will be run through the .htaccess code again. In your case, this creates a loop and the server 'times out' with the 'redirectiion limit' error message.

Jim

jdMorgan

5:53 pm on May 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ack! I guess not, if you're in httpd.conf.

Flush your browser cache after any change to the code. Also, restart your server after changes to httpd.conf.

If you still get a 500 error, then check the log to make sure it's the same cause. If not post the new error message.

Jim

Aric

9:05 pm on May 23, 2005 (gmt 0)

10+ Year Member



Thanks for replies.

I hear what you are saying Jim, and see the logic of it. The error in the log is clear to me now that is a loop.

I tried the line you suggested but it still came up as a 500 error.

I was using the code in .htaccess

I moved all of the rewrite code to the httpd.conf directly for that domain in its <virtualhost> entry and........ it works like a charm :)

Any idea why it behaves so differently when in .htaccess?

Aric

9:40 pm on May 23, 2005 (gmt 0)

10+ Year Member



Ok, this creates another side effect. Images that try to load in [subdomain.domain.com...] are broken.

I assume the rewrite conditions throw this off since they run through the conditions themselves.

Can I exclude images and anything else you may think of from this rule/condition, so they load normally?

Thanks guys, I really appreciate this :) This is my first time playing with mod_rewrite; I've been reading up on it all weekend and this forum has been the most resourceful so far for the beginner.

jdMorgan

11:03 pm on May 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, it all hinges on how you define what is to be rewrriten and what should not be rewritten. The problem is likely to be related to the use of relative links for your images. It is the browser that resolves relative links based on where it thinks the current page is located. If mod_rewrite makes the browser and server disagree on the location of the current page, then you get problems like this.

One solution is to add RewriteConds to prevent certain types of files -- or files in certain other subdirectories -- from being rewritten. The choice should be based on the most efficient code possible for a solution that makes sense for your site.

mod_rewrite code is not usually directly 'portable' between .htaccess and httpd.conf; The URLs 'seen' and tested by RewriteRule in .htaccess are 'localized' to the directory in which that .htaccess file is located. For example, the URL http:www.example.com/foo/bar/index.html is seen as "/foo/bar/index.html" in httpd.conf, as "foo/bar/index.html" in the .htaccess file located at "/.htaccess", as "bar/index.html" in the .htaccess file located in at "/foo/.htaccess", and as "index.html" by the .htaccess file in "/foo/bar/" -- This is why .htaccess is referred to as a "per-directory" configuration file.

Jim