Mod rewrite 为两个不同的页面重写mod_
我有两页:Mod rewrite 为两个不同的页面重写mod_,mod-rewrite,Mod Rewrite,我有两页: categories.php 在categories.php页面中,我获取所有类别 news.php 在新闻页面中,我通过id获取新闻 我对类别使用了mod_rewrite: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([a-zA-Z0-9-/]+)$ categories.php?url=$1 RewriteRule
categories.php
在categories.php页面中,我获取所有类别
news.php
在新闻页面中,我通过id获取新闻
我对类别使用了mod_rewrite:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/]+)$ categories.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ categories.php?url=$1
site/categori1 ----> this rule works for categorie page
是否可以打开mod_重写两个不同的页面?(categories.php和news.php)
重写规则^([a-zA-Z0-9-/]+)$news.php?url=$2
重写规则^([a-zA-Z0-9-/]+)/$new.php?url=$2
上面的site/news-title1规则不适用于news.php页面
如何对两个不同的页面使用mod_rewrite?
提前感谢您必须更具体地说明正则表达式所匹配的内容: “^([a-zA-Z0-9-/]+)$”将与几乎所有内容匹配 您需要确定分类页面与新闻页面的区别,并在这方面进行匹配,例如:
RewriteRule ^category/([a-zA-Z0-9-/]+)$ categories.php?url=$1
RewriteRule ^category/([a-zA-Z0-9-/]+)/$ categories.php?url=$1
RewriteRule ^news/([a-zA-Z0-9-/]+)$ news.php?url=$1
RewriteRule ^news/([a-zA-Z0-9-/]+)/$ news.php?url=$1
您可以用其他方式区分规则,但您必须这样做
例如,您可以重新排列上面我的建议中的规则,使类别规则捕获所有不以新闻为前缀的内容:
RewriteRule ^news/([a-zA-Z0-9-/]+)$ news.php?url=$1
RewriteRule ^news/([a-zA-Z0-9-/]+)/$ news.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)$ categories.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ categories.php?url=$1
您必须更具体地说明正则表达式所匹配的内容: “^([a-zA-Z0-9-/]+)$”将与几乎所有内容匹配 您需要确定分类页面与新闻页面的区别,并在这方面进行匹配,例如:
RewriteRule ^category/([a-zA-Z0-9-/]+)$ categories.php?url=$1
RewriteRule ^category/([a-zA-Z0-9-/]+)/$ categories.php?url=$1
RewriteRule ^news/([a-zA-Z0-9-/]+)$ news.php?url=$1
RewriteRule ^news/([a-zA-Z0-9-/]+)/$ news.php?url=$1
您可以用其他方式区分规则,但您必须这样做
例如,您可以重新排列上面我的建议中的规则,使类别规则捕获所有不以新闻为前缀的内容:
RewriteRule ^news/([a-zA-Z0-9-/]+)$ news.php?url=$1
RewriteRule ^news/([a-zA-Z0-9-/]+)/$ news.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)$ categories.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ categories.php?url=$1
www.site.com/news-title1
和www.site.com/category1
基本上匹配相同的URL,因此mod_rewrite无法最终决定重定向到哪里。除了您已经拥有的之外,我还建议您:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^news-([a-zA-Z0-9-/]+)/?$ news.php?url=$1 [L,NC,QSA]
RewriteRule ^([a-zA-Z0-9-/]+)/?$ categories.php?url=$1 [L,NC,QSA]
现在,类型为www.site.com/news some title
的URI将转发到www.site.com/news.php?url=some title
(任何以新闻-
开头的内容)
类型为
www.site.com/some category
的URI将被转发到www.site.com/category.php?url=some category
www.site.com/news-title1
和www.site.com/category1
基本上匹配相同的url,因此mod_rewrite无法最终决定重定向到哪里。除了您已经拥有的之外,我还建议您:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^news-([a-zA-Z0-9-/]+)/?$ news.php?url=$1 [L,NC,QSA]
RewriteRule ^([a-zA-Z0-9-/]+)/?$ categories.php?url=$1 [L,NC,QSA]
现在,类型为www.site.com/news some title
的URI将转发到www.site.com/news.php?url=some title
(任何以新闻-
开头的内容)
类型为www.site.com/some category
的URI将转发到www.site.com/category.php?url=some category