mod_rewrite

Apache Module mod_rewrite

Serving files from a folder outside the web root with mod_rewrite

I was serving static content within a drupal installation, which became a huge nightmare when doing upgrades. So, I decided, I should just create a symbolic link to a folder outside the installation and route the traffic via mod_rewrite rules.

I first created a folder called "/var/www/site_files" and then symlinked to it from "/var/www/html/site_files". This way, I just have to recreate one single symbolic link after doing the upgrades.

With the below rules, I was able to maintain the links to the existing pages.

  ## BEGIN: site_files ##
  # Redirect to the homepage if site_files is being called
  RewriteRule ^site_files/?$ http://%{HTTP_HOST}/ [L,R=301]
  # Apped / at the end of url, if directory is being called without /
  RewriteCond %{REQUEST_URI} !^/?$
  RewriteCond %{DOCUMENT_ROOT}/site_files%{REQUEST_URI} -d
  RewriteRule ^(.+[^/])$ /$1/ [R=301,L]
  # Get directory or file if exists in site_files directory
  RewriteCond %{REQUEST_URI} !^/$
  RewriteCond %{DOCUMENT_ROOT}/site_files%{REQUEST_URI} -d [OR]
  RewriteCond %{DOCUMENT_ROOT}/site_files%{REQUEST_URI} -f
  RewriteRule ^(.*)$ site_files/$1 [L]
  ## END: site_files ##

Syndicate content
Comment