Forum Moderators: phranque

Message Too Old, No Replies

Silent redirect for dynamic subdomains

Silent redirect for dynamic subdomains in .htaccess

         

margarita889

11:14 pm on Dec 18, 2008 (gmt 0)

10+ Year Member



What I need to accomplish is the following redirect from:

myname.example.com
-to-
www.example.com:8080/pages/co.faces?var=myname

What I have so far in my .htaccess is this:

RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]*)\.example.com$
RewriteRule .* http://www.example.com:8080/pages/co.faces?var=%1 [L]

Which is working fine, except that the user sees the new (and long and ugly) URL rather than staying on the original "myname.example.com" URL.

How do I keep the new URL from showing?

g1smd

11:18 pm on Dec 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You asked for a redirect at the start of the question, but then are talking about something which performs as a rewrite by the end of the question.

Which do you want?

Your code probably performs a 302 redirect as coded right now.

You could turn that into a 301 redirect by changing [L] into [R=301,L].

You could turn the existing code into a rewrite by omitting the domain name from the "right" of the rule.

How do the subdomains map to your filesystem anyway?

margarita889

11:28 pm on Dec 18, 2008 (gmt 0)

10+ Year Member



OK, I guess I need a rewrite.

I removed the domain name from the beginning of the rewrite rule, but now all I get is an Internal Server Error.

The rule is now:

RewriteRule .* pages/co.faces?var=%1 [L]

(and also tried this one with the leading slash, which also doesn't work):

RewriteRule .* /pages/co.faces?var=%1 [L]

:(

margarita889

11:30 pm on Dec 18, 2008 (gmt 0)

10+ Year Member



Oh, and the subdomains do not map to any specific subdirectories...we just want them to become URL variables to dynamically pull in content.

jdMorgan

12:53 am on Dec 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Modified to enforce basic HTTP subdomain-naming rules, and stop recursion:

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([a-z0-9][a-z0-9_\-]*[a-z0-9])\.example\.com
RewriteCond %{QUERY_STRING} !&?var=[^&]+
RewriteRule .* pages/co.faces?var=%1 [L]

Because your rule "drops" the directory/file paths in the requested URL, you will need to use Path_Info in your script to determine what object is being requested from the subdomain.

Consider: How do you want to handle image requests? CSS and external JavaScript file requests? What about requests for favicon.ico, robots.txt, labels.rdf, sitemap.xml? These objects *will almost certainly* be requested from the subdomains, and you need a plan to handle them.

Jim