[英]Mod_rewrite check if php file exists
我正在嘗試編寫一些mod_rewrite規則。 目前一切正常。 目前,我的通用網址如下所示:
http://website.com/about-us
http://website.com/privacy
目前,這兩個鏈接是mod_rewritten到content.php
,因為about-us
和privacy
不存在.php文件,而是它們的內容存儲在content.php檢索的數據庫中。
我有另一個網址:
http://website.com/contact-us
哪個確實存在.php文件,因為它包含自定義數據。 如何查看contact-us.php
存在。 如果是,請將website.com/contact-us
重定向到website.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]
如果您需要任何進一步的信息,請告訴我! 我很感謝回復:)
將.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]
使匹配不區分大小寫。
我會用這個:
#
# 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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.