KNOWNHOST WIKI

User Tools

Site Tools


developmental:how-can-i-use-htaccess-for-redirects

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
developmental:how-can-i-use-htaccess-for-redirects [2019/03/25 09:42]
Daniel P. [Useful Links]
developmental:how-can-i-use-htaccess-for-redirects [2020/05/13 13:35] (current)
Karson N.
Line 7: Line 7:
 There are different types of redirects that can be performed via .htaccess files. Let's break them down and understand each type before learning how to use them. There are different types of redirects that can be performed via .htaccess files. Let's break them down and understand each type before learning how to use them.
  
 +\\
 ===== 301 (permanent) redirect ===== ===== 301 (permanent) redirect =====
 This is the most common type and typically what you'll want to use. While the difference between 301 and 302 is small, it is notable. 301 (permenant) indicates that the old link url (redirected URL) is no longer in use. Whereas the 302 redirect suggests that the old URL is temporarily offline, returning shortly. ​ This is the most common type and typically what you'll want to use. While the difference between 301 and 302 is small, it is notable. 301 (permenant) indicates that the old link url (redirected URL) is no longer in use. Whereas the 302 redirect suggests that the old URL is temporarily offline, returning shortly. ​
  
 +\\
 ===== 302 (temporary) redirect ===== ===== 302 (temporary) redirect =====
  
Line 16: Line 18:
 For all documentation/​article purposes, we will be using 301 redirects. For all documentation/​article purposes, we will be using 301 redirects.
  
 +\\
 ===== Creating the .htaccess file and uploading it to your server ===== ===== Creating the .htaccess file and uploading it to your server =====
 Creating the .htaccess file, to use for 301 or 302 redirects is simple. You'll simply need to open your favorite text/​document editor and create a new blank file. Add the relevant code/​content to the document and save the file as "​htaccessrules.txt"​. After you've confirmed the accurate rules inside the document, use your favorite FTP((or [[software:​how-can-i-configure-filezilla-to-upload-files-via-sftp|SFTP]])) Client and upload the document to your website'​s document root (public_html). Once you've uploaded the document, simply rename it .htaccess ​ Creating the .htaccess file, to use for 301 or 302 redirects is simple. You'll simply need to open your favorite text/​document editor and create a new blank file. Add the relevant code/​content to the document and save the file as "​htaccessrules.txt"​. After you've confirmed the accurate rules inside the document, use your favorite FTP((or [[software:​how-can-i-configure-filezilla-to-upload-files-via-sftp|SFTP]])) Client and upload the document to your website'​s document root (public_html). Once you've uploaded the document, simply rename it .htaccess ​
Line 21: Line 24:
 Via SSH or the terminal, this is easily done using your favorite text editor (eg. nano or vim) by creating a new file locally. ​ Via SSH or the terminal, this is easily done using your favorite text editor (eg. nano or vim) by creating a new file locally. ​
  
 +\\
 ===== Redirect scenarios and code examples ===== ===== Redirect scenarios and code examples =====
  
 Here are some examples of some common things .htaccess rewrite or redirect rules are used for. Here are some examples of some common things .htaccess rewrite or redirect rules are used for.
  
 +\\
 ==== Redirect a single page URL (html/php file) to another ==== ==== Redirect a single page URL (html/php file) to another ====
 <​code>​Redirect 301 /​retiredpage.html http://​www.knownhosttest.com/​newpage.html</​code>​ <​code>​Redirect 301 /​retiredpage.html http://​www.knownhosttest.com/​newpage.html</​code>​
  
 +\\
 ==== Redirect a folder/​directory URL (including all sub contents) to another ==== ==== Redirect a folder/​directory URL (including all sub contents) to another ====
 <​code>​RedirectMatch 301 ^/oldname/ http://​www.knownhosttest.com/​newname/</​code>​ <​code>​RedirectMatch 301 ^/oldname/ http://​www.knownhosttest.com/​newname/</​code>​
  
 +\\
 ==== Redirect an entire domain name to another ==== ==== Redirect an entire domain name to another ====
 <​code>​RedirectMatch 301 ^(.*)$ http://​www.knownhosttest.com</​code>​ <​code>​RedirectMatch 301 ^(.*)$ http://​www.knownhosttest.com</​code>​
  
 +\\
 ==== Redirect .html files to .php files ==== ==== Redirect .html files to .php files ====
 <​code>​RedirectMatch 301 (.*)\.html$ http://​www.knownhosttest.com$1.php</​code>​ <​code>​RedirectMatch 301 (.*)\.html$ http://​www.knownhosttest.com$1.php</​code>​
  
 +\\
 ==== Redirect www to non-www (remove www) ==== ==== Redirect www to non-www (remove www) ====
 <​code>​ <​code>​
Line 44: Line 53:
 </​code>​ </​code>​
  
 +\\
 ==== Redirect non-www to www (force www) ==== ==== Redirect non-www to www (force www) ====
 <​code>​ <​code>​
Line 51: Line 61:
 </​code>​ </​code>​
  
 +\\
 ==== Redirect http to https (force https) ==== ==== Redirect http to https (force https) ====
  
Line 68: Line 79:
 </​code>​ </​code>​
  
 +\\
 ===== Useful Links ===== ===== Useful Links =====
  
developmental/how-can-i-use-htaccess-for-redirects.txt · Last modified: 2020/05/13 13:35 by Karson N.