.htaccess URL重写以匹配符号,字母和数字 [英] .htaccess URL rewrite to match symbols, letters and numbers

查看:367
本文介绍了.htaccess URL重写以匹配符号,字母和数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重写此URL:

I am trying to rewrite this 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

在这里,加载正确的控制器.

Hereby, loading the right controller.

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

My major problem is that I want the rule to also rewrite the second part of the URL.

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

I tried different regex combinations, but all to no avail.

推荐答案

为使classifieds/activate/whatever默默地击打index.php?page=activate&extra=whatever,您需要以下内容:

In order for classifieds/activate/whatever to silently hit the index.php?page=activate&extra=whatever, you need something like this:

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>

如果您希望传递任何查询字符串参数,则需要添加

In case you want any query string parameter to be passed, you need to add the QSA flag to the end of the rewrite rule.

如果愿意,您可以在线对其进行测试.

You can test it online, if you wish.

如果您需要更通用的重写规则,以将URI的第二段(在本例中为activate)重写为page参数,则需要执行以下操作:

If you need a more generic rewrite rule to rewrite the second segment of the URI (activate in this case) to the page parameter, here's what you need:

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

您可以更详细地了解想要的字符;将(.*)替换为([0-9A-Za-z])(\w)之类的内容以仅接受字母数字值.

You can be more specific about what characters you want to expect; swapping (.*) with something like ([0-9A-Za-z]) or (\w) to accept only alphanumeric values will do.

这篇关于.htaccess URL重写以匹配符号,字母和数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆