Forum Moderators: phranque
# BACKWARD COMPATIBILITY RULESET
# FOR REWRITING FILE URI TO file.php IF EXISTS
Options Indexes +FollowSymLinks +MultiViews
Options +ExecCGI
RewriteEngine on
RewriteBase /
# parse out basename, but remember the fact
RewriteRule ^(.*).html$ $1 [C,E=WasHTML:yes]
# rewrite to document.php if exists
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [S=1]
# else reverse the previous basename cutout
RewriteCond %{ENV:WasHTML} ^yes$
RewriteRule ^(.*)$ $1.html # backward compatibility ruleset for
# rewriting document.html to document.php
# when and only when document.php exists
<Directory /var/www/htdocs>
RewriteEngine on
RewriteBase /var/www/htdocs
RewriteCond $1.php -f
RewriteCond $1.html !-f
RewriteRule ^(.*).html$ $1.php
</Directory> Options Indexes +FollowSymLinks +MultiViews
Mixing Options with a + or - with those without is not valid syntax, and is likely to cause unexpected results.
^paintings/rats/blahblah RewriteEngine on
RewriteBase /
RewriteRule blahblah(\.html|\.php)? valid-content-here [L] [edited by: phranque at 12:42 am (utc) on Feb 26, 2014]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]
RewriteEngine on There's no duplicate content; where foo.html was updated to foo.php, the original foo.html was deleted from server
if neither match, redirect to the 404
ErrorDocument 404 /my404page.hmtl
RewriteRule ^([^.]+/)\.(html|php) http://www.example.in/$1 [R=301,L] RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^.]+)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^.]+)$ /$1.html [L] RewriteCond %{THE_REQUEST} ^[A-Z]{3-9}\ /[^.]+/\.(html|php) #PHP and HTML extensionless redirect
RewriteCond %{THE_REQUEST} ^[.]{3-9}\ /[^.]+/\.(html|php)
RewriteRule ^([^.]+/)\.(html|php)$ http://www.example.com/$1 [R=301,L] RewriteCond %{THE_REQUEST} ^[A-Z]{3-9}\ /[^.]+/\.(html|php) # Canonical redirect
# Redirect from www.example.com to example.com
RewriteCond %{HTTP_HOST} !^(example\.com)$
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L] # backward compatibility ruleset for
# rewriting document.html to document.php
# when and only when document.php exists
RewriteCond $1.php -f
RewriteCond $1.html !-f
RewriteRule ^(.*).html$ $1.php [L]
<Directory> tags. You had suggested the following: RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^.]+)$ /$.php [L]
RewriteCond %[REQUEST_FILENAME}\.html -f
RewriteRule ^([^.]+)$ /$.html [L] # 1 per Lucy24
# PHP and HTML extensionless redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3-9}\ /[^.]+/\.(html|php)
RewriteRule ^([^.]+/)\.(html|php)$ http://www.example.com/$1 [R=301,L] # 1 per g1smd
# PHP and HTML extensionless redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3-9}\ (([^/]+/)*[^/.]+)\.(php多tml)
RewriteRule ^([^.]+/)\.(html|php)$ http://www.example.com/$1 [R=301,L] # 2
# Canonical redirect
# Redirect from www.example.com to example.com
RewriteCond %{HTTP_HOST} !^(example\.com)$
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L] # 3
# serve content from .php or .html files requested as extensionless files
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^.]+)$ /$1.php [L]
RewriteCond %[REQUEST_FILENAME}\.html -f
RewriteRule ^([^.]+)$ /$1.html [L] [edited by: g1smd at 6:30 pm (utc) on Feb 27, 2014]
RewriteCond %{THE_REQUEST} ^[A-Z]{3-9}\ /[^.]+\.(html|php)
...
RewriteCond %{THE_REQUEST} ^[A-Z]{3-9}\ /(([^/]+/)*[^/.]+)\.(php多tml) [edited by: phranque at 4:28 pm (utc) on Feb 28, 2014]
[edit reason] fixed typo [/edit]
[edited by: phranque at 9:42 am (utc) on Mar 3, 2014]
[edit reason] fixed the first line of code [/edit]
[edited by: g1smd at 8:03 am (utc) on Feb 28, 2014]