htaccess url在url中使用多个变量重写

时间:2011-02-08 06:53:45

标签: html .htaccess url-rewriting

我想在.htaccess文件上制作一些网址重写规则,以便此链接:http://myseite.com/index.php?var1=value1&var2=value2成为:http://myseite.com/var1/value2.html

到目前为止,我已成功解决了这个问题,但只针对一个变量。

我也试过这段代码:

RewriteRule ^([^/]*)/([^/]*)\.html$ /index.php?var1=$1&var2=$2 [L]

但它不起作用..

谢谢你的帮助!

1 个答案:

答案 0 :(得分:4)

请求 http://mysite.com/var1/value2.html 被重写为 http://mysite.com/index.php?var1=var1&var2=var2

其中index.php和.htaccess文件位于documentroot

.htaccess文件仅在httpd.conf(或某个apache conf文件)为您正在使用的DocumentRoot设置了“AllowOverride All”时才有效。请先检查。

接下来,确保在Apache conf文件中启用了mod_rewrite(在更改conf文件后重新启动webserver),然后在.htaccess文件中启用它

.htaccess的内容:

<IfModule mod_rewrite.c>
RewriteEngine On
# /var1/value2.html to /index.php?var1=value1&var2=value2
RewriteRule ^([^/]+)/([^\.]+)\.html$ /index.php?var1=$1&var2=$2
</IfModule>

将它放在/index.php中以查看它的工作原理:

<? print_r($_GET); ?>