2013-10-27 15 views
7

私はいくつかのmod_rewriteルールを書こうとしています。今はすべてがうまくいきます。私の一般的なURLは次のようになり瞬間:現時点ではMod_rewriteは、PHPファイルが存在するかどうかを確認します。

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

about-usprivacyが.phpファイルとして存在していないため、これらの2つのリンクは、content.phpにmod_rewrittenされているのではなく、その内容がに保存されていますcontent.phpが取得するデータベース。

私は別のURLがあります。

http://website.com/contact-us 

それはカスタムデータが含まれているため、.phpのファイルとして存在しません。 contact-us.phpが存在するかどうかを確認する方法を教えてください。

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] 

何か、さらに情報が必要な場合、私に知らせてください!それがない場合は、そのままでそれ以外の場合は、以下website.com/content.php

にリダイレクトが私のmod_rewriteファイルで、website.com/contact-us.phpwebsite.com/contact-usをリダイレクト私は返事:)

答えて

9

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] 

にご.htaccessコードを変更

感謝 は、私はまた、あなたの他のRewriteRule秒間パターンマッチングを簡素化しました。余分なクエリパラメータを自動的に転送するには、[QSA]を使用し、[NC]を使用して、大文字と小文字を区別しないようにしてください。

+0

応答のおかげで、残念ながら、それは、500内部サーバーエラーを考え出す:( –

+0

は '/' '%{DOCUMENT_ROOT}%{REQUEST_URI}'の間を削除します。 –

+0

他の 'RewriteRule'のためのより簡単なパターンマッチングを見てください。 –

1

私はこれを使用します

# 
# 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] 
関連する問題