This article was co-authored by Stan Kats. Stan Kats is a Professional Technologist and the COO and Chief Technologist for The STG IT Consulting Group in West Hollywood, California. Stan provides comprehensive technology solutions to businesses through managed IT services, and for individuals through his consumer service business, Stan's Tech Garage. Stan holds a BA in International Relations from The University of Southern California. He began his career working in the Fortune 500 IT world. Stan founded his companies to offer an enterprise-level of expertise for small businesses and individuals.
There are 9 references cited in this article, which can be found at the bottom of the page.
This article has been viewed 1,488,085 times.
There are several reasons to redirect a URL and a few basic ways to approach it. For a website that already has a lot of traffic and good search engine results but needs to change domain addresses, a redirect is a good choice for the transition period. Your traffic still goes to the old domain but is then automatically redirected to the new one. In time, as search engines update their databases, the new domain will pick up its own search results. A redirect can also cause several different URLs to be directed to a single website and can shorten complex URL addresses. Methods for how to redirect a URL depend on what code your website is written in and on how much confidence and experience you have with editing that code.[1]
Steps
Coding an .htaccess 301 Redirect
-
1Find out if your website is running on an Apache server. This is necessary for moving forward with the .htaccess method - check with your web host if you're unsure.
-
2Locate and download your .htaccess file. An .htaccess file is a file that web servers check for information on how to handle errors, security, and redirect requests for your site. Check your root directory (where all your website files are stored) and then download the file for editing.[2]Advertisement
-
3Create an .htaccess file. If no .htaccess exists in your root folder currently, you can create one using an application like Notepad (or a similar plain text application). The code for the file is contained in the next step.[3]
- Make sure that when you save your .htaccess file it begins with a “.”
- Note that this file has no tail extension (e.g. “.com” or “.txt”)
-
4Type in the code. Paste the following code into the .htaccess text file: "redirect 301 /old/oldURL.com http://www.newURL.com"[4]
- In the code, "oldURL.com" represents the landing page address your visitors will need to be redirected from while "http://www.newURL.com" represents the address you'd like your visitors redirected to.
- There should be exactly one blank space between "oldURL.com" and "http://"
- Don't add "http://www" to the (old) URL in the first part of the code![5]
- The code "301" is most commonly used on redirected sites and means "moved permanently". Research other "300" codes to learn about other functions.
-
5Set the new URL destination. Change “http://www.newURL.com” to the domain address you'd like visitors to be redirected to.
-
6Save the new .htaccess file. Change the dropdown to “all files” and save the file as .htaccess with no extension.
-
7Create a backup. Rename any existing .htaccess files or html files with the same name to keep a backup copy. For example use the name .htaccessbackup so that you can find and recognize the file in case you need to do a restore of the previous code.
-
8Upload the modified file to the root directory of the old domain. Now that you've modified the code you need to put this file back so the old URL can read it and redirect as planned.
-
9Test the redirect. Open a new private browsing window and type the old domain name into your web browser. If you've done everything correctly it will redirect to the new site.[6]
- Using a private browsing window simply ensures that your browser is accessing the new redirect instead of relying on cached data (data stored to help your most commonly visited pages load faster).
- In lieu of a private browsing window, you can also clear your browser's cache through the browser preferences menu. For more information on how to do that, see Clear-Your-Browser's-Cache.
Using a Redirection Service
-
1Check with your web host. If you're unsure of your own coding ability or just want to redirect a URL without digging into the code, there are several redirection services available and your current web host may be one of them.[7] Many popular web hosts offer redirection services and the accompanying support to help you achieve your goals. Check on the features your current host/plan offers or contact them directly about what your options are.
-
2Pick a 3rd party service. If your web host doesn't offer redirection, there are many other options out there. Depending on what your redirection needs are you may be able to do this for free.
- Many services allow you to configure options for your redirect such as the type (permanent or temporary) or whether query parameters are passed along.
- Very few redirection services will allow you to forward HTTPS (secure) links.
-
3Follow the instructions from the redirection service. Typically these services are extremely user-friendly and can help walk you through the process, prompting you at each step for the correct information.
- Depending on your website host, you may be able to configure the redirection in your website settings.[8]
- Note: in some cases you'll still need to be able to edit the DNS (domain name server) records for the domain names you'd like to redirect. These are accessible through your web host.
-
4Update the DNS records. Your 3rd party redirection provider will let you know if this is necessary and you can access and edit these records from your web hosting account.
- The instructions to edit DNS records this step will vary by the service used, but typically both the redirection service provider and your web host will have easy-to-follow instructions.
Using a Meta Command
-
1Access the code for the page you want to redirect. This is a different method that involves you changing the webpage's code directly so you'll first need to download the files associated with the URL(s) you'd like to redirect away from.[9]
- Note that in most cases using a Meta command is not ideal for your redirect. Webpages with Meta code redirects are often filtered by search engines since it's a known spam technique.
-
2Open the code for edits. Use “Notepad” or a similar text editor to open the webpage's code file. Save a backup or duplicate copy before making your edits to the code as a safety measure.
-
3Amend the code. The Meta code goes after the "head" tag (<head>) in the page's code. Type in :
.<META http-equiv="refresh" content="0;URL=http://www.newsite.com/newurl.html">[10]- There is exactly one blank space between "refresh" and "content"
- "0" here stands for number of seconds before the redirect happens.
- "www.newsite.com/newurl.html" is the specific URL that the page is to be redirected to.
- It's also possible to create a custom error message or an announcement that your sites has moved, but this may draw unwanted attention to your redirect!
-
4Save the file and re-upload to your old domain. If you are redirecting traffic from an old URL, it's likely that other changes have also taken place to your URL's code (e.g. the removal of your site content). What's important is that the URL code now contains the meta redirect code.
-
5Test the redirect. Type the URL into your browser directly or use a search engine to find it. The page should now redirect immediately to the new URL you specified in the code without any messages or landing points in between.
Using Other Coding Languages
-
1Find out what code your site is written in. For each programming language, the appropriate redirect code may be slightly different. If you're unsure of the answer to this question, contact your web host for more information.
-
2Research other redirect code. There are different coded commands for each language and several options to explore within each language. A quick internet search is likely to yield the appropriate code for your site.
- For instance it's easy to search for and find code for PHP, ASP, ColdFusion and Javascript redirects online.
-
3Test the redirect. After finding the correct code for your site, implementation will be quite similar to the other coding methods outlined. Afterwards always make sure to test the redirect by visiting your (old) URL to see if things work as planned.
Expert Q&A
-
QuestionCan you redirect a URL to a specific page?Stan KatsStan Kats is a Professional Technologist and the COO and Chief Technologist for The STG IT Consulting Group in West Hollywood, California. Stan provides comprehensive technology solutions to businesses through managed IT services, and for individuals through his consumer service business, Stan's Tech Garage. Stan holds a BA in International Relations from The University of Southern California. He began his career working in the Fortune 500 IT world. Stan founded his companies to offer an enterprise-level of expertise for small businesses and individuals.
Professional TechnologistYes, you can—just use your website's settings to redirect your site to a different page. If your host doesn't have that type of setting, set up a very simple site that automatically redirects the visitor to another page. -
QuestionCan I redirect a website that I don't own using only my computer?Community AnswerIt depends on what you mean by redirect. If you mean to redirect incoming traffic from the website to wherever you want it to go, then the short answer is no. It's possible, but also illegal depending on where you are, and what the laws are. If you want to direct a link to a destination of your choice, use something like TinyURL or RarME.
-
QuestionCan it be sent straight to a message with no URL?Community AnswerYou could write a javascript routine that just spits out whatever text you want it to say, but you'd still have to include that in your index.html page. At that point, you might as well just place your message in your index.html or home.html page. Then again, I'm no javascript expert. Check some of the javascript code libraries, you might find what you want; then you just cut and paste.
References
- ↑ https://www.techopedia.com/definition/1708/url-redirect
- ↑ https://www.inmotionhosting.com/support/website/htaccess/where-is-my-htaccess-file
- ↑ https://support.hostway.com/hc/en-us/articles/115000678970-How-to-create-an-htaccess-file-and-what-it-can-be-used-for-
- ↑ https://www.rankpay.com/301-redirects/
- ↑ http://www.isitebuild.com/301-redirect.htm.
- ↑ http://www.tamingthebeast.net/articles3/spiders-301-redirect.htm
- ↑ Stan Kats. Cybersecurity Expert. Expert Interview. 16 July 2021.
- ↑ Stan Kats. Cybersecurity Expert. Expert Interview. 16 July 2021.
- ↑ https://web.pa.msu.edu/services/computing/faq/auto-redirect.html
About This Article
1. Open the .htaccess file on the web server.
2. Type redirect 301 /old/oldURL.com http://www.newURL.com.
3. Replace the "newURL" section of the code with the website you want to redirect to.
4. Save the file and re-upload if necessary.