1

要求された URL を確認し、要求がファイルに対するものかディレクトリに対するものかに応じて、さまざまなオプションを提供する必要があります。

私の URL は次のようになります。

  • http://www.example.com/Services/Service-1(ディレクトリ、サーブ/pages/Services/Service1/index.php)
  • http://www.example.com/Services/Service-1/Feature-1/Sub-Feature(実際のファイル、 serve /pages/Services/Service-1/Feature-1/Sub-Feature.php)

.htaccess を理解していないため (これには RewriteCondition が必要ですか?)、現在、次のようにディレクトリ構造のすべてのフォルダーを列挙するのに行き詰まっています。

RewriteRule ^Services/Service-1/(.*)$ /pages/Services/Service-1/$1.php
RewriteRule ^Services/Service-1 /pages/Services/Service-1/index.php

RewriteRule ^Services/Service-2/(.*)$ /pages/Services/Service-2/$1.php
RewriteRule ^Services/Service-2 /pages/Services/Service-2/index.php

RewriteRule ^Services/(.*)$ /pages/Services/$1.php
RewriteRule ^Services /pages/Services/index.php

RewriteRule ^Testimonials/(.*)$ /pages/Testimonials/$1.php
RewriteRule ^Testimonials /pages/Testimonials/index.php

言うまでもなく、これは本当に面倒です。コンテンツのフォルダーを追加するたびに、.htaccess をいじる必要があります。

もっと良い方法があるに違いないことはわかっていますが、Google と Stackoverflow の検索では、試してみてもうまくいくものは見つかりませんでした。

4

2 に答える 2

3

ご想像のとおり、 rewriteCond を使用して、要求された uri がファイルかディレクトリかを確認できます。

# f for a file, d for a directory
RewriteCond %{REQUEST_FILENAME} -f 

あなたの .htaccess は次のようになります。

Options -MultiViews
RewriteEngine ON    

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.+) /$1/index.php [QSA,L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.+) /$1.php [QSA,L]

EDIT : ファイルが page サブディレクトリにある場合は、次のコードを使用する必要があります。

Options -MultiViews
RewriteEngine ON    

RewriteCond %{DOCUMENT_ROOT}/pages/$1 -d
RewriteRule (.+) /pages/$1/index.php [QSA,L]

RewriteCond %{DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule (.+) /pages/$1.php [QSA,L]
于 2013-06-20T21:28:23.837 に答える
0

mod ネゴシエーションが有効になっている場合は、はるかに簡単です (通常は有効です)。

Options MultiViews
MultiviewsMatch Any
DirectoryIndex index.php

ただし、.php を強制することはありません。somefile.html と somefile.php がある場合、通常は .html ファイルが選択されます。

于 2013-06-20T21:36:28.403 に答える