Transparently redirect your root directory to a subdirectory

From DreamHost
Jump to: navigation, search

Overview

Depending on how your site is constructed, it may be necessary to redirect requests for the primary URL to a subdirectory. For example, requests to example.com could go to example.com/blog. This is most often necessary with One-Click Installs.

Visit the (Panel > ‘Goodies’ > ‘One-Click Installs’) page to view available options.

When installing a One-Click Install:

  • The target directory must be completely empty to succeed.
  • For example, if you want to put the installation in the directory /example.com, this directory must be empty and contain no files.
  • Another solution is to install the software into an empty subdirectory such as example.com/blog; then, you could create rules in an .htaccess file to automatically redirect requests for example.com to example.com/blog.

Redirect requests with .htaccess

You can redirect all requests to a subdirectory by adding an .htaccess file to the root of your domain’s directory:

  1. Visit the FTP page for instructions on how to upload.
  2. Once connected, upload (or create) a text file named .htaccess (with no extension).
  3. Make sure it’s uploaded to your domain’s directory such as example.com.
  4. Add the following content to this .htaccess file:
    RewriteEngine on
    RewriteBase /
    
    RewriteCond %{REQUEST_URI} !^/blog/
    
    # Rewrites all URLS [Replace "domain" with the actual domain, without the TLD (.com, .net, .biz, etc)]
    RewriteCond %{HTTP_HOST} ^(www\.)?domain\.
    
    # Rewrite all those to insert /folder
    RewriteRule ^(.*)$ /blog/$1 [L]
    

This example will redirectory all requests for a website automatically to a subdirectory named /blog. If you need to redirect to another directory, just change the two references of /blog to your subdirectory.

---

The following example provides an alternative method of transparently redirecting. It works with any subdomain (including root/naked domains) and only transparently redirects a root request, while allowing directly requested subdirectories like domain.com/other to be accessed.

RewriteEngine on
RewriteCond %{HTTP_HOST} domain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /blog/$1 [L]

Redirecting in WordPress

The redirect feature is included in WordPress. Visit their codex page for instructions on Giving WordPress Its Own Directory.