Apache/Rewrite

From braindump
Revision as of 01:21, 24 December 2013 by Uroesch (talk | contribs) (Created page with "{{DISPLAY_TITLE:Apache mod_rewrite recipes}} == Recipes == === Redirect to file with different extension == Assuming some file have changed from <tt><basename>.html</tt> to <t...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Template:DISPLAY TITLE:Apache mod rewrite recipes

Recipes

= Redirect to file with different extension

Assuming some file have changed from <basename>.html to <basename>.php. The server should only redirect if the <basename>.html file does not exist.

<IfModule mod_rewrite.c>
  RewriteEngine Off
  ## RewriteLog  /var/tmp/rewrite_log
  ## RewriteLogLevel  9
  RewriteCond %{DOCUMENT_ROOT}$1 !-f
  RewriteCond $3 ^.html?$
  RewriteRule ((.*)(\.html?)) $2.php [L]
</IfModule>

The first RewriteCond is to check if the file exists or not the second one determines if the extension is either .htm or .html. Finally the RewriteRule swaps out the file extension and hands it back [L].