Forum Moderators: phranque

Message Too Old, No Replies

Using mod_rewrite in .htaccess

Pointing subdomains to internal paths

         

Radderz

2:00 pm on Feb 6, 2004 (gmt 0)

10+ Year Member


Hi everyone!

I'm having an issue with using mod_rewrite in a .htaccess file to redirect a sub domain to a different internal directory without redirecting the browser's address bar.

I have spent a while playing around with it, as I am new to mod_rewrite, and have come up with something I think should work in theory, but I think I maybe getting some syntax wrong.

I wish to direct the following, capturing all directories and files:
Subdomain.domain.com/*/*
to the path
subdomain/*/*

by doing the following:
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$
RewriteRule ^(.*)$ subdomain/$1
However, this seems to throw back a 500-server error.

I have successfully done two versions, which are similar, but am unable to combine them successfully to get a working result

I can divert anything at the subdomain (but excludes directories I think) to an exact file:
Subdomain.domain.com/*
to the path
subdomain/file.html

By doing the following:
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$
RewriteRule ^(.*)$ subdomain/file.html

Or I can divert the browser (which I really want to hide and do this server side) from anything to anything (again, I'm not convinced the subdirectories are forwarding):
Subdomain.domain.com/*
To URL
Domain.com/subdomain/*

By doing the following:
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$
RewriteRule ^(.*)$ http://domain.com/subdomain/$1

Any ideas?

I'm also not convinced I know how the $1 and when to use the start/end ^ $ symbols, so It's probably something fundamental that I'm getting wrong..

Anyway, thanks for your help in advance,

Radderz

jdMorgan

5:24 pm on Feb 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Radderz,

Welcome to WebmasterWorld [webmasterworld.com]!

I'm also not convinced I know how the $1 and when to use the start/end ^ $ symbols, so It's probably something fundamental that I'm getting wrong.

Read first, code later: Regular-Expressions tutorial [etext.lib.virginia.edu] : Apache mod_rewrite [httpd.apache.org] (see "back-references")


# External redirect (updates browser address bar)
RewriteCond %{REQUEST_URI} !^/(subdomain_1¦subdomain_2¦subdomain_3¦subdomain_n)
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
RewriteRule (.*) http://domain.com/%1/$1 [R=301,L]

- or -

# Internal rewrite (filename substitution only)
RewriteCond %{REQUEST_URI} !^/(subdomain_1¦subdomain_2¦subdomain_3¦subdomain_n)
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
RewriteRule (.*) /%1/$1 [L]

The first exclusion RewriteCond in both examples is necessary to prevent an infinite redirect loop - the probable cause of your 500-Server Error. I know of no way to use a variable to replace the discrete list of subdomains. However, one way to simplify it is to name all subdomains with a common element not used in any other directory or filenames, and then look for that common element to prevent redirect loops, for example:

RewriteCond %{REQUEST_URI} !^/buy_widget_
RewriteCond %{HTTP_HOST} ^buy_widget_(.+)\.domain\.com
RewriteRule (.*) /buy_widget_%1/$1 [L]

This method prevents having to put a potentially-very-long list of subdomain/subdirectory names into the exclusion condition.

Replace all broken pipe "¦" characters above with solid pipe characters from your keyboard before use - posting on the board here modifies them.

If you do get errors, check your raw server error log; These error logs are often very helpful.

Jim

Radderz

6:17 pm on Feb 6, 2004 (gmt 0)

10+ Year Member



Excellent, thanks very much for your help.

That's been bugging me for a few days, and now it's all fixed!

Thanx!

Radderz