Forum Moderators: phranque

Message Too Old, No Replies

A step further - automatic subdomain redirect

Umm... read the thread?

         

fmchris

5:02 pm on Jan 27, 2008 (gmt 0)

10+ Year Member



I have a web 2.0-ish service where users can have their own profile. I know how to configure .htaccess files to redirect like this

example.com/user/%1 -> example.com/user?u=%1

Now what I want to do is take this to subdomains, EG

%1.example.com -> example.com/user?u=%1

I have this, but it simply returns 404 errors:

RewriteEngine On
RewriteCond %{HTTP_HOST}!^www\.example\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/user.php?u=%1 [L,R]

Server config is Apache 1.3.37 (Unix) with PHP 5.2.3

[edited by: jdMorgan at 4:37 pm (utc) on Jan. 28, 2008]
[edit reason] example.com [/edit]

jdMorgan

4:35 pm on Jan 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use an internal rewrite to avoid exposing your internal structure, and add a RewriteCond to prevent looping:

RewriteEngine On
#
RewriteCond $1 !^user\.php$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /user.php?u=%1 [L]

Also, note that this code, like yours, drops the originally-requested page, image, or script URL. I presume that you are recovering that information inside the script by using the server Request_URI variable. If not, then you will likely need to add further exclusions to this rule in order to be more specific about what requested URLs should be rewritten to the user.php script -- and more to the point, which should not be rewritten.

Jim

[edited by: jdMorgan at 4:37 pm (utc) on Jan. 28, 2008]

fmchris

8:22 pm on Jan 28, 2008 (gmt 0)

10+ Year Member



Thanks, I'll try it out and see if it works. Unfortunately, I'm working with a shared host, so I don't have access to htaccess logs.

phranque

2:03 am on Jan 29, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], fmchris!

fmchris

3:11 am on Jan 29, 2008 (gmt 0)

10+ Year Member



I get a 404...

"Firefox can't find the server at testing.example.com."

However, if I visit the file with the ACTUAL URL, it works fine...

"http://example.com/user.php?u=testing"

jdMorgan

4:17 am on Jan 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's not a 404 message, that's a DNS lookup failure message.

You have to define your subdomains in your DNS zone file (explicitly or as a wild-card), and your server must also be configured to 'route' those subdomains (or all such wild-card subdomains) to your 'account' filespace (this is typically done with a ServerAlias directive in httpd.conf or conf.d).

It sounds like one or both of these steps have not been done.

Jim