David Lewis
David Lewis

Reputation: 190

.htaccess to redirect the root to a subfolder

Using .htaccess I am trying to redirect requests for the root of a site to a subdir. The three approaches I have tried either fail or leave something to be desired.

This fails with indefinite recursion:

Redirect / http://example.org/sub

These don't seem to work for a defaulted index.html:

Redirect / http://example.org/sub/index.html
Redirect /index.html http://example.org/sub/index.html

This works, but leaves the directory at the top level, which necessitates rewriting relative links in the html file:

DirectoryIndex http://example.org/sub/index.html

So, is there any straightforward way just redirect the root totally to a subfolder and have everything work there? I am trying to avoid the fancier features of .htaccess -- I'd probably just prefer the third alternative if it gets too complicated.

Upvotes: 0

Views: 74

Answers (1)

anubhava
anubhava

Reputation: 784898

In the DocumentRoot of old site have this rule:

RewriteEngine On

RewriteCond %{HTTP_HOST} !example\.org$ [NC]
RewriteRule ^ http://example.org/sub%{REQUEST_URI} [L,NE,R=301]

Upvotes: 1

Related Questions