Forum Moderators: phranque
I currently have my .htaccess looking like this:
RewriteEngine onRewriteCond %{HTTP_HOST} ^blah.example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.blah.example.com$
RewriteRule ^(.*)$ "http\:\/\/example\.com\/subdomains\/blah\/" [R=301,L]
Can someone please help me to get this right? It's linking to http://example.com/subdomains/blah correctly, however, it's not changing, and/or keeping the URL in the browser to blah.example.com... I'm very new to .htaccess editing, and all of the darn tutorials I see online for this are not very helpful, they talk about numbers being added to it, and so on and on. Do I need a REQUEST_URI in here somewhere? If so, where and how.
Thanks!
[edited by: tedster at 8:46 am (utc) on Jan. 4, 2009]
[edit reason] switch to example.com - it can never be owned [/edit]
i would question why you want to serve the same content to multiple domains.
the way you intend to set it up, you will get the same content for example.com, blah.example.com and www.blah.example.com which a fundamental problem.
it changes the URL in the browsers window to
You specified that with the R flag.
If you're looking for reverse proxying, use the P flag.
Note: without setting ProxyPassReverse in your httpd.conf, redirects issued by the backend server are broken. Also, you'll need to specify the path in the substitution. I assume there's no way to setup bla.example.com to point to example.com (i.e. you can avoid a reverse proxy)?
RewriteCond %{HTTP_HOST} ^(www\.)?blah\.example\.com
RewriteRule ^(.*)$ "http://example.com/subdomains/blah/$1" [R=301,L]
# Externally redirect to canonical non-www hostnames
RewriteCond %{HTTP_HOST} ^www\.(([^.]+\.)*)example\.com
RewriteRule (.*) http://%1example.com/$1 [R=301,L]
#
# Internally rewrite "blah" hostname requests to
# /subdomains subdirectory if not already done
RewriteCond %{HTTP_HOST} ^blah\.example\.com
RewriteCond $1 !^subdomains/
RewriteRule (.*) /subdomains/blah/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?blah\.example\.com
RewriteRule ^(.*)$ "http://example.com/subdomains/blah/$1" [R=301,P,L]
Can't understand why jdMorgan's code was giving me an error...perhaps if I just put the real names of the subdomain, etc. I can understand better...
Here's what I have as of now...
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?acs\.example\.com
RewriteRule ^(.*)$ "http://example.com/subdomains/acs/$1" [R=301,P,L]
Ok, so if you go to acs.graphicsmayhem.com it will stay at acs.graphicsmayhem.com and not change to graphicsmayhem.com/subdomains/acs, how can I use jdMorgan's example above to accomplish this?
[edited by: HungDry at 5:10 pm (utc) on Jan. 4, 2009]
[edited by: jdMorgan at 5:14 pm (utc) on Jan. 4, 2009]
[edit reason] example.com [/edit]
Then post the relevant contents of your server error log if you get a 500 error.
Jim
Not Found
The requested URL /subdomains/acs/ was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
--------------------------------------------------------------------------------
Apache/1.3.41 Server at acs.example.com Port 80
here is the exact code I am using:
RewriteEngine on
# Externally redirect to canonical non-www hostnames
RewriteCond %{HTTP_HOST} ^www\.(([^.]+\.)*)example\.com
RewriteRule (.*) http://%1example.com/$1 [R=301,L]
#
# Internally rewrite "blah" hostname requests to
# /subdomains subdirectory if not already done
RewriteCond %{HTTP_HOST} ^acs\.example\.com
RewriteCond $1 !^subdomains/
RewriteRule (.*) /subdomains/acs/$1 [L]
[edited by: jdMorgan at 5:50 pm (utc) on Jan. 4, 2009]
[edit reason] Please do not post your domain. See Terms of Service. [/edit]
[edited by: jdMorgan at 5:52 pm (utc) on Jan. 4, 2009]
[edit reason] Do not post your domain. See Terms of Service. [/edit]
What you want is to link to the URL http://acs.example.com/ on your pages, and have the content served from the server filepath /subdomains/acs directory when that link is clicked and that URL is requested by the HTTP client.
A URL is not a filepath, or vice-versa; The URL is defined on your Web pages, while the filepath is defined by your server directory structure. The main function of a server is to transparently map HTTP-requested URLs to server filepaths, and mod_rewrite simply allows doing so in an indirect, non-default way (in addition to the several other functions that it supports).
Your requirements call for an internal rewrite, not an external redirect (which by definition sends a new URL to the browser which effectively causes the browser to update its address bar).
In addition, the hostname canonicalization redirect I posted is needed to prevent duplicate-content problems and to simplify the internal rewrite code.
The code I posted works on thousands of servers, so the problem is no longer in code design, but in implementation or in server configuration.
Please post the relevant contents of your server error log, as requested above. Progress will be limited without the information it contains.
Please do not post your domain name, as I will have to edit it out every time (as above) to comport with our Terms of Service. You may also find this thread out-ranking your own site for searches on the domain name if I do not do so.
Jim
[edited by: jdMorgan at 6:08 pm (utc) on Jan. 4, 2009]
acs.example.com/subdomains/acs/
when it should be trying to get it from:
example.com/subdomains/acs/
so i am getting an error saying:
The requested URL /subdomains/acs/ was not found on this server. And I believe it's looking for it in acs.example.com
so I think I just need to fix the path where it's loading from,
which probably just needs to be changed somewhere in the .htaccess file from acs.example.com to example.com. Does that sound about right to you?
[edited by: HungDry at 6:57 pm (utc) on Jan. 4, 2009]
If the acs subdomain is hosted in the same filespace as the main domain, then you can rewrite to that filespace.
If the acs subdomain is hosted in a separate filespace with its own DocumentRoot declared, then what you want won't be possible to do, or to do efficiently, because in order to reach a different virtual server, a redirect or a proxy through-put will be needed. If you redirect, then the browser will update its address bar. If you do a proxy, then all traffic for "acs" will pass through the main server -going and coming- and will be logged. In addition, the 'acs' server will see all requests as coming from the main server and not the originating client, unless you configure the main server to send the x-forwarded-for header, and configure the acs server to log that header instead of the remote_host header.
How did you create and/or define the 'acs' subdomain -- WHat tool or method did you use?
Jim
If you don't like the way the control panel does this, then you'll need to change from a name-based virtual hosting account to an IP-based virtual hosting account, so that you can map IP addresses (and therefore domains) to any filespace you choose.
Alternatively, if you host allows directly-editing the httpd.conf file (not likely with a shared host), you could set the DocuemntRoot of the cvs subdomain to be the same as that for the main domain, and thereby guarantee that your main domain's .htaccess code would be run for cvs subdomain requests.
Jim
If you do a proxy, then all traffic for "acs" will pass through the main server -going and coming- and will be logged. This will massively clutter the stats for that domain. In addition, the 'acs' server will see all requests as coming from the main server and not the originating client, unless you configure the main server to send the x-forwarded-for header, and configure the acs server to log that header instead of the remote_host header.
This is a lot of extra work, and can be completely avoided by doing things the other way. That other way is much more clean, more efficient, and a whole lot better.
Jim
RewriteEngine On
# Externally redirect to canonical non-www hostnames
RewriteCond %{HTTP_HOST} ^www\.(([^.]+\.)*)domain\.net
RewriteRule (.*) [%1domain.net...] [R=301,L]
#
# Internally rewrite "blah" hostname requests to
# /subdomains subdirectory if not already done
RewriteCond %{HTTP_HOST} ^adam\.domain\.net
RewriteCond $1 !^cms/
RewriteRule (.*) /cms/adam/$1 [L]
I was expecting that to rewrite adam.domain.net into domain.net/cms/adam, thus executing the software installed in that folder.
Made sure I did a "hard refresh" in the browser - control+reload.
Get 500 error, here it is:
[Sat Jan 17 00:08:23 2009] [error] [client 68.38.xx.xx] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
Using Bluehost, originally used cpanel to set up subdomain - with redirection, then without redirection.
Deleting the subdomain in cpanel altogether leaves X.domain.net in the address bar of the browser, but the software in domain.net/cms/X is not run (the page that appears if you go to domain.net appears instead). No server errors in this scenario.
Any ideas?
Also, is it possible to wildcard the subdomains, so that literally whatever subdomain I enter is rewritten to the appropriate subfolder? Would prefer not to hard code the mappings.
Really sorry to ask, but have been googling for hours and doing tons of experimentation. Just get to a point where no hair is left... :(
[edited by: eelixduppy at 5:14 pm (utc) on Jan. 18, 2009]
[edit reason] obfuscated IP [/edit]
Careful inspection of the input URL, the wanted filepath, and the rules might point to a modification you can do to the rule to prevent that happening. Often a negative match
RewriteCond looking at %{THE_REQUEST} can be used.