Phi*_*oss 8 apache .htaccess mod-rewrite
我正在尝试编写一些mod_rewrite规则.目前一切正常.目前,我的通用网址如下所示:
http://website.com/about-us
http://website.com/privacy
Run Code Online (Sandbox Code Playgroud)
目前,这两个链接mod_rewritten来content.php,因为about-us和privacy不存在的.php文件,而是他们的内容被存储在数据库中,这content.php检索.
我有另一个网址:
http://website.com/contact-us
Run Code Online (Sandbox Code Playgroud)
哪个确实存在.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]
Run Code Online (Sandbox Code Playgroud)
如果您需要任何进一步的信息,请告诉我!我很感谢回复:)
Rav*_*yal 11
将您的.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]
Run Code Online (Sandbox Code Playgroud)
我也为你的其他人简化了模式匹配RewriteRule.请注意使用[QSA]自动转发任何额外的查询参数[NC]并使匹配不区分大小写.