Using .htaccess to redirect from one domain to another

筅森魡賤 提交于 2019-12-03 20:41:31

You need to add commands like this to your .htaccess file:

redirect permanent /some-article/ http://www.example.com/some-article/

Is this a server with mod_rewrite? In this case you could do a generic redirection for all paths:

RewriteEngine On

RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]

I would modify your existing rewrite block to look like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# New code to redirect from example.net to example.com
# Permanent redirect for caching purposes, also include the query string
RewriteCond %{HTTP_HOST} ^example\.net
RewriteRule (.*) http://example.com/$1 [R=permanent,QSA,L]

# New code to redirect from www.example.net to www.example.com
RewriteCond %{HTTP_HOST} ^www\.example\.net
RewriteRule (.*) http://www.example.com/$1 [R=permanent,QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

AddHandler php5-script .php

Note that I haven't actually tested this...

REMOTE_HOST may also work instead of HTTP_HOST. The mod_rewrite documentation recommends using HTTP_HOST, although this will not work if the brower only understands HTTP/1.0

I converted from Drupal to WordPress a while ago, and I ended up spending a bunch of time trying to get these redirects to work properly. My big hangup was figuring out how to use RewriteCond in conjunction with RewriteRule. Here's an excerpt from an article I wrote about this:


# Rewrite drupal urls to worpress
RewriteCond %{QUERY_STRING} ^q=node/(.+)$
RewriteRule ^(.*)$ http://blog.componentoriented.com/?p=%1 [R=301,L]

# Forward RSS feed
RewriteCond %{QUERY_STRING} ^q=rss.xml$
RewriteRule ^(.*)$ http://blog.componentoriented.com/?feed=rss2 [R=301,L]
RewriteCond %{QUERY_STRING} ^q=atom/feed$
RewriteRule ^(.*)$ http://blog.componentoriented.com/?feed=rss2 [R=301,L]

It's also really helpful to keep an eye on inbound hits and links to see if anyone is using an old form of URL that you're not translating yet. I had an inbound link that was encoded as a PostNuke URL (from two blog platforms ago!), and this technique made it really easy to fix.

Here's a link to my article, btw: Use .htaccess to redirect from Drupal to Wordpress

old Domain: RewriteCond %{HTTP_HOST} !^abcd.com$ [NC]

New Domain: RewriteRule ^(.*)$ http://abcdef.com/$1 [R=301,L]

I bet if you put this at your old domain and simply change the second line to your new use your new domain, it would work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!