在 URL 中添加尾部斜杠

发布于 2024-09-28 23:26:13 字数 774 浏览 6 评论 0原文

我当前的 .htaccess 文件如下所示:

RewriteEngine on
RewriteBase /

Options +FollowSymLinks -Indexes

RewriteRule ^video/(.*)$ video.php?id=$1 [L]
RewriteRule ^video/(.*)$([a-zA-Z0-9]+) video.php?id=$1 [L]
RewriteRule ^tag/(.*)/page-(.*)/$ tag.php?tag=$1&page=$2 [L]
RewriteRule ^tag/(.*)/page-(.*)$ tag.php?tag=$1&page=$2 [L]
RewriteRule ^tag/(.*)?$ tag.php?tag=$1
RewriteRule ^page/(.*)$ page.php?id=$1 [L]
RewriteRule ^feed feed.php [L]

我想向我的所有网址添加斜杠,

如下所示:

> example.com/video/video_id/
> 
> example.com/tag/keyword/
> 
> example.com/tag/keyword/page-2/      (3... and so on...)
> 
> example.com/page/name/
> 
> example.com/feed/

我想将当前链接重定向到新的斜杠网址

有人可以帮助我吗?

My current .htaccess file looks like this:

RewriteEngine on
RewriteBase /

Options +FollowSymLinks -Indexes

RewriteRule ^video/(.*)$ video.php?id=$1 [L]
RewriteRule ^video/(.*)$([a-zA-Z0-9]+) video.php?id=$1 [L]
RewriteRule ^tag/(.*)/page-(.*)/$ tag.php?tag=$1&page=$2 [L]
RewriteRule ^tag/(.*)/page-(.*)$ tag.php?tag=$1&page=$2 [L]
RewriteRule ^tag/(.*)?$ tag.php?tag=$1
RewriteRule ^page/(.*)$ page.php?id=$1 [L]
RewriteRule ^feed feed.php [L]

i want to add a slash to all my url's

like this:

> example.com/video/video_id/
> 
> example.com/tag/keyword/
> 
> example.com/tag/keyword/page-2/      (3... and so on...)
> 
> example.com/page/name/
> 
> example.com/feed/

And i want to redirect my current links to the new slash url's

Can somebody help me, please?

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

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

发布评论

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

评论(1

一身仙ぐ女味 2024-10-05 23:26:13

您当前的 htaccess 文件支持尾随 /,尽管您可能更喜欢

RewriteRule ^video/(.*)/$ video.php?id=$1 [L]

这样,这样您就不必处理 video.php 中的 /

只需将所有网址更新为 example.com/video/video_id/ 而不是您正在使用的任何内容(您的框架/平面 HTML 文件)中的 example.com/video/video_id

您的旧网址仍然有效。如果您确实想重定向它们,您可以:

RewriteCond %{REQUEST_URI} ^video/ [NC]
RewriteRule ^video/(.*)$ video.php?id=$1 [L,R=301]

[NC] 表示无大小写检查(因此 /VIDEO)可以工作。 [R=301] 表示永久重定向(对于 SEO 有用)。

仔细检查并扩展您的其他规则。

编辑:

抱歉,我认为之前不太正确。请尝试以下操作:

RewriteEngine on
RewriteBase /

Options +FollowSymLinks -Indexes

RewriteCond %{REQUEST_URI} ^video/ [NC]
RewriteRule ^video/(.*)$ video/$1/ [L,R=301]
RewriteRule ^video/(.*)/$ video.php?id=$1 [L]

RewriteCond %{REQUEST_URI} ^tag/ [NC]
RewriteRule ^tag/(.*)/page-(.*)$ tag/$1/page-$2/ [L,R=301]
RewriteRule ^tag/(.*)/page-(.*)/$ tag.php?tag=$1&page=$2 [L]

...

Your current htaccess file supports a trailing / although you probably would prefer

RewriteRule ^video/(.*)/$ video.php?id=$1 [L]

So that you don't have to handle the / in video.php

Just update all of your URLs to be example.com/video/video_id/ instead of example.com/video/video_id in whatever you are using (your framework/flat HTML files).

Your old URLs will still work. If you really want to redirect them, you can:

RewriteCond %{REQUEST_URI} ^video/ [NC]
RewriteRule ^video/(.*)$ video.php?id=$1 [L,R=301]

The [NC] means No-case checking (so /VIDEO) would work. The [R=301] means permanent redirect (useful for SEO).

Go through and expand for your other rules.

Edit:

Sorry, I don't think it was quite right before. Try the following:

RewriteEngine on
RewriteBase /

Options +FollowSymLinks -Indexes

RewriteCond %{REQUEST_URI} ^video/ [NC]
RewriteRule ^video/(.*)$ video/$1/ [L,R=301]
RewriteRule ^video/(.*)/$ video.php?id=$1 [L]

RewriteCond %{REQUEST_URI} ^tag/ [NC]
RewriteRule ^tag/(.*)/page-(.*)$ tag/$1/page-$2/ [L,R=301]
RewriteRule ^tag/(.*)/page-(.*)/$ tag.php?tag=$1&page=$2 [L]

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