Difference between revisions of "Apache/Rewrite"

From braindump
Jump to navigation Jump to search
Line 20: Line 20:
<IfModule mod_rewrite.c>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteEngine On

## Use when SEO - Adds suffix to URL = YES
## Use when SEO - Adds suffix to URL = YES
## RewriteCond %{REQUEST_URI} (/[^.]*|\.(html?|php))$ [NC]
## RewriteCond %{REQUEST_URI} (/[^.]*|\.(html?|php))$ [NC]

## use when SEO - Adds suffix to URL = NO
## use when SEO - Adds suffix to URL = NO
RewriteCond %{REQUEST_URI} (/[A-Z0-9-_]+)$ [NC]
RewriteCond %{REQUEST_URI} (/[A-Z0-9-_]+)$ [NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-d

Revision as of 21:26, 17 February 2015

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 On
  ## 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].


Joomla rewrite rule

Ensure in 2.5.x the Global Configuration -> SEO -> Use URL rewriting value is set to Yes.

<IfModule mod_rewrite.c>
  RewriteEngine On
  
  ## Use when SEO - Adds suffix to URL = YES
  ## RewriteCond %{REQUEST_URI} (/[^.]*|\.(html?|php))$  [NC] 
  
  ## use when SEO - Adds suffix to URL = NO
  RewriteCond %{REQUEST_URI} (/[A-Z0-9-_]+)$             [NC] 
  
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule .* /index.php
</IfModule>