更改我的所有网站链接:最好的方法是什么? [英] Changing all my website links: what is the best way?

查看:98
本文介绍了更改我的所有网站链接:最好的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

.htaccess重写以将根URL重定向到子目录

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

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

访问者来自Google(使用旧链接)时会出现问题由于页面将 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

...
...
...

什么是最好的方法吗?

推荐答案

您可以将它们重定向到新的位置 with

You can redirect them to the new Location with

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

如果您使用PHP版本> = 5.4.0,您还可以发送正确的HTTP状态代码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 进行重定向 RewriteRule

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 指令防止重写已经以 / blog 开头的URL。否则你将获得无休止的递归。
RewriteRule 指令前缀为 / blog 的所有网址,不适用于其他规则 L 并发送HTTP状态码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.

这篇关于更改我的所有网站链接:最好的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆