改变我所有的网站的链接,最好的方法是什么?有的、最好的、我所、链接

2023-09-02 11:28:08 作者:我主宰者你的灵魂

可能重复:   .htaccess改写重定向根URL子目录

我需要改变我的网站的所有链接(2455)。

I need to change all the links(2455) in my site.

将有一个问题,当访问者来自谷歌(使用旧的链接)将作为页 404找不到网页

There will be a problem when visitors come from Google (using the old links) as the page will be 404 page not found

欲重定向旧链接到页面的新版本。例如:

I want to redirect the old links to the new version of the page. For example:

旧链接:

http://example.com/hotel/13234
http://example.com/hotel/132
http://example.com/hotel/323
http://example.com/page/about_us
http://example.com/page/contact

新的联系:(加博客)域名,

New links: (add "blog" ) after domain

http://example.com/blog/hotel/13234
http://example.com/blog/hotel/132
http://example.com/blog/page/about_us
http://example.com/blog/hotel/323

...
...
...

什么是做到这一点的最好方法是什么?

What is the best way to do this?

推荐答案

您可以将其重定向到新的位置

You can redirect them to the new Location with

header('Location: /blog' . $_SERVER['PHP_SELF']);

如果你使用的PHP版本> = 5.4.0,您还可以发送一个适当的HTTP状态code 301(永久移动)与

If you use PHP version >= 5.4.0, you can also send a proper HTTP status code 301 (moved permanently) with

http_response_code(301);

当你想使用的.htaccess 你可以用的 的RewriteCond 和的 重写规则

When you want to use .htaccess you can redirect with RewriteCond and RewriteRule.

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule .* /blog$0 [L,R=301]

的RewriteCond 指令prevents重写URL,它已经与 /博客启动。否则,你会得到一个无限递归。 该重写规则指令prefixes与 /博客,不适用其他规则的 并发送HTTP状态code 301 R = 301

The RewriteCond directive prevents rewriting URLs, which already start with /blog. Otherwise you will get an endless recursion. The RewriteRule directive prefixes all URLs with /blog, doesn't apply further rules L and sends HTTP status code 301 R=301.

对于这项工作,这些指令必须在的.htaccess 允许的。您可以使用相同的指令在你的主的conf文件中或在虚拟主机环境。

For this to work, these directives must be allowed in .htaccess. You can use the same directives in your main conf file or in a virtual host context.