ステップ1。
すべてのリンクを変更して、リンクから.html
削除します。Multiviews
オフになっていることを確認します。
Options -Multiviews
ステップ2。
ホスト名に www がない場合にブラウザをリダイレクトするルールが必要です。
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
ステップ 3。
URL が要求された場合、または拡張子が削除された場合にブラウザーをリダイレクトするルールが必要です。.html
.php
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^\ ]+)\.(php|html?)
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ /%2 [L,R=301]
ステップ 4。
リクエストが実際に php または html ファイルに対するものであった場合、URIを内部的に書き換えるルールが必要です。
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.htm -f
RewriteRule ^(.*)$ /$1.htm [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [L]
.htaccess
全体として、htaccess に次のルールを含める必要があります (この問題を解決しようとして既に持っている可能性のあるルールは破棄してください)。
# Make sure Multiviews is off
Options -Multiviews
# turn on rewrite engine
RewriteEngine On
# Redirect requests that are missing www and not a subdomain
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
# Redirect if requests is for a .htm, .html, or .php
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^\ ]+)\.(php|html?)
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ /%2 [L,R=301]
# rewrite to the proper extension if such a file exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.htm -f
RewriteRule ^(.*)$ /$1.htm [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [L]
したがって、完全に:
- ブラウザの URL アドレス バーに http://example.com/index.html と表示されている場合、http: //www.example.com/indexにリダイレクトされます。
- ブラウザの URL アドレス バーに http://www.example.com/index と表示されている場合、 http://www.example.com/index.html (または存在する場合)のコンテンツが表示されます。アドレス バーは変更されません。
index.php
- ブラウザの URL アドレス バーにhttp://foo.example.com/images/image.pngと表示されている場合、 に画像が表示され
/images/image.png
ます。アドレス バーは変更されません。
- ブラウザの URL アドレス バーに http://foo.example.com/path/foo/bar.php と表示されている場合、http: //foo.example.com/path/foo/barにリダイレクトされます。
- ブラウザの URL アドレス バーにhttp://foo.example.com/path/foo/barと表示されている場合、 http://foo.example.com/path/foo/bar.phpのコンテンツが表示されます。アドレス バーは変更されません。