Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a subdirectory as root with htaccess in Apache 1.3

I'm trying to deploy a site generated with Jekyll and would like to keep the site in its own subfolder on my server to keep everything more organized.

Essentially, I'd like to use the contents of /jekyll as the root unless a file similarly named exists in the actual web root. So something like /jekyll/sample-page/ would show as http://www.example.com/sample-page/, while something like /other-folder/ would display as http://www.example.com/other-folder.

My test server runs Apache 2.2 and the following .htaccess (adapted from http://gist.github.com/97822) works flawlessly:

RewriteEngine On  # Map http://www.example.com to /jekyll. RewriteRule ^$ /jekyll/ [L]  # Map http://www.example.com/x to /jekyll/x unless there is a x in the web root. RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^/jekyll/ RewriteRule ^(.*)$ /jekyll/$1  # Add trailing slash to directories without them so DirectoryIndex works. # This does not expose the internal URL. RewriteCond %{REQUEST_FILENAME} -d RewriteCond %{REQUEST_FILENAME} !/$ RewriteRule ^(.*)$ $1/  # Disable auto-adding slashes to directories without them, since this happens # after mod_rewrite and exposes the rewritten internal URL, e.g. turning # http://www.example.com/about into http://www.example.com/jekyll/about. DirectorySlash off 

However, my production server runs Apache 1.3, which doesn't allow DirectorySlash. If I disable it, the server gives a 500 error because of internal redirect overload.

If I comment out the last section of ReWriteConds and rules:

RewriteCond %{REQUEST_FILENAME} -d RewriteCond %{REQUEST_FILENAME} !/$ RewriteRule ^(.*)$ $1/ 

…everything mostly works: http://www.example.com/sample-page/ displays the correct content. However, if I omit the trailing slash, the URL in the address bar exposes the real internal URL structure: http://www.example.com/jekyll/sample-page/

What is the best way to account for directory slashes in Apache 1.3, where useful tools like DirectorySlash don't exist? How can I use the /jekyll/ directory as the site root without revealing the actual URL structure?

Edit:

After a ton of research into Apache 1.3, I've found that this problem is essentially a combination of two different issues listed at the Apache 1.3 URL Rewriting Guide.

I have a (partially) moved DocumentRoot, which in theory would be taken care of with something like this:

RewriteRule   ^/$  /e/www/  [R] 

I also have the infamous "Trailing Slash Problem," which is solved by setting the RewriteBase (as was suggested in one of the responses below):

RewriteBase    /~quux/ RewriteRule    ^foo$  foo/  [R] 

The problem is combining the two. Moving the document root doesn't (can't?) use RewriteBase—fixing trailing slashes requires(?) it… Hmm…

like image 897
Andrew Avatar asked Mar 08 '10 14:03

Andrew


People also ask

Does htaccess work on subdirectories?

htaccess file are applied to the directory in which the . htaccess file is found, and to all subdirectories thereof. However, it is important to also remember that there may have been . htaccess files in directories higher up.

What is root subdirectory?

Subdirectories may refer to folders located directly within a folder, as well as folders that are stored in other folders within a folder. For example, the main directory of a file system is the root directory. Therefore, all other folders are subdirectories of the root folder.


2 Answers

Finally got it, after a week of trying. RewriteRules really are voodoo…

RewriteEngine On  # Map http://www.example.com to /jekyll. RewriteRule ^$ /jekyll/ [L]  # Map http://www.example.com/x to /jekyll/x unless there is a x in the web root. RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^/jekyll/ RewriteRule ^(.*)$ /jekyll/$1  # Add trailing slash to directories within jekyll # This does not expose the internal URL. RewriteCond %{SCRIPT_FILENAME} -d RewriteRule ^jekyll/(.*[^/])$ http://www.example.com/$1/ [R=301] 

No need for DirectorySlash. It magically all works.

like image 63
Andrew Avatar answered Oct 14 '22 02:10

Andrew


You can simply use:

RewriteBase   /jekyll 

And everything starts from that point.

like image 23
Marcos Placona Avatar answered Oct 14 '22 01:10

Marcos Placona