adn
adn

Reputation: 917

configure .htaccess to use a subfolder as root and mask the url

I've been trying several solutions in the internet and can't make the redirect voodoo to work.

Setup

My setup is a website build with jekyll, and I want to consume it as mydomain.com/~user.

The folder structure is

~user
  - _site
    - index.html
    - folder1
      - index.html
    - folder2
      - index.html
  - other_stuff

Problem

I was able to redirect from mydomain.com/~user to mydomain.com/~user/_site. So, half way through. The problem came when I tried to mask and remove the _site from the URL again. And I hit a wall.

Basically I want to use: mydomain.com/~user and see that URL in the browser, while beeing served the page that lives in mydomain.com/~user/_site/index.html. And same goes for other links

Request                    See                         Served
=======                    =======                     =======
mydomain.com/~user         mydomain.com/~user          mydomain.com/~user/_site/
mydomain.com/~user/folder1 mydomain.com/~user/folder1  mydomain.com/~user/_site/folder1

What I try

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/~user/_site/?
RewriteRule ^(.*)$ /~user/_site/$1


RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteRule !^~user/_site(.*)$ /~user/_site$1 [L]


RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteRule ^~user/_site(.*)$ /~user$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteRule !^~user/_site(.*)$ /~user/_site$1 [L]

However, all these

Partial solution

After tweaking the proposed solution, I was able to identify one variation that partially works

RewriteEngine On
RewriteBase /~user
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !^/_site/? [NC]
RewriteRule ^(.*)$ _site/$1 [L]

Note that in the last two line, i remove the ~user from the regex. This redirects all subfolders and pages, and allows other folders that are in ~user to be accessed.

However, the "home" mydomain.com/~user is the only one that is not redirected. As it exists the RewriteCond %{REQUEST_FILENAME} !-d doesn't allow it to be processed.

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule ^/?$ _site/ [L]

To redirect the home (~user) to mydomain/~user/_site, but it doesn't work. And tried variations of ^/?$ and ^$. But they don't work either.

Ideas?

Upvotes: 0

Views: 54

Answers (1)

Panama Jack
Panama Jack

Reputation: 24448

You should try it this way.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !^/~user/_site/? [NC]
RewriteRule ^~user/(.*)$ /~user/_site/$1 [L]

Upvotes: 1

Related Questions