I am trying to setup a system on my client’s shared hosting plan where nothing will live on the root of the server. We’re going to have 2 folders in the root - live and dev.
What I want to do is to redirect the domain name so that it points to the live folder (and all URLs will point to files in this folder), and then have a subdomain - dev.example.com that points to the dev folder.
Below is the .htaccess file I wrote with that doesn’t seem to be working.
Unless this is a client requirement: WHY? IMHO, it’s a waste of your time and effort (unless, of course, you’re being paid by time) to rearrange your directories like this. Using a “hidden” live directory will cause unnecessary problems with location (URI) as ALL your links will have to be relative to the DocumentRoot to keep the URI hidden - until someone wants to view an image which will then show live/images/filename.
Repeating a website (at least with relative links) in a dev subdirectory is just fine - but require that it be accessed via dev.example.com else throw it back at the DocumentRoot (live in your case).
Comments on code:
RewriteEngine On
#RewriteCond %{HTTP_HOST} example[COLOR="Blue"]\\[/COLOR].com [COLOR="Blue"][NC][/COLOR]
[COLOR="Red"]# serves no useful purpose
# {HTTP_HOST} is case insensitive[/COLOR]
#RewriteCond %{REQUEST_URI} !live/
[COLOR="Red"]# will redirect dev/, too![/COLOR]
#RewriteRule ^(.*)$ live/$1 [L]
RewriteCond %{HTTP_HOST} [COLOR="Blue"]([/COLOR]www[COLOR="Blue"]\\[/COLOR].[COLOR="Blue"])?[/COLOR]example[COLOR="Blue"]\\[/COLOR].com [COLOR="Blue"][NC][/COLOR]
[COLOR="Red"]# don't specify only www - what about the non-www case?
# the No Case flag is needed because {HTTP_HOST} is not case sensitive[/COLOR]
RewriteCond %{REQUEST_URI} !live/
RewriteRule ^(.*)$ live/$1 [L]
# this will, as desired, redirect EVERYTHING (and nothing) to live/
RewriteCond %{HTTP_HOST} dev\\.example[COLOR="Blue"]\\[/COLOR].com [COLOR="Blue"][NC][/COLOR]
[COLOR="Red"]# escape to ensure dot characters
# No case flag for case insensitive {HTTP_HOST}[/COLOR]
RewriteCond %{REQUEST_URI} !dev/
RewriteRule ^(.*)$ dev/$1 [L]
[COLOR="Red"]# this will, as desired, redirect EVERYTHING (and nothing) to dev/ IF the dev subdomain has been requested
# ... BUT, doesn't the dev subdomain already point to the dev subdirectory?[/COLOR]