.htaccess 和参数
我在互联网上挖掘有关 .htaccess 的内容并重写我需要对我的网站执行的规则。
我看到了一些我不明白的东西,想知道它是什么意思
,我想知道我需要在我的网站上使用的两个正则表达式以及我需要使用的所有其他正则表达式之间有什么区别):
RewriteRule ^home$ mainpage.php?id=$1 [QSA]
我
RewriteRule ^home(/)?$ mainpage.php?id=$1 [L]
查看了 QSA 和 L但是 (/)
是什么意思呢?
i digg on the internet about .htaccess and rewrite rules i need to do with my site.
i saw something i dont understand and want to know what it means
i am wondering what is the difference between the 2 regular expression that i need to use for my site among all others i need to use):
RewriteRule ^home$ mainpage.php?id=$1 [QSA]
and
RewriteRule ^home(/)?$ mainpage.php?id=$1 [L]
i looked at the QSA and L but what the (/)
means?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
表达式将为
(/)?
而不是(/)
。表示 URL 上是否有斜线。这两个 URL 将转到同一个位置:http://www.domain.com/home/
,
因此斜杠是可选的。这样,如果机器人或搜索引擎放置它,该规则就会起作用。
你可以这样重写规则:
另外,我看到你说:
如果您的所有页面都转到同一个文件(在本例中为 mainpage.php),您可以创建一个规则来自动重写它们,而不是创建 10 或 15 条规则(或更多)。您可以这样做:
此规则将使用字母、数字、破折号和下划线作为页面。
The expression will be
(/)?
not(/)
. It means slash or not on the URL. These two URLs will go to the same place:http://www.domain.com/home/
and
So the slash is optional. This way, if a bot or a search engine puts it, the rule will work.
You can rewrite the rules like this:
Also, I saw you said:
If all your pages goes to the same file (in this case mainpage.php), you can create one rule that will automatically rewrites them instead of creating 10 or 15 rules (or more). You can do this like this:
This rule will use Letters, Numbers, Dash and Underscores as the page.
(/)?
表示“可选斜杠”。(/)
创建一个包含/匹配括号之间任何内容的组。在本例中,由于它是单个字符,因此括号更多的是用于捕获片段而不是其他任何东西。 (任何匹配都将变成$1
、$2
等,具体取决于模式中有多少个捕获组(括号集)以及它们所在的位置。)顺便说一句,您的第一个RewriteRule 没有多大意义——因为没有组为
$1
提供值,所以它可能始终为空。在第二个中,$1
将为空或斜杠,具体取决于子模式是否匹配。home
将被重写为mainpage.php?id=
,home/
将被重写为mainpage.php?id=/
。?
表示“前一个原子的 0 或 1”。 (顺便说一句,“原子”是正则表达式的构建块。)在这种情况下,由于它前面的原子是(/)
(实际上是/
),?
表示“0 或 1 个斜杠”。(/)?
means "an optional slash".(/)
creates a group containing/matching whatever's between the parentheses. In this case, since it's a single character, the parentheses are more for capturing pieces than anything else. (Whatever matches will become$1
,$2
, etc depending on how many capture groups (sets of parentheses) there are in the pattern and where they are.)BTW, your first RewriteRule doesn't make a whole lot of sense -- since there's no group to provide a value for
$1
, it'll probably always be blank. In the second,$1
will either be empty or a slash, depending on whether the subpattern matches.home
will be rewritten tomainpage.php?id=
, andhome/
will be rewritten tomainpage.php?id=/
.?
means "0 or 1 of the previous atom". ("Atoms", BTW, are the building blocks of a regex.) In this case, since the atom preceding it is(/)
(which is effectively/
), the?
means "0 or 1 slashes".