问题
Possible Duplicate:
.htaccess rewrite to redirect root URL to subdirectory
I need to change all the links(2455) in my site.
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:
Old links:
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?
回答1:
You can redirect them to the new Location with
header('Location: /blog' . $_SERVER['PHP_SELF']);
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);
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]
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.
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.
回答2:
Using mod_rewrite:
RewriteEngine On
RewriteRule $ /blog [L,R=301]
This will prefix all links with blog and permanently redirect them.
来源:https://stackoverflow.com/questions/14073832/changing-all-my-website-links-what-is-the-best-way