htaccess重写url动态长度

时间:2012-10-27 19:47:18

标签: apache .htaccess

我正在开始一个网站项目,我想用动态长度重写网址。例如,站点中可以存在以下URL:

http://www.url.com/signup/

或者

http://www.url.com/profile/mark/pm/compose/

现在我有一个.htaccess脚本工作,但我怀疑它的效率。这是代码:

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?page=$1

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?page=$1&subone=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?page=$1&subone=$2

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?    page=$1&subone=$2&subtwo=$3
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?page=$1&subone=$2&subtwo=$3

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?page=$1&subone=$2&subtwo=$3&subthree=$4
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?page=$1&subone=$2&subtwo=$3&subthree=$4

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?arg[0]=$1&arg[1]=$2&arg[2]=$3&arg[3]=$4&arg[4]=$5
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?arg[0]=$1&arg[1]=$2&arg[2]=$3&arg[3]=$4&arg[4]=$5

我实际上想做这样的事情,但我无法弄清楚我是如何让它发挥作用的......

RewriteRule ^{[a-zA-Z0-9_-]+}/$ index.php?urlvar={$#}

所以网址

http://www.url.com/profile/mark/pm/compose/

将导致

http://www.url.com/index.php?urlvar[0]=profile&urlvar[1]=mark&urlvar[2]=pm&urlvar[3]=compose

任何人都知道如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我得到了这个工作: 在.htaccess中:

Options +FollowSymLinks
RewriteEngine on

RewriteRule (.*)/ index.php?path=$1

在index.php中:

<?php
    print_r(explode("/", $_GET['path']));
?>