.htaccess URL重写以匹配符号,字母和数字

时间:2016-12-31 01:34:50

标签: php apache .htaccess mod-rewrite url-rewriting

我正在尝试重写此网址:

localhost/classifieds/index.php?page=activate&extra=$2y$10$LsV9m8JgEz2zMaJYRNqocuOzaqR8oHmn3tBIjIQv.TI4JWd3rWVrC

到:

localhost/classifieds/activate/$2y$10$LsV9m8JgEz2zMaJYRNqocuOzaqR8oHmn3tBIjIQv.TI4JWd3rWVrC

现在,第一部分是成功的,如果我输入:

localhost/classifieds/activate
它给了我:

localhost/classifieds/index.php?page=activate

在此,加载正确的控制器。

我的主要问题是我希望规则也重写URL的第二部分。

我尝试了不同的正则表达式组合,但都无济于事。

1 个答案:

答案 0 :(得分:0)

要让index.php?page=activate&extra=whatever默默地点击Options +FollowSymLinks <IfModule mod_rewrite.c> RewriteEngine on RewriteBase / # Match the host, if you wish #RewriteCond %{HTTP_HOST} ^localhost$ RewriteRule classifieds/activate/(.*) index.php?page=activate&extra=$1 [L] </IfModule> ,您需要这样的内容:

activate

如果您希望传递任何查询字符串参数,则需要将QSA flag添加到重写规则的末尾。

如果您愿意,可以在线测试。

如果您需要更通用的重写规则来将URI的第二部分(在本例中为page)重写为RewriteRule classifieds/(.*)/(.*) index.php?page=$1&extra=$2 [L] 参数,那么您需要:

(.*)

您可以更具体地了解您想要的字符;使用([0-9A-Za-z])(\w)等内容交换{{1}}只接受字母数字值即可。