8

いくつかの mod_rewrite ルールを作成しようとしています。現時点ではすべてが正常に機能しています。現在、私の一般的な URL は次のようになっています。

http://website.com/about-us
http://website.com/privacy

現時点では、これら 2 つのリンクは に mod_rewrite されていますcontent.php。なぜならabout-us、 とprivacyは .php ファイルとして存在せず、コンテンツは content.php が取得するデータベースに保存されているからです。

私は別のURLを持っています:

http://website.com/contact-us

カスタムデータが含まれているため、これは .php ファイルとして存在します。存在するかどうかを確認するにはどうすればよいですかcontact-us.php。ある場合は にリダイレクトwebsite.com/contact-uswebsite.com/contact-us.php、そうでない場合は にリダイレクトしますwebsite.com/content.php

以下は私の mod_rewrite ファイルです。

RewriteEngine On

# I want the following condition and rule to look to see if the .php file exists,
# and if it does, redirect to the physical page, otherwise, redirect to content.php
RewriteCond %{DOCUMENT_ROOT}/([a-zA-Z0-9\-_]+).php -f
RewriteRule ^([a-zA-Z0-9\-_]+)\.php$ [L]


RewriteRule ^([a-zA-Z0-9\-_]+)$ /content.php [L]
RewriteRule ^(product1|product2|product3)/(designer1|designer2|designer3)$ /search.php?productType=$1&designer=$2 [L]
RewriteRule ^sale/(product1|product2|product3)$ /search.php?sale=1&productType=$1 [L]

さらに情報が必要な場合は、お知らせください。返信ありがとうございます:)

4

2 に答える 2

13

.htaccessコードを次のように変更します

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d # not an existing dir
RewriteCond %{REQUEST_FILENAME} !-f # not an existing file
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f # and page.php exists

# redirect to the physical page
RewriteRule ^(.*)$ $1.php [L]

# otherwise, redirect to content.php
RewriteRule ^ /content.php [L]

RewriteRule ^sale/(product[123])$ /search.php?sale=1&productType=$1 [QSA,NC,L]
RewriteRule ^(product[123])/(designer[123])$ /search.php?productType=$1&designer=$2 [QSA,NC,L]

他の のパターン マッチングも簡略化しましたRewriteRule[QSA]追加のクエリ パラメータを自動的に転送し[NC]、大文字と小文字を区別しないようにするために を使用していることに注意してください。

于 2013-10-27T14:00:14.843 に答える
2

私はこれを使用します:

#
# redirect first the evidences:
#
RewriteRule ^(product1|product2|product3)/(designer1|designer2|designer3)$ /search.php?    productType=$1&designer=$2 [L]
RewriteRule ^sale/(product1|product2|product3)$ /search.php?sale=1&productType=$1 [L]

#
# redirect then the generality:
#
RewriteCond %{DOCUMENT_ROOT}%%{REQUEST_URI}.php -f
RewriteRule ^ %{DOCUMENT_ROOT}%%{REQUEST_URI}.php [L]

#
# all other requests are rewritten to /content.php:
#
RewriteRule ^ /content.php [L]
于 2013-10-27T14:09:08.357 に答える