1

这是我当前的 .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解释为example.com/index.php?u=username

现在我想传递第二个参数,例如example.com/index.php?u=username&e=email并且仍然保持格式example.com/arg1&arg2。我如何在 .htaccess 中做到这一点?

4

1 回答 1

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]

(这假定 URL 看起来像: http://example.com/username/emailaddress,但如果你真的想要&分隔它们,请改用此规则:

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

这假设一个 URL 看起来像:http://example.com/username&emailaddress

现在你原来的规则:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z-]+)/?$ /index.php?u=$1 [L,QSA]
于 2012-12-18T06:06:47.827 回答