Forum Moderators: phranque

Message Too Old, No Replies

adding subdomain rewrite to .htaccess

         

passmaster16

6:36 am on Jun 29, 2009 (gmt 0)

10+ Year Member



Hi,

I've been using the following code to rewrite my main site (mainsite.org) which resides a subdirectory off the root (public_html/sitea). This code has been working fine for months.


Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteBase /

#Change http://example.com to http://www.example.com (Optional)
RewriteCond %{HTTP_HOST} ^mainsite.org$
RewriteRule ^/?(.*)$ [mainsite.org...] [R=301,L]

# Rewrite http://www.example.com/ to http://www.example.com/subdir
RewriteCond %{REQUEST_URI} !^/sitea
RewriteRule ^(.*)$ sitea/$1 [L]

Now I'd like to add a subdomain called newsubsite.mainsite.org which resides in a different subdirectory off the root (public_html/siteb). I went ahead and created a new subdomain in cPanel so that newsubsite.mainsite.org points to /public_html/siteb. I then added the code below. The subdomain does not work. I'm getting a 500 internal server error. My .htaccess resides in the root directory (public_html). I was wondering it the problem is with my rewrite condition or the fact that I have the subdomain in cPanel set to use the subdirectory (siteb).


RewriteCond %{HTTP_HOST} ^newsubsite\.mainsite\.org$
RewriteCond %{REQUEST_URI} !^/siteb
RewriteRule ^(.*)$ /siteb/$1 [L]

Any insight would be greatly appreciated.

Thanks

jdMorgan

1:28 pm on Jun 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



THe problem is likely that cPanel maps siteb directly to the subdirectory (using code in the server config file much like what you've got for siteA in .htaccess). Therefore, your code will never be executed for siteB.

However, requests for siteB *should* already be getting mapped to the siteB subdirectory, so the 500-Server error may be due to some other problem. Look at your server error log to find out what the problem is.

The problem with control panel add-on domains is that they cannot share respurces with your main domain. If you wish to host one or more additional (sub)domains, look into IP-based shared hosting. With IP-based shared hosting, your site has a unique IP address, and you can point as many domain at it as you like. The control panel is *not* used to set up these additional domains, since it is not necessary to declare the additional (sub)doamins at the server level (the (sub)domains need only be declared in your DNS Zone file), and you can then use mod_rewrite to 'map' these additional (sub)domains to any directory structure you like. In addition, since they are all mapped to the same filespace, they can freely share resources (images, scripts, etc.)

Cost in the U.S. for a unique IP address is about $12 per year.

Jim

passmaster16

1:43 pm on Jun 29, 2009 (gmt 0)

10+ Year Member



Thanks jd. I agree a seperate IP may be the way to go. I was just trying to find a quick and easy fix as this a volunteer work for a nonprofit -- just a hassle to have to buy anything.

Anyway, I'll have to check the logs out. In regard to the 500 server error, when I remove that second block of code out,


RewriteCond %{HTTP_HOST} ^newsubsite\.mainsite\.org$
RewriteCond %{REQUEST_URI} !^/siteb
RewriteRule ^(.*)$ /siteb/$1 [L]

what I get is a 404 not found error and the subdomain is trying to redirect to /siteA/siteB/ so it thinks that my new site folder is nested below the existing site folder. So if it wasn't excuting the code at all, why would it throw a server 500 error when the second block is added?

jdMorgan

10:11 pm on Jun 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, maybe it *is* executing it... Were you able to specify the "root directory" for siteB when you added it? (If so, that's a better control panel set-up than most I've seen.)

If the code is being executed because siteB's root is the same as siteA's, then you'll need to exclude both site-subdirectories from both rules to prevent recursion. Based on your code posted above, you could replace the second RewriteCond in both rules with:


RewriteCond $1 !^(sitea¦siteb)/

You'll need to replace that broken pipe "¦" with a solid pipe before use; Posting on this forum modifies the pipe character.

Note that we're using the requested URI as captured by the RewriteRule pattern in $1 here, so the pattern does not start with a slash.

Jim

passmaster16

5:58 pm on Jun 30, 2009 (gmt 0)

10+ Year Member



Jim,

The control panel will *appears* to let me specify whatever I want as the root directory for the subdomain. Right now, I am using /public_html/siteb as the root for my new site. But I was thinking that maybe I should be specifying just /public_html in the control panel as this is where .htaccess is located and that my existing site which is sitea exists in /public_html/sitea You're probably right with your earlier assessment that it shouldn't execute anything in .htaccess if the control panel is making the change in a server config file.

Maybe I'll set the subdomain alias to point at /public_html and add in the code you have above to prevent the recursion and see how that works.

I'll report back with my results. Thanks Jim!

jdMorgan

6:43 pm on Jun 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I suggest you change only the .htaccess code first. If that fixes your 500-Server Error, then you are done with this project.

Good rule: Change only one thing at a time if you want to be able to ascribe results to actions...

Jim

passmaster16

8:42 pm on Jun 30, 2009 (gmt 0)

10+ Year Member



Jim, you're right about making one change at a time. I went ahead and changed both:

RewriteCond %{REQUEST_URI} !^/sitea
RewriteCond %{REQUEST_URI} !^/siteb

to instead use

RewriteCond $1 !^(sitea¦siteb)/

Now it seems to be working as it should. A request for mainsite.org or www.mainsite.org is rewritten to public_html/sitea and requests for newsubsite.mainsite.org are rewritten to public_html/siteb I made no changes to my subdomain in cPanel so it still points to public_html/siteb

The only thing that isn't working at this point is when I enable SEO-friendly URLs for the Joomla in siteb (newsubsite.mainsite.org). Joomla has an option to use .htaccess. When I enable that option along with SEO-friendly URLs, I get a 404 not found error when I try to click on any page linked off of newsubsite.mainsite.org. An example error is The requested URL /subpage1.html was not found on this server.

Sitea (www.mysite.org) is not affected by this even though it too is a Joomla site with SEO-friendly URLs/.htaccess enabled and is located in public_html/sitea. Turning off SEO optimization for siteb fixes the problem, but I'd like to use SEO-friendly URLs if possible. Any ideas on what might be going on? I was thinking that once the requests were being rewritten to siteb, that maybe it doesn't know how to find its path back to these subdirectories when I enable SEO.

Thanks much for your insight.

jdMorgan

10:25 pm on Jun 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Look at your server error log, which will show the *filepath* that is failing, as opposed to the server access log, which shows the *URL*. That information is usually quite informative.

Jim

passmaster16

3:53 pm on Jul 1, 2009 (gmt 0)

10+ Year Member



Hi Jim,

Turns out it was my fault. I thought the .htaccess under the /public_html/siteb directory was enabled, but it wasn't. I had to rename the htaccess.txt to .htaccess. Now it seems like the subdomain is working with SEO. Just assumed that selecting the option within the Joomla panel would automatically enable it -- apparently not.

I just had to final question to wrap up this thread.

1.) What is the RewriteCond $1 !^(sitea¦siteb)/ condition doing differently than the

RewriteCond %{REQUEST_URI} !^/sitea
RewriteCond %{REQUEST_URI} !^/siteb

2.) Is there any reason why this isn't a good way of setting up two websites on one host besides the fact that one is using the subdomain while the other is not?

Thank you.

jdMorgan

5:37 pm on Jul 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) Nothing really, except that it'll run faster in a .htaccess context. In .htaccess, your rules are compiled for each and every HTTP request, and then executed. In a server-config context, your rules are only compiled once when the server is restarted, and then only executed on a per-HTTP-request basis. So, in .htaccess, you have the compile time plus the execution time for every request, whereas in a server config file, you have only the execution time. So in .htaccess context, the shorter the code, the better.

In this case, using a single RewriteCond and a local 'OR' is faster for compilation, and using the back-reference to the URL-path already captured in $1 by the RewriteRule pattern is also faster, since it uses fewer characters that %{REQUEST_URI}.

A 'hybrid' approach would be to use

 RewriteCond %{REQUEST_URI} !^/(sitea¦siteb) 

but again, there's no practical difference except for the number of characters that have to be parsed by the mod_rewrite compiler.

In httpd.conf or other server-config files, using separate RewriteConds turns out to be faster in the per-HTTP-request code-execution stage, based on some benchmarking results contributed several years ago by member Andreas_Friedrich.

2) I'm not sure what you mean by this question, but given the choice, for simplicity and performance, you'd want to set up two virtual servers at the server config level instead of doing this in .htaccess. But it's not an intrinsically *bad* way of doing it if you've got limited server privileges and .htaccess is your only practical choice (Alternatively, you could pass *all* requests to a script to sort out the subdomain->filesystem mapping if you wanted to do it that way, but that'd be even less efficient).

BTW, both of your sites are "using a subdomain," since "www" is a subdomain of example.com, just as "newsubsite" is. And both are mapped (i.e. rewritten) to subdirectories in this case, so there is no practical difference at all in how they're being handled. (Not to be pedantic, but I'm a big fan of using correct terminology, as it increases understanding, resulting in far less confusion and helping to dissipate the 'magic' and superstition surrounding server config issues.) :)

Jim

passmaster16

8:46 pm on Jul 2, 2009 (gmt 0)

10+ Year Member



Hi Jim, you're right on the terminology. Sometimes we forget that www is a subdomain...especially nowadays where the emphasis on the usage of www in URLs is diminished.

One last thing that I noticed in relation to that. If user enters www.newsubsite.mainsite.org, the site comes up but the www persists.

I thought I could fix this by adding a condition that looks for that pattern before rewriting to the siteb folder.

The code would look like this

RewriteCond %{HTTP_HOST} ^www\.newsubsite\.mainsite\.org$
RewriteRule ^/?(.*)$ [newsubsite.mainsite.org...] [R=301,L]

RewriteCond %{HTTP_HOST} ^newsubsite\.mainsite\.org$
RewriteCond $1 !^(sitea¦siteb)/
RewriteRule ^(.*)$ /siteb/$1 [L]

I boldfaced the two lines I added. I though this would redirect www.newsubsite.mainsite.org to simply newsubsite.mainsite.org -- I really don't want to use the "www" for the subdomain if I can avoid it. Unfortuantely this pattern doesn't appear to do anything for me. Typing www.newsubsite.mainsite.org still yields www.newsubsite.mainsite.org in the address bar -- no redirection taking place. What am I doing wrong?

Thanks

jdMorgan

3:11 am on Jul 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> What am I doing wrong?

Nothing that I can see... Did you flush your browser cache before testing? Is the code in a directory-path that will be traversed when serving that request?

Note that you should not end-anchor hostnames in positive-match patterns, or if you do, then include subpatterns to match FQDNs or appended port numbers. That is, us

RewriteCond %{HTTP_HOST} ^www\.newsubsite\.mainsite\.org

-or-
RewriteCond %{HTTP_HOST} ^www\.newsubsite\.mainsite\.org$\.?(:[0-9]+)?$

www.newsubsite.mainsite.org.:80 is a perfectly valid, but non-canonical hostname, and it would not get redirected by your rule as-is.

Jim

passmaster16

10:05 pm on Jul 3, 2009 (gmt 0)

10+ Year Member



Jim, Yes I flushed my browser cache. I also tried changing the condition to

RewriteCond %{HTTP_HOST} ^www\.newsubsite\.mainsite\.org

It didn't make a difference. The .htaccess file is located in the root of /public_html so I would think it should have the access. Definitely odd that it doesn't want to redirect the www

The only reason I'm really concerned with it is because I don't want Google to penalize me for duplicate content.