R – .htaccess rewrite and subdomains

.htaccessapachemod-rewrite

I have a subdomain setup as onlinedev.domain.com
I need to use htaccess to rewrite to domain.com/online_content, while still showing onlinedev.domain.com in the address bar (SSL is for onlinedev.domain.com).
this is what I currently have that is very close:

php_flag display_errors off

RewriteEngine On 
RewriteCond %{HTTP_HOST} !^$ 
RewriteCond %{HTTP_HOST} onlinedev\.domain\.com$ [NC] 
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC] 
RewriteCond %2<->%3 !^(.*)<->\1$ [NC] 
RewriteRule ^(.+) /%2/$1 [L]

This correctly rewrites to domain.com/onlinedev, but if I try to change the RewriteRule to:

RewriteRule ^(.+) /online_content/$1 [L]

I get an error

I understand that there are typically better ways to do this subdomain work, but without getting into server config and DNS details, I need to do it with htaccess.

And yes, I do need to rewrite to a directory that has a different name than the subdomain.

Best Answer

Well, I figured it out. The issue was that I was causing an infinite loop. Once the rewrite had happened, it was still trying to rewrite to the directory. Here is my new htaccess that took care of it:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^$ 
RewriteCond %{HTTP_HOST} onlinedev\\.domain\\.com$ [NC] 
RewriteCond %{REQUEST_URI} !^/online_content/
RewriteRule ^(.+) /online_content/$1 [L]

Notice that I added a check to make sure that the REQUEST_URI is not the name of the directory I am rewriting to.

Related Topic