我希望创建一个可以处理多个GET变量并相应地输出数据的PHP文件,但是我想删除基本的"category"URL元素以缩短它,使用htaccess,如下所示:
http://website.com/news/politics
http://website.com/videos/funny
http://website.com/posts/food
目前,我对每种类型使用不同的文件,但这很痛苦:
RewriteRule ^news/([^/]+)/?$ tpl-news.php?slug=$1 [L,QSA,NC]
RewriteRule ^videos/([^/]+)/?$ tpl-videos.php?slug=$1 [L,QSA,NC]
RewriteRule ^posts/([^/]+)/?$ tpl-posts.php?slug=$1 [L,QSA,NC]
etc ...
或者,我可以使用以下内容:
RewriteRule ^category/([^/]+)/?$ tpl-category.php?type=$1&slug=$2 [L,QSA,NC]
我不想有一个这样的网址:
http://website.com/category/news/politics
http://website.com/category/videos/funny
http://website.com/category/posts/food
但想像这样从 URL 中删除"类别":
http://website.com/news/politics
http://website.com/videos/funny
http://website.com/posts/food
但是,其他页面/URL(例如以下页面/URL)不应受此规则的影响:
http://website.com/account/user
http://website.com/newspost/abcdef // a single post page
http://website.com/videopost/abcdef // a single video page
这些"类型"(新闻、视频、帖子等)数量有限,即我不反对预定义这些类型,这样它们就不会与页面的其他 URL 冲突(例如图像文件夹、管理部分、用户配置文件子页面等)
您可以使用
以下通用规则:
RewriteRule ^(news|videos|post)/([^/]+)/?$ tpl-category.php?type=$1&slug=$2 [L,QSA]
或者,如果您想使用现有的.php
文件,例如 tpl-news.php
、tpl-post.php
等然后使用:
RewriteRule ^(news|videos|post)/([^/]+)/?$ tpl-$1.php?slug=$2 [L,QSA]