gpt4 book ai didi

apache - Mod_rewrite 检查 php 文件是否存在

转载 作者:行者123 更新时间:2023-12-04 06:03:42 26 4
gpt4 key购买 nike

我正在尝试编写一些 mod_rewrite 规则。目前一切正常。目前我的通用 URL 如下所示:

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

目前,这两个链接被修改为 content.php , 因为 about-usprivacy不以 .php 文件的形式存在,而是将它们的内容存储在一个由 content.php 检索的数据库中。

我有另一个网址:
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]

如果您需要任何进一步的信息,请告诉我!
我很欣赏回复:)

最佳答案

更改您的 .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 的模式匹配s。注意 [QSA]的使用自动转发任何额外的查询参数和 [NC]使匹配不区分大小写。

关于apache - Mod_rewrite 检查 php 文件是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19618751/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com