htaccess使用4个GET参数重写URL

时间:2016-06-10 15:31:58

标签: apache .htaccess mod-rewrite

使用.htaccess重写网址时遇到问题。

3 GET参数可以正常工作。

test.com/account/activation/id123

<Files .htaccess>
order allow,deny
deny from all
</Files>

Options -Indexes

RewriteEngine On
RewriteBase /

RewriteRule ^account$ account.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^account/([^./]+)$ account.php?id=$1 [NC,L]
RewriteRule ^account/([^./]+)/([^./]+)$ account.php?id=$1&activation=$2 [NC,L]

直到我尝试添加其他参数。

RewriteRule ^account/([^./]+)/([^./]+)$ account.php?id=$1&activation=$2&re=$3 [NC,L]

test.com/account/activation/id123/taken

它不起作用。它转到404找不到页面。

我的代码出了什么问题?

提前感谢...

1 个答案:

答案 0 :(得分:1)

您的正则表达式仍然接受2种模式。您可以使用以下规则:

RewriteEngine On
RewriteBase /

RewriteRule ^account/?$ account.php [NC,L]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^account/([^/]+)/?$ account.php?id=$1 [NC,L,QSA]

RewriteRule ^account/([^/]+)/([^/]+)/?$ account.php?id=$1&activation=$2 [NC,L,QSA]

RewriteRule ^account/([^/]+)/([^/]+)/([^/]+)/?$ account.php?id=$1&activation=$2&re=$3 [NC,L,QSA]