301 redirect to new domain htaccess
To set up a 301 redirect from an old domain to a new domain using an .htaccess
file, you can use the following code:
Redirect 301 /old-domain.com http://new-domain.com
This will redirect all requests from old-domain.com
to new-domain.com
.
If you want to redirect specific pages or directories, you can use the following format:
Redirect 301 /old-domain.com/path/to/page http://new-domain.com/new-path/to/page
This will redirect requests from old-domain.com/path/to/page
to new-domain.com/new-path/to/page
.
You can also use regular expressions to match patterns in the old domain and redirect them to the new domain. For example:
Redirect 301 ^/old-domain.com/(.*)$ http://new-domain.com/$1
This will redirect all requests from old-domain.com
to new-domain.com
, preserving the path and query string.
Make sure to update the old-domain.com
and new-domain.com
placeholders with your actual domain names.
Here are some additional tips:
- Make sure to test your redirects thoroughly to ensure they are working as expected.
- You can use the
RedirectMatch
directive instead ofRedirect
if you need more flexibility with regular expressions. - If you have multiple redirects, you can use the
Redirect
directive multiple times or use a singleRedirectMatch
directive with multiple patterns. - If you're using a virtual host, you may need to add the redirect directive to the virtual host configuration file instead of the
.htaccess
file.
Here is an example of a complete .htaccess
file with multiple redirects:
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect old domain to new domain
Redirect 301 /old-domain.com http://new-domain.com
Redirect 301 /old-domain.com/path/to/page http://new-domain.com/new-path/to/page
RedirectMatch 301 ^/old-domain.com/(.*)$ http://new-domain.com/$1
# Other redirects...
</IfModule>
Remember to update the old-domain.com
and new-domain.com
placeholders with your actual domain names.