要重复htaccess的重写变量 [英] To repeat variables in htaccess rewrite

查看:128
本文介绍了要重复htaccess的重写变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要做一些修改一个htaccess的。我的网站是能够在URL中显示多个类别,以便在不同类别的项目可以被显示给用户,在同一时间。实施例将像domain.com/fans.php?cat [] = 1&安培;猫[] = 2及猫[] = 3

I have a htaccess that needs some modification. My site is able to show multiple category within a URL so that items in different category can be shown to user at the same time. Example would be like domain.com/fans.php?cat[]=1&cat[]=2&cat[]=3

为使这个网址的友好,我加入到我的htaccess一行1类,另一条线路为2类,另一个为3类等等。如下所示

To make this URL friendly, I add into my htaccess one line for 1 category, another line for 2 category and another for 3 category and more. As shown below

RewriteRule ^fans/category/([a-zA-Z0-9\-]+)$ fans.php?cat=$1

上面将成为domain.com/fans/category/1

RewriteRule ^fans/category/([a-zA-Z0-9\-]+)&([a-zA-Z0-9\-]+)$ fans.php?cat[]=$1&cat[]=$2

上面将成为domain.com/fans/category/1&2

RewriteRule ^fans/category/([a-zA-Z0-9\-]+)&([a-zA-Z0-9\-]+)&([a-zA-Z0-9\-]+)$ fans.php?cat[]=$1&cat[]=$2&cat[]=$3

上面将成为domain.com/fans/category/1&2&3

我的问题是,如果我需要显示4个类别,甚至10类是什么?有没有一种方法来简化htacesss重写规则,允许类n次结合?还是我必须要不断的重复就像我在做什么上面。

My question is, what if I need to show 4 category or even 10 category? Is there a way to simplify rewrite rules in htacesss that will allow n times of category combine? or do I have to keep repeating like what I am doing above.

推荐答案

有可能有一个规则,只要有足够多的可选的正则表达式组和空白键(猫[] = )是没有问题的。示例3参数最大:

It is possible to have one rule as long as there are enough optional regex groups, and empty keys (cat[]=) are not a problem . Example for 3 parameters maximum:

请求: http://example.com/fans/category/1&2&3

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^fans/category/([^&]+)&?([^&]+)?&?([^&]+)?$ /fans.php?cat1=$1&cat2=$2&cat3=$3 [L,NC]

不过,当有很多的参数,它的更好,更加灵活,通过完整的URI路径与参数传递给脚本,使用单一的重写规则。像这样的:

However, when there are many parameters, it's better and more versatile to pass the complete URI-path with parameters to the script, using a single rewrite rule. Like this:

RewriteRule ^fans/category/(.*) /fans.php?$1 [L,NC]

和获得脚本的参数与code类似于此:

And get the parameters in the script with a code similar to this one:

<?php 
$Query = $_SERVER['QUERY_STRING'];
$Parameters = explode ('&', $Query);
echo "cat1 = ". $Parameters[0] . "<br />";
echo "cat2 = ". $Parameters[1] . "<br />";
echo "cat3 = ". $Parameters[2] . "<br />";
...
?>

有几种方法来达到同样的效果在PHP,也许有一个更好的,这仅仅是一个例子。

There are several ways to achieve the same result in php and maybe there is a better one, this is just an example.

这篇关于要重复htaccess的重写变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆