Forum Moderators: phranque
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
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]
# 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]
RewriteCond %{REQUEST_URI} !^/buy_widget_
RewriteCond %{HTTP_HOST} ^buy_widget_(.+)\.domain\.com
RewriteRule (.*) /buy_widget_%1/$1 [L]
If you do get errors, check your raw server error log; These error logs are often very helpful.
Jim