htaccess url rewrite with multiple variables in url

99封情书 提交于 2019-12-30 14:08:50

问题


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.

I also tried this code:

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

But it doesn't work..

Thank you for the help!


回答1:


Request for http://mysite.com/var1/value2.html is rewritten to http://mysite.com/index.php?var1=var1&var2=var2

where index.php and .htaccess file are at the documentroot

.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.

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

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>

put this in /index.php to see it work:

<? print_r($_GET); ?>


来源:https://stackoverflow.com/questions/4930378/htaccess-url-rewrite-with-multiple-variables-in-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!