
リンクにindex.phpを含める必要性をなくしたい。代わりに
example.com/about/index.php
そうであってほしい
example.com/about/
第二に、インデックス以外のページも、URLに.phpファイル拡張子がなくてもナビゲートできるようにしたいです。
example.com/about/who_we_are.php
そうであってほしい
example.com/about/who_we_are
両方を同時に動作させることはできませんでした。.htaccess ファイルの内容は次のとおりです。
Options -Indexes
DirectoryIndex index.php index.htm index.html
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
現在、インデックス以外のページから .php を削除していますが、ディレクトリのインデックス ページはすべてホームページにリダイレクトされています。
何が間違っているのでしょうか?
答え1
この質問を投稿してすぐに、この問題の解決策を見つけました。
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)index$ $1 [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
この問題の解決策を探している他の人たちにも役立つことを願っています。