子域的 htaccess 重定向 -> 类似名称的子目录?

发布于 2024-07-30 05:24:22 字数 738 浏览 4 评论 0原文

我正在重建一个网站,目前有大量内容停放在如下所示的 URL 上。

http://string.domain.com/year/month/ dd/string-pulled-from-title

由于各种原因,我想将所有新内容停放在如下所示的 URL

http://www.domain.com/blogs/string/year/month/dd/string-pulled-from-title< /a>

我想对未来的内容进行更改,但不希望所有旧的内容都进入 404。

我相信我的 htaccess 中的 301 重定向规则可以解决问题,发送所有通过旧内容传入的引用流量指向新格式的链接。

但这个规则应该是什么样的呢? 我读过一些教程,但在任何示例中都没有找到这种确切的情况。

请注意,我不想对所有子域执行此操作,而只想对大约 10 个特定子域执行此操作。 因此,如果有人可以帮助我找出其中之一,那么我可以将其复制粘贴到每个子域的 htaccess 中 10 次并进行设置。

I'm restructuring a web site with a great deal of content currently parked at URLs that look like this.

http://string.domain.com/year/month/dd/string-pulled-from-title

For various reasons, I'd like to park all new content at URLs that looks like this

http://www.domain.com/blogs/string/year/month/dd/string-pulled-from-title

I'd like to make the change for future content, but don't want all the old stuff to go 404.

I believe a 301 redirect rule in my htaccess will do the trick, sending all referred traffic coming in through old links to the new formats.

But what should this rule look like? I've read a few tutorials but haven't found this exact case in any examples.

Note, I don't want to do this for all subdomains, only for about 10 specific ones. So if someone could help me figure out one of these, then I can copy paste it 10 times in my htaccess for each subdomain and be set.

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

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

发布评论

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

评论(1

国产ˉ祖宗 2024-08-06 05:24:22

将其放入旧站点的 .htaccess 文件中(将域调整为您的实际域):

RewriteEngine On
RewriteRule ^(.*)$ http://example.com/blogs/string/$1 [R=301]

这将抓取旧站点 URL 的这一部分:

year/month/dd/string-pulled-from-title

并将其重定向到新站点新位置:

blogs/string/year/month/dd/string-pulled-from-title

或者,如果您想要一些更多的变量,例如,无需自定义修复每个 .htaccess,请将其放入每个子域的文件中:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]

如果您要重定向到同一域,并且包含 www,将重写规则调整为以下内容:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]

注意第二个 RewriteCond< /code> 检查以确保请求的 URL 包含前导 www,如果目标 URL 本身包含 www,则可能会导致无休止的重定向 并且也会尝试重定向该子域。

Drop this into the .htaccess file of the old site (adjusting the domain to your actual one):

RewriteEngine On
RewriteRule ^(.*)$ http://example.com/blogs/string/$1 [R=301]

This will grab this part of the URL at the old site:

year/month/dd/string-pulled-from-title

and redirect it to the new site under the new location:

blogs/string/year/month/dd/string-pulled-from-title

Alternatively, if you want something a little more variable like, without having to custom fix each .htaccess, drop this in the file for each subdomain instead:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]

If you're redirecting to the same domain, and it includes the www, adjust the rewrite rules to the following:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]

Note the second RewriteCond which checks to make sure that the URL requested does not include the leading www, which may lead to an endless redirect if the destination URL itself includes www and would try and redirect that subdomain as well.

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