如果PHP文件存在的mod_rewrite检查 [英] Mod_rewrite check if php file exists

查看:95
本文介绍了如果PHP文件存在的mod_rewrite检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写一些mod_rewrite规则。一切正常的时刻。此刻我的通用网址是这样的:

I'm trying to write a few mod_rewrite rules. 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

目前,这两个链接mod_rewritten到 content.php ,因为关于美隐私不存在.php文件,而是他们的内容存储在一个数据库中,这content.php检索。

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.

我有另外一个网址:

http://website.com/contact-us

其中确实存在为.php文件,因为它包含自定义数据。 我怎么能检查是否接触us.php 的存在。如果是这样,重定向 website.com/contact-us website.com/contact-us.php ,否则,重定向到 website.com/content.php

Which does exist as a .php file, because it contains custom data. How can I check to see if contact-us.php exists. If it does, redirect website.com/contact-us to website.com/contact-us.php, otherwise, redirect to website.com/content.php

下面是我的mod_rewrite的文件,因为它主张:

Below is my mod_rewrite file as it stands:

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]

如果您需要任何进一步的信息,请让我知道! 我AP preciate的答复:)

If you need any further information, please let me know! I appreciate the replies :)

推荐答案

更​​改的.htaccess code到

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]

我也简化为匹配您的其他重写规则 S中的图案。注意使用 [QSA] 自动转发任何额外的查询参数和 [NC] 进行匹配病例不敏感的。

I've also simplified the pattern matching for your other RewriteRules. Notice the use of [QSA] to forward any extra query parameters automatically and [NC] to make the match case-insensitive.

这篇关于如果PHP文件存在的mod_rewrite检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆