繁体   English   中英

.htaccess重写规则,用于固定模式URL的重定向

[英].htaccess rewrite rule for redirection for fixed pattern URL

我正在尝试为博客模块编写.htacces重写规则。

如果=> www.xyz.com/index.php它不应重定向任何内容

如果=> www.xyz.com/contactus.php它不应重定向任何内容

但是如果=> www.xyz.com/blogs/test-string1-and-string2
(针对博客页面的URL模式已固定)

它应该重定向到blog_details.php?id = $ 1

我尝试了以下规则:

RewriteCond %{HTTP_HOST} !/blog/

RewriteRule blogs/(\w+)  blog_details.php?id=$1 [NC]

这不是完美的规则。 生成站点地图和Seo结果时会引起问题。

我想要对URL进行强力验证,例如URL末尾的.php

博客/后用连字符(-)分隔的单词。 然后,我可以重定向网址。

首先,您无需在此处检查HTTP_HOST ,因为您没有更改它。

如果这样做,则应将其与可能的主机名(而不是可能的URL)进行比较,即

RewriteCond %{HTTP_HOST} !^www.nottherightone.tld$

要检查,如果URL不以“ .php”结尾并且包含连字符,则应该可以

RewriteCond %{REQUEST_URI}   !.php$
RewriteRule blogs/(.*-.*)    blog_details.php?id=$1 [NC]

以下规则应为您工作:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blogs/(.+)$ blog_details.php?id=$1 [NC,L,QSA]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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