可能重复: .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.
上一篇:codeigniter子文件夹文件夹、codeigniter
下一篇:htaccess的的mod_rewrite的Opencart搜索页面的网址页面、网址、htaccess、mod_rewrite