Search code examples
htmlapache.htaccess

Rewriter URLs for files in subfolder


I have a question regarding configuring apache for the following setting:

  1. URLs like http://www.example.com/abc.html should point to DOCUMENT_ROOT/abc.html
  2. URLs like http://www.example.com/_dev/abc.html should point to DOCUMENT_ROOT/_dev/abc.html
  3. URL in HTML files served from DOCUMENT_ROOT/_dev should be rewritten from "/abc.html" to "/_dev/abc.html" automatically

Solution

  • You can use this code in your DOCUMENT_ROOT/.htaccess file:

    RewriteEngine On
    RewriteBase /
    
    # if not a file
    RewriteCond %{REQUEST_FILENAME} !-f
    # if not a directory
    RewriteCond %{REQUEST_FILENAME} !-d
    # if URI is not starting with /_dev/ then rewrite to /_dev/<uri>
    RewriteRule ^((?!_dev/).*)$ /_dev/$1 [L,NC]