简体   繁体   English

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

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

I am trying to write .htacces rewrite rules for blog module. 我正在尝试为博客模块编写.htacces重写规则。

if => www.xyz.com/index.php It should not redirect anything 如果=> www.xyz.com/index.php它不应重定向任何内容

if => www.xyz.com/contactus.php It should not redirect anything 如果=> www.xyz.com/contactus.php它不应重定向任何内容

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

It should redirect to blog_details.php?id=$1 它应该重定向到blog_details.php?id = $ 1

I have tried following rules.: 我尝试了以下规则:

RewriteCond %{HTTP_HOST} !/blog/

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

This is not perfect rule. 这不是完美的规则。 It is causing problems while generating sitemaps and Seo results. 生成站点地图和Seo结果时会引起问题。

I want some strong validation for URL like .php at the end of URL or 我想要对URL进行强力验证,例如URL末尾的.php

words separated by hyphen (-) after blog/. 博客/后用连字符(-)分隔的单词。 Then I can redirect url. 然后,我可以重定向网址。

First you need not check the HTTP_HOST here, as you are not changing it. 首先,您无需在此处检查HTTP_HOST ,因为您没有更改它。

If you do, you should compare it to a possible Hostname, not to a possible URL, ie 如果这样做,则应将其与可能的主机名(而不是可能的URL)进行比较,即

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

To check, if the URL does not end in ".php" and contains hyphens, this should be a possbility 要检查,如果URL不以“ .php”结尾并且包含连字符,则应该可以

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

Following rule should work for you: 以下规则应为您工作:

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