带有两个参数的URL转发

时间:2012-12-18 05:58:07

标签: apache .htaccess url web lamp

这是我目前的.htaccess

   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^([0-9a-zA-Z-]+)/?$ index.php?u=$1 [L]
   RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
   RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

www.example.com 转换为 example.com 并解释 example.com/username AS example.com/的index.php?U =用户名

现在我想传递第二个参数,例如 example.com/index.php?u=username&e=email ,并保持格式 example.com/arg1&arg2 < / strong>即可。我怎么在.htaccess中做到这一点?

1 个答案:

答案 0 :(得分:0)

首先,您需要在路由规则之前移动重定向

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

然后,您要检查2参数路线:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z-]+)/([^/]+)/?$ /index.php?u=$1&e=$2 [L,QSA]

(这假定网址类似于:http://example.com/username/emailaddress,但如果您确实希望&将其分开,请改用此规则:

RewriteRule ^([0-9a-zA-Z-]+)&([^/]+)/?$ /index.php?u=$1&e=$2 [L,QSA]

这假定网址如下:http://example.com/username&emailaddress

现在你原来的规则:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z-]+)/?$ /index.php?u=$1 [L,QSA]