.htaccess - 使用链编写高效代码,然后编写url重定向

时间:2011-04-13 13:25:34

标签: php .htaccess friendly-url

假设我在用户浏览页面(www.date.com)上有以下URL: www.date.com/Location-A/Education-B/Sex-C/Interest-D /

情况是用户可以仅浏览所有(4)个可用标准中的数字(1或2) www.date.com/Education-B/或 www.date.com/Sex-C/Interest-E /

还假设网址中的浏览条件顺序很重要(即A / B - 确定,但B / A不正常)

我的.htaccess文件中有代码

RewriteEngine On

1个可变案例

RewriteRule ^/Location-([^/]+)/$ /index.php?Location=$1 [L]

RewriteRule ^/Education-([^/]+)/$ /index.php?Education=$1 [L]

(...)

2个变量案例

(...)

4个变量案例

RewriteRule ^/Location-([^/]+)/Education-([^/]+)/Sex-([^/]+)/Interest-([^/]+)/$ /index.php?Location=$1&Education=$2&Sex=$3&Interest=$4 [L]

如果我有8个变量,这将变得非常长。

有更聪明的方式实现上述目标吗?可能使用[C] - 链或[N] - 下一个? (URL变量的顺序很重要)

提前致谢

1 个答案:

答案 0 :(得分:1)

试试这个:

RewriteRule ^/([^-]+)-([^/]+)/$ /index.php?$1=$2 [L]
RewriteRule ^/([^-]+)-([^/]+)/([^-]+)-([^/]+)/$ /index.php?$1=$2&$3=$4 [L]
RewriteRule ^/([^-]+)-([^/]+)/([^-]+)-([^/]+)/([^-]+)-([^/]+)/$ /index.php?$1=$2&$3=$4&$5=$6 [L]
RewriteRule ^/([^-]+)-([^/]+)/([^-]+)-([^/]+)/([^-]+)-([^/]+)/([^-]+)-([^/]+)/$ /index.php?$1=$2&$3=$4&$5=$6&$7=$8 [L]
...