これが私の設定です:
- mod_rewriteが有効になっているApacheを実行しているサーバーでホストされているhttp://domain.com (明らかに架空の名前...)
- 「foo」という名前のフォルダ:( http://domain.com/foo/にあり、.htaccessファイルを入れたい)3種類のファイル.php、.htm、.htmlのみが含まれています
- .phpファイル(例のために、そのうちの1つを参照します:phpfile.php)
- .htmファイル(例のために、そのうちの1つを参照します:htmfile.htm)
- .htmlファイル(例のために、そのうちの1つを参照します:htmlfile.html)
- fooフォルダー内では、別の拡張子を持つ、または拡張子のない同等のファイルはありません(たとえば、phpfile.htmもphpfile.htmlもphpfileもfooに存在せず、php.file.phpのみが存在します)
これが私が達成しようとしていることです:
- ブラウザのアドレスバーにhttp://domain.com/foo/phpfile.phpまたはhttp://domain.com/foo/htmfile.htmまたはhttp://domain.com/foo/htmlfile.htmlと入力して、 "入る":
- 301でhttp://domain.com/foo/phpfileまたはhttp://domain.com/foo/htmfileまたはhttp://domain.com/foo/htmlfileにリダイレクトされます(使用したファイルによって異なります)選択)、つまり、後者は現在アドレスバーに表示されているURLです
- ブラウザのアドレスバーにhttp://domain.com/foo/phpfileまたはhttp://domain.com/foo/htmfileまたはhttp://domain.com/foo/htmlfileと入力し、「Enter」キーを押すと、次のようになります。
- リダイレクトされません。アドレスバーは何も変更されませんが、代わりに、サーバーは、要求したものに応じて、phpfile.php、htmfile.htm、またはhtmlfile.htmlを提供します。
私はこれに一生懸命取り組んでいますが、.htaccessファイル(「foo」フォルダーにあります)のこのコンテンツが付属しています。残念ながら、この2つのケースの最後のケースでしか機能しません。 (つまり、「phpfile」を要求すると「phpfile.php」を提供し、「htmfile」を要求すると「htmfile.htm」を提供し、「htmlfile」を要求すると「htmlfile.html」を提供します)、これは301リダイレクトを無視します。
Options +FollowSymLinks
RewriteEngine on
RewriteBase /foo
# Redirect permanently the requests for existing .php files
# to extensionless files (non present on the server)
# so that phpfile.php gets redirected to phpfile
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} \.php$
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule (.*)\.php$ $1 [R=301,NC]
# Redirect permanently the requests for existing .htm(l) files
# to extensionless files (non present on the server)
# so that htmfile.htm gets redirected to htmfile
# so that htmlfile.html gets redirected to htmlfile
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} \.html?$
RewriteCond %{REQUEST_FILENAME}\.html? -f
RewriteRule (.*)\.html?$ $1 [R=301,NC]
# Matching requests for non existing extensionless files
# with their existing equivalent on the server
# so that domain.com/foo/phpfile will display
# the contents of domain.com/foo/phpfile.php,
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule (.*)$ $1.php [L]
# so that domain.com/foo/htmlfile will display
# the contents of domain.com/foo/htmlfile.html,
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule (.*)$ $1.html [L]
# so that domain.com/foo/htmfile will display
# the contents of domain.com/foo/htmfile.htm,
RewriteCond %{REQUEST_FILENAME}\.htm -f
RewriteRule (.*)$ $1.htm [L]
ヘルプ/アドバイスを事前に感謝します。