htaccess url 用 url 中的多个变量重写 [英] htaccess url rewrite with multiple variables in url
问题描述
我想在我的 .htaccess 文件上做一些 url 重写规则,这样这个链接:http://myseite.com/index.php?var1=value1&var2=value2
将变成:http://myseite.com/var1/value2.html
I want to make some url rewrite rules on my .htaccess file so that this link: http://myseite.com/index.php?var1=value1&var2=value2
will become : http://myseite.com/var1/value2.html
到目前为止,我已经成功地解决了这个问题,但只针对一个变量.
So far I have managed successfully to solve this problem but only for one variable.
我也试过这个代码:
RewriteRule ^([^/]*)/([^/]*).html$ /index.php?var1=$1&var2=$2 [L]
但它不起作用..
感谢您的帮助!
推荐答案
Request forhttp://mysite.com/var1/value2.html被改写为http://mysite.com/index.php?var1=var1&var2=var2
Request for http://mysite.com/var1/value2.html is rewritten to http://mysite.com/index.php?var1=var1&var2=var2
index.php 和 .htaccess 文件在文档根目录中
where index.php and .htaccess file are at the documentroot
.htaccess 文件仅在 httpd.conf(或某些 apache conf 文件)为您正在使用的 DocumentRoot 设置了AllowOverride All"时才有效.首先检查.
.htaccess files will only work if httpd.conf (or some apache conf file) has "AllowOverride All" set for the DocumentRoot you are working in. Check that first.
接下来,确保您在 Apache conf 文件中启用了 mod_rewrite(更改 conf 文件后重新启动网络服务器),然后在您的 .htaccess 文件中启用它
Next, make sure you have mod_rewrite enabled in Apache conf files (restart webserver after changing conf files) and then enable it in your .htaccess file
.htaccess 的内容:
contents of .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 看看它是否工作:
put this in /index.php to see it work:
<? print_r($_GET); ?>
这篇关于htaccess url 用 url 中的多个变量重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!