Difference between revisions of "Apache/Rewrite"
Jump to navigation
Jump to search
(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...") |
|||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{ |
{{DISPLAYTITLE:Apache mod_rewrite recipes}} |
||
== Recipes == |
== Recipes == |
||
=== Redirect to file with different extension == |
=== Redirect to file with different extension === |
||
Assuming some file have changed from <tt><basename>.html</tt> to <tt><basename>.php</tt>. The server should only redirect if the <tt><basename>.html</tt> file does not exist. |
Assuming some file have changed from <tt><basename>.html</tt> to <tt><basename>.php</tt>. The server should only redirect if the <tt><basename>.html</tt> file does not exist. |
||
<IfModule mod_rewrite.c> |
<IfModule mod_rewrite.c> |
||
RewriteEngine |
RewriteEngine On |
||
## RewriteLog /var/tmp/rewrite_log |
## RewriteLog /var/tmp/rewrite_log |
||
## RewriteLogLevel 9 |
## RewriteLogLevel 9 |
||
Line 13: | Line 13: | ||
The first <tt>RewriteCond</tt> is to check if the file exists or not the second one determines if the extension is either <tt>.htm</tt> or <tt>.html</tt>. Finally the <tt>RewriteRule</tt> swaps out the file extension and hands it back <tt>[L]</tt>. |
The first <tt>RewriteCond</tt> is to check if the file exists or not the second one determines if the extension is either <tt>.htm</tt> or <tt>.html</tt>. Finally the <tt>RewriteRule</tt> swaps out the file extension and hands it back <tt>[L]</tt>. |
||
=== 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} (/[^.]?.+)$ [NC] |
|||
RewriteCond %{REQUEST_FILENAME} !-f |
|||
RewriteCond %{REQUEST_FILENAME} !-d |
|||
RewriteRule .* /index.php |
|||
</IfModule> |
|||
[[Category:Apache]] |
[[Category:Apache]] |
Latest revision as of 20:08, 30 April 2015
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} (/[^.]?.+)$ [NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* /index.php </IfModule>