TL;DR
This guide shows you how to automatically redirect visitors from one website or webpage to another. We’ll cover methods using .htaccess files (for Apache servers), JavaScript, and meta refresh tags. Choose the method that best suits your needs and technical setup.
1. Using .htaccess Files (Apache Servers)
This is the most common and reliable way to redirect on an Apache web server. You’ll need access to your website’s files, usually via FTP or a file manager in your hosting control panel.
- Locate your .htaccess file: It’s often found in the root directory of your website (e.g.,
/public_html). If it doesn’t exist, you can create one – make sure the filename starts with a dot! - Edit the .htaccess file: Add redirection rules to the file. Here are some examples:
- Redirecting a single page: This redirects
/old-page.htmlto/new-page.html.Redirect 301 /old-page.html /new-page.html - Redirecting an entire website: This redirects everything from your old domain (e.g.,
http://www.olddomain.com) to a new domain (e.g.,http://www.newdomain.com).Redirect 301 / http://www.newdomain.com/ - Redirecting with wildcards: This redirects all files in a directory.
Redirect 301 /old-directory/* /new-directory/
- Redirecting a single page: This redirects
- Save the .htaccess file: Make sure to save your changes. The redirection should take effect almost immediately, but it may take a few minutes for the server to update its cache.
- Test the redirection: Visit the old URL in your browser to confirm that you are redirected to the new one.
Important: The 301 code indicates a permanent redirect, which is good for SEO. Use 302 for temporary redirects.
2. Using JavaScript
JavaScript redirection works on all browsers but relies on the user having JavaScript enabled. It’s less reliable than .htaccess.
- Add a script tag to your webpage: Place this code within the
<head>section of the HTML page you want to redirect.<script> window.location.href = "http://www.newdomain.com"; </script> - Save the webpage: Save your HTML file and upload it to your server.
- Test the redirection: Visit the page in your browser. If JavaScript is enabled, you should be redirected automatically.
Note: Some users disable JavaScript for security reasons, so this method isn’t foolproof.
3. Using Meta Refresh Tag
The meta refresh tag redirects after a specified time delay. It’s generally discouraged by SEO experts because of the delay and potential user experience issues.
- Add a meta tag to your webpage: Place this code within the
<head>section of the HTML page.<meta http-equiv="refresh" content="0; url=http://www.newdomain.com"> - Save the webpage: Save your HTML file and upload it to your server.
- Test the redirection: Visit the page in your browser. You should be redirected after 0 seconds (immediately).
Warning: Avoid using a delay longer than necessary, as this can frustrate users. Search engines may also penalize pages with long refresh delays.
Troubleshooting
- .htaccess not working? Check your server configuration to ensure .htaccess files are enabled. Also, verify the syntax of your rules is correct.
- JavaScript redirection failing? Make sure JavaScript is enabled in your browser and that there are no errors in your script.
- Meta refresh tag not redirecting? Double-check the URL in the
contentattribute.