Mod-重写“规范 URL”

发布于 2024-12-27 18:04:55 字数 488 浏览 3 评论 0原文

嘿,我如何使用 mod_rewrite 制作规范 URL(对于 Facebook“解析”)。

例子: 我有: example.com/index.php?page=eventdetails&id=241

我会创建一个规范的 URL,应如下所示:

example.com/ eventdetails/241/一些会被脚本忽略的文本

我用这个例子测试了它,但它不起作用:

RewriteEngine on
RewriteRule ^/eventdetails/([0-9]+)/$ index.php?show=eventdetails&id=$1

.htaccess 文件位于“home”方向。

我没有找到任何针对这个例子的解决方案@google,所以我希望有人可以帮助我!

Hy, how can i make canonical URLs (For Facebook "Parsing") with mod_rewrite.

Example:
I have:
example.com/index.php?page=eventdetails&id=241

I would make an canonincal URL, which should look like this:

example.com/eventdetails/241/some text which would be ignored from script

I tested it with this example, but it doesn't work:

RewriteEngine on
RewriteRule ^/eventdetails/([0-9]+)/$ index.php?show=eventdetails&id=$1

The .htaccess File is in the "home" direction.

I dont find any Solutions for exact this example @google, so i hope someone can help me!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

别闹i 2025-01-03 18:04:55

试试这个:

RewriteEngine on
RewriteBase /
RewriteRule ^eventdetails/([0-9]+)/?$ index.php?show=eventdetails&id=$1 [L,NC]
RewriteRule ^eventdetails/([0-9]+)/([a-z0-9\-_]+)/?$ php.php?show=eventdetails&id=$1&name=$2 [L,NC]

/? 表示末尾有一个可选的 / 。因此 example.com/eventdetails/241/example.com/eventdetails/241 将适用于此规则。如果不使用事件名称,第一个规则将仅使用第一个规则。

此网址将使用第二个网址(如果使用事件名称):example.com/eventdetails/241/event-name/。现在,我不推荐 + 因为它对 SEO 不太友好,也许有些人会接受,但有些人不会。我推荐使用-

我添加了 L 以防万一您有其他规则,并添加了 NC 标志以应对非情况。

L: [L] 标志导致 mod_rewrite 停止处理规则集。在大多数情况下,这意味着如果规则匹配,则不会处理更多规则。这对应于 Perl 中的最后一个命令,或 C 中的 break 命令。使用此标志来指示应立即应用当前规则,而不考虑进一步的规则。

NC:使用[NC]标志会导致RewriteRule以不区分大小写的方式进行匹配。也就是说,它不关心匹配的 URI 中字母是大写还是小写。

来自: http://httpd.apache.org/docs/2.3/rewrite/flags .html

Try this:

RewriteEngine on
RewriteBase /
RewriteRule ^eventdetails/([0-9]+)/?$ index.php?show=eventdetails&id=$1 [L,NC]
RewriteRule ^eventdetails/([0-9]+)/([a-z0-9\-_]+)/?$ php.php?show=eventdetails&id=$1&name=$2 [L,NC]

The /? means an optinal / at the end. So example.com/eventdetails/241/ and example.com/eventdetails/241 will work for this rule. The first one will only use the first rule if the event name is not use.

And this Url will use the second one (IF the event name is use): example.com/eventdetails/241/event-name/. Now here, I don't recommend the + since it's not really SEO friendly, maybe some will accept it but some not. I recommend the - instead.

I added the L in case you have other rules and NC flag for non-case.

L: The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed. This corresponds to the last command in Perl, or the break command in C. Use this flag to indicate that the current rule should be applied immediately without considering further rules.

NC: Use of the [NC] flag causes the RewriteRule to be matched in a case-insensitive manner. That is, it doesn't care whether letters appear as upper-case or lower-case in the matched URI.

from: http://httpd.apache.org/docs/2.3/rewrite/flags.html

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