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 The first has no space for a filename.
the wrong pipe character
# REDIRECT .PHP AND .HTML REQUESTS TO EXTENSIONLESS - see Rules 1, 2 and 3
# AND -F TEST TO MATCH REQUEST TO PHYSICAL .PHP AND .HTML
# 1 (per g1smd at Apache Web Server Forum, http://www.webmasterworld.com/apache/4648950.htm)
# PHP and HTML EXTENSIONLESS REDIRECT
RewriteCond %{THE_REQUEST} ^[A-Z]{3-9}\ /([^/]+/)*[^/.]+\.(php|html)
RewriteRule ^(([^/]+/)*[^/.]+)\.(php|html)$ http://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 (per lucy24 at Apache Web Server Forum, http://www.webmasterworld.com/apache/4648950.htm)
# 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] somehow redirected the request for foo.html to find foo.php when foo.html was not available
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)\.html$ /$1.php [L] # REDIRECT .PHP AND .HTML REQUESTS TO EXTENSIONLESS - see Rules 1, 2 and 3
# AND -F TEST TO MATCH REQUEST TO PHYSICAL .PHP AND .HTML
# 1 (per g1smd at Apache Web Server Forum, http://www.webmasterworld.com/apache/4648950.htm)
# PHP and HTML EXTENSIONLESS REDIRECT
RewriteCond %{THE_REQUEST} ^[A-Z]{3-9}\ /([^/]+/)*[^/.]+\.(php|html)
RewriteRule ^(([^/]+/)*[^/.]+)\.(php|html)$ http://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 (per lucy24 at Apache Web Server Forum, http://www.webmasterworld.com/apache/4648950.htm)
# 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]
# 4
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)\.html$ /$1.php [L] Both.