1

I currently have a blog setup as the subfolder of the main site, mydomain.com.au/blog/, however the /blog is dynamically generated through PHP and doesn't physically exist.

I have created a subdomain blog.mydomain.com.au and I am trying to get it to display everything just like mydomain.com.au/blog/ but mask the url so that it shows blog.mydomain.com.au.

I have made several attempts using htaccess and got close a couple of times, but there's always something wrong. Below are the 2 attempts which got me the closest to the expected results:

1st attempt

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^blog\.mydomain\.com\.au$
RewriteRule ^.*$ http://www.mydomain.com.au/blog%{REQUEST_URI} [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

This will get blog.mydomain.com.au to redirect to www.mydomain.com.au/blog/, but does not mask the URL (the address bar will show www.mydomain.com.au/blog/).

2nd attempt

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com\.au$ [NC]
RewriteRule blog/(.*) http://blog.mydomain.com.au/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

This got me the closest. It redirects and masks the URL just the way I want it to for all pages under /blog/ (e.g. blog.mydomain.com.au/whatever/ redirects to www.mydomain.com.au/blog/whatever/ but masks the URL as blog.mydomain.com.au/whatever/), but it doesn't do it for blog.mydomain.com.au, which gets redirected to www.mydomain.com.au.

Dimitri Dewaele
  • 10,311
  • 21
  • 80
  • 127

1 Answers1

0

You have two different requirements here. The first one is a rewrite, which will work, if the request is blog.mydomain.com.au and you remove the domain part and the R flag in the rule substitution

RewriteCond %{HTTP_HOST} ^blog\.mydomain\.com\.au$ [NC]
RewriteRule !^blog/ /blog%{REQUEST_URI} [L]

The second one is an actual redirect, if the request is www.mydomain.com.au/blog/

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com\.au$ [NC]
RewriteRule blog/(.*) http://blog.mydomain.com.au/$1 [R,L]

When everything works as you expect, you can change R to R=301.

Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • thanks for the reply and the very useful link! I have tried your rule, but the same thing happens. blog.mydomain.com.au redirects to www.mydomain.com.au. Moreover, strangely enough, it works a few times and then it doesn't... Having cleared the browser cache after adding the rule I tested by clicking a few links on the blog. They were going to blog.mydomain.com.au/whatever, but after a few clicks (maybe 6) it went back to www.mydomain.com.au/whatever... I switched back to my old rule, but changed the 301 to a 302 redirect. – Razvan Costica Apr 24 '13 at 03:51
  • @RazvanCostica I guess, the links in the blog are sometimes relative links and sometimes you have absolute links including the domain. – Olaf Dietsche Apr 25 '13 at 00:47
  • The scenario is like this: i have a website with an integrated blog and now I am trying to move the blog from www.mydomain.com.au/blog to blog.mydomain.com.au, as per my client request, without actually having to export everything from the blog and recreating it under the blog folder, as this would be pretty time consuming. So all the links in the blog have the URL structure www.mydomain.com.au/blog/whatever/ and trying to redirect them to blog.mydomain.com.au/whtever. – Razvan Costica Apr 25 '13 at 05:30
  • Now, I have managed to do that for all the urls (no matter if you type them manually in the address bar or clock on the already existing links), except that the document root blog.mydomain.com.au will redirect to www.mydomain.com.au instead of www.mydomain.com.au/blog/. I used the following code faa suggested: `RewriteCond %{HTTP_HOST} !(www\.)?blog\.raca\.com\.au [NC] RewriteRule ^blog/?(.*)$ http://blog.raca.com.au/$1 [L,R]` Any suggestions? – Razvan Costica Apr 25 '13 at 05:30
  • @faa Sorry is I am pushy, but my ISP told me everything is fine on their end and told me to properly configure my htaccess. But I am thinking it might indeed be a problem with the htaccess configuration, because if there was a problem on the main apache config files, my first rule wouldn't redirect to the /blog/, right? Is there a way to combine the code you gave me with my first rule? Thanks again! – Razvan Costica Apr 25 '13 at 05:57
  • @RazvanCostica Please see the updated post. Both rules together should move the blog from `www.mydomain.com.au/blog/` to `blog.mydomain.com.au`. – Olaf Dietsche Apr 25 '13 at 10:20
  • Thank you for your help Olaf, but when added (together with the second rule or by itself), the first rule breaks the site with an Error 500. The first rule by itself redirects everything just fine, except for the blog document root which redirects to the site document root. – Razvan Costica Apr 25 '13 at 13:33
  • I wrote this observation before, but it got deleted: the funny thing is that even though all urls are forced with a trailing slash, neither the site nor the blog document roots force the trailing slash. Maybe it's got something to do with why the blog document root redirects to the wrong place? – Razvan Costica Apr 25 '13 at 13:59
  • @RazvanCostica This shouldn't happen. In my test environment `blog.mydomain.com.au` is properly rewritten to `www.mydomain.com.au/blog/`. There must be another rule or directive causing the redirect to the main site. Do you have `blog.mydomain.com.au` defined as a `ServerAlias`? – Olaf Dietsche Apr 25 '13 at 18:32
  • My ... ISP had set blog.mydomain.com.au A record to point at www.mydomain.com.au. Now they have set it to the IP. I will let you know how it goes once the domain has propagated. Thanks again for your help! – Razvan Costica Apr 26 '13 at 03:31