Use a subdirectory as root with htaccess in Apache 1.3

42,187

Solution 1

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.

Solution 2

You can simply use:

RewriteBase   /jekyll

And everything starts from that point.

Solution 3

The only simple solution worked for me is here

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.com$
RewriteCond %{REQUEST_URI} !folder/
RewriteRule (.*) /folder/$1 [L]

Put this code into your .htaccess file.

domain-name.com – Type your own domain name

folder – Type the name of the subfolder which has the test/development website

Share:
42,187
Andrew
Author by

Andrew

I'm currently working on a PhD in Public Policy at the Sanford School of Public Policy at Duke University, where I'm studying education and freedom of expression INGOs that work in Egypt and China. Life rocks.

Updated on September 25, 2020

Comments

  • Andrew
    Andrew over 3 years

    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…