Thomas Tempelmann
Thomas Tempelmann

Reputation: 12079

Relocating the DocumentRoot using .htaccess files

I am trying to get all http requests to go to a different local directory than where DocumentRoot points to:

The web server config:

<VirtualHost 1.2.3.4:80>
DocumentRoot /local/home/me/example.com
ServerName   example.com

However, the entire (PHP) code for this site is not at DocumentRoot but rather in a subdir:

/local/home/me/example.com/code/

Currently, to access the site, I have to use URLs that include the /code/ subdir. I like to get rid of that so that http://example.com/ accesses /local/home/me/example.com/code/

I understand that the right way is to make DocumentRoot point to the subdir. My web hoster won't let me change this setting, though (in fact, that's how I had it before, when I hosted this site on my private web server where I could set the DocumentRoot just like that).

I now hope I can accomplish this with rewrites in .htaccess file(s). I just don't understand how.

My questions, in particular:

Upvotes: 0

Views: 72

Answers (1)

anubhava
anubhava

Reputation: 785276

You can use this code in your /local/home/me/example.com/.htaccess file:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!code/).*)$ code/$1 [L,NC]

(?!code/) is negative lookahead that will check if request already doesn't start with /code/ and will add this if condition is true.

Upvotes: 1

Related Questions