【问题标题】:.htaccess URL rewrite to match symbols, letters and numbers.htaccess URL 重写以匹配符号、字母和数字
【发布时间】:2016-12-31 01:34:49
【问题描述】:

我正在尝试重写此 URL:

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 的第二部分。

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

【问题讨论】:

  • 这会有帮助吗? RewriteRule ^classifieds/activate/(.*)$ index.php?page=activate&extra=$1 [NC,L,QSA]。 LE:我通常让 PHP 应用程序动态地路由所有内容,管理实例化哪个控制器(如果存在)。 RewriteRule ^(.*)$ index.php [L,QSA] 以编程方式将 URL 拆分为控制器、操作和参数
  • 非常感谢。我稍微修改了你的答案,它可以工作。这是我最后用的RewriteRule ^([a-zA-Z0-9]+)/(.*)/$ index.php?page=$1&extra=$2

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


【解决方案1】:

为了让classifieds/activate/whatever 默默地点击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>

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

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

如果您需要更通用的重写规则来将 URI 的第二段(在本例中为 activate)重写为 page 参数,这就是您需要的:

RewriteRule classifieds/(.*)/(.*) index.php?page=$1&extra=$2 [L]

您可以更具体地确定您想要的字符;将(.*)([0-9A-Za-z])(\w) 之类的东西交换以仅接受字母数字值即可。

【讨论】:

    猜你喜欢
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    相关资源
    最近更新 更多