.htaccess 和参数

发布于 2024-12-23 15:04:17 字数 338 浏览 1 评论 0原文

我在互联网上挖掘有关 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸印温柔 2024-12-30 15:04:17

表达式将为 (/)? 而不是 (/)。表示 URL 上是否有斜线。这两个 URL 将转到同一个位置:

http://www.domain.com/home/

http://www.domain.com/home

因此斜杠是可选的。这样,如果机器人或搜索引擎放置它,该规则就会起作用。

你可以这样重写规则:

RewriteRule ^home/?$ mainpage.php?id=$1 [L,QSA]

另外,我看到你说:

在我需要使用的所有其他内容中

如果您的所有页面都转到同一个文件(在本例中为 mainpage.php),您可以创建一个规则来自动重写它们,而不是创建 10 或 15 条规则(或更多)。您可以这样做:

RewriteEngine on
REwriteBase /
RewriteRule ^([a-z0-9\-_]+)/?$ mainpage.php?id=$1 [L,QSA]

此规则将使用字母、数字、破折号和下划线作为页面。

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

http://www.domain.com/home

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:

RewriteRule ^home/?$ mainpage.php?id=$1 [L,QSA]

Also, I saw you said:

among all others i need to use

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:

RewriteEngine on
REwriteBase /
RewriteRule ^([a-z0-9\-_]+)/?$ mainpage.php?id=$1 [L,QSA]

This rule will use Letters, Numbers, Dash and Underscores as the page.

梦中楼上月下 2024-12-30 15:04:17

(/)? 表示“可选斜杠”。

  • (/) 创建一个包含/匹配括号之间任何内容的组。在本例中,由于它是单个字符,因此括号更多的是用于捕获片段而不是其他任何东西。 (任何匹配都将变成 $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 to mainpage.php?id=, and home/ will be rewritten to mainpage.php?id=/.

  • The ? 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".
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文