简体   繁体   English

Mod_rewrite检查php文件是否存在

[英]Mod_rewrite check if php file exists

I'm trying to write a few mod_rewrite rules. 我正在尝试编写一些mod_rewrite规则。 Everything is working fine at the moment. 目前一切正常。 At the moment my generic URLs look like this: 目前,我的通用网址如下所示:

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

At the moment, these 2 links are mod_rewritten to content.php , because about-us and privacy don't exist as .php files, but rather, their content is stored in a database, which content.php retrieves. 目前,这两个链接是mod_rewritten到content.php ,因为about-usprivacy不存在.php文件,而是它们的内容存储在content.php检索的数据库中。

I have another url: 我有另一个网址:

http://website.com/contact-us

Which does exist as a .php file, because it contains custom data. 哪个确实存在.php文件,因为它包含自定义数据。 How can I check to see if contact-us.php exists. 如何查看contact-us.php存在。 If it does, redirect website.com/contact-us to website.com/contact-us.php , otherwise, redirect to website.com/content.php 如果是,请将website.com/contact-us重定向到website.com/contact-us.php ,否则,重定向到website.com/content.php

Below is my mod_rewrite file as it stands: 下面是我的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]

If you need any further information, please let me know! 如果您需要任何进一步的信息,请告诉我! I appreciate the replies :) 我很感谢回复:)

Change your .htaccess code to .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]

I've also simplified the pattern matching for your other RewriteRule s. 我还简化了其他RewriteRule的模式匹配。 Notice the use of [QSA] to forward any extra query parameters automatically and [NC] to make the match case-insensitive. 注意使用[QSA]自动转发任何额外的查询参数,使用[NC]使匹配不区分大小写。

I would use this: 我会用这个:

#
# 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM