Forum Moderators: phranque
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
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
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?
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)/
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
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!
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.
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.
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) 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
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
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 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
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.