Forum Moderators: phranque

Message Too Old, No Replies

Subdomain problem

Redirection problem

         

iGeek

5:59 am on Mar 28, 2007 (gmt 0)

10+ Year Member



I have a subdomain www.gallery.mydomain.com. In my .htaccess file, I have this:


RewriteCond %{HTTP_HOST}!^www\. [NC]
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]

Which prints www. before my URL if not there. However, when I access my subdomain, it redirects to www.gallery.mydomain.com/gallery which obviously doesn't exist and Apaches spits a "Not Accessible" error. How do I fix this?

jdMorgan

12:49 pm on Mar 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From the symptoms, I'd guess that on your server, subdomains are being internally rewritten to subdirectories, as is common.

Look at your site using FTP or the host's "File manager" tool and see where in the filesystem the files for subdomains are stored -- It's likely they're stored in subdirectories below the main site.

Be sure that the non-www to www domain external redirection code happens before any other internal rewriting. Otherwise, you'll do the internal rewrite followed by the external redirect, which changes the browser address bar. And this 'exposes' the subdirectory structure, which is not good.

On servers where this problem comes up, it's often a matter of the code generated by the "Control Panel" and your .htaccess code interfering with each other. In that case, you can look into disabling the control panel or putting separate code into each subdomain's subdirectory instead of trying to handle everything in the root .htaccess file. Other than that, the choices aren't good: change hosts, or do without the .htaccess functions that don't work with the control panel code.

Jim

iGeek

1:37 pm on Mar 28, 2007 (gmt 0)

10+ Year Member



I lost you in the third paragraph.

Yes, my subdomains redirect to a sub directory inside public_html. So I need to rewrite the URL before the server redirects to the sub directory?

jdMorgan

2:16 pm on Mar 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Be sure that the non-www to www domain external redirection code happens before any other internal rewriting.

Do the domain redirect first.

> I lost you in the third paragraph.

When setting up subdomains, many webmasters must use the server's "Control Panel" -- A script that writes configuration directives to httpd.conf, conf.d, and .htacess files. These scripts are convenient, but limited in capability and flexibility. If the control panel writes code into httpd.conf or conf.d to do an internal rewrite (to create a file space for your subdomain), then that code will run before your .htaccess code, and this can 'break' your code.

If forced to use a control panel, sometimes it will allow you to specify where in the filesystem the subdomains' space should be created. In that case, the most flexible approach is to map all subdomains into the main domain's space (point all subdomains and domains to the root directory), and then use your own code in .htaccess to 'sort them out' into subdirectories. This duplicates what the control panel would have done by default, except that it allows you to control all of the code, and therefore, control its execution order.

I'm sorry if this isn't clear. Control panels and their options and implementations vary, so I'm attempting to generalize a complex subject.

In case it isn't clear, mod_rewrite can do two main things: It can generate a redirect, which sends a message back to the requesting browser saying, "The resource you have requested has moved. Ask for it again at this new URL." The server then provides the new URL specified in the code that invoked the redirect. This terminates the current HTTP transaction. The browser will then change its address bar, and generate a new HTTP request, asking for the originally-requested object using the new URL provided in the server's redirect response.

In contrast, an internal rewrite simply changes the filepath associated with a URL. By default, a filepath is generated by stripping off the hostname (domain) from the URL, and then prepending the contents of the DocumentRoot variable, as configured in httpd.conf or conf.d. So, you might ask for example.com/foo.html, and by default the server would strip off example.com, prepend /users/widgetco/var/html/public, and serve the results from the file at /users/widgetco/var/html/foo.html.

An internal rewrite can change that default mapping of "page name" to "file name" if desired, and cause this request to be served with the contents of the file at /users/widgetco/var/html/bar.htm, just for a simple example.

Anyway, it is important even for this simple example, that if both a domain redirect and an internal rewrite are to be done for some or all requests, that the external redirect be done first. Otherwise, the action of the internal rewrite will be revealed to the user by the action of the external redirect, and other untoward effects can occur as well. That's what I suspect is happening here.

Although it may at first seem to be a simple problem due to a small coding error, this is not the case; Your code is fine, but you've got a complex problem, and I suspect that your server configuration code and .htaccess code are working at cross-purposes.

Jim

iGeek

2:40 pm on Mar 28, 2007 (gmt 0)

10+ Year Member



Okay, I see some light...

I think I confused tech support; usually they reply within ten minutes and, well, nothing.

Thanks for your help, by the way.