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
Next 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/06/11 07:43] (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>
-RewriteEngine On +  RewriteEngine On 
-RewriteCond %{HTTP_HOST} ^www\.knownhosttest\.com$ [NC] +  RewriteCond %{HTTP_HOST} ^www\.knownhosttest\.com$ [NC] 
-RewriteRule ^(.*)$ http://knownhosttest.com/$1 [L,R=301]+  RewriteRule ^(.*)$ http://knownhosttest.com/$1 [L,R=301]
 </code> </code>
  
 +\\
 ==== Redirect non-www to www (force www) ==== ==== Redirect non-www to www (force www) ====
 <code> <code>
-RewriteEngine On +  RewriteEngine On 
-RewriteCond %{HTTP_HOST} !^www\.knownhosttest\.com$ [NC]  +  RewriteCond %{HTTP_HOST} !^www\.knownhosttest\.com$ [NC]  
-RewriteRule ^(.*)$ http://www.knownhosttest.com/$1 [L,R=301]+  RewriteRule ^(.*)$ http://www.knownhosttest.com/$1 [L,R=301]
 </code> </code>
  
 +\\
 ==== Redirect http to https (force https) ==== ==== Redirect http to https (force https) ====
  
Line 57: Line 76:
 <code> <code>
 <If "%{HTTPS} == 'off'"> <If "%{HTTPS} == 'off'">
-  Redirect permanent "/" "https://domain.tld"+    Redirect permanent "/" "https://domain.tld"
 </If> </If>
 </code> </code>
Line 64: Line 83:
  
 <code> <code>
-RewriteCond %{HTTPS} off +  RewriteCond %{HTTPS} off 
-RewriteRule (.*) https://%{SERVER_NAME}/$1 [R=permanent,L]+  RewriteRule (.*) https://%{SERVER_NAME}/$1 [R=permanent,L]
 </code> </code>
  
 +\\
 ===== Useful Links ===== ===== Useful Links =====
  
-Check out this great tool for genenrating .htaccess files: [[http://beamusup.com/generate-htaccess/|.htaccess Generator Tool]].\\ +Check out this great tool for genenrating .htaccess files: ((http://beamusup.com/generate-htaccess/))[[http://beamusup.com/generate-htaccess/|.htaccess Generator Tool]].\\ 
-Official Apache .htaccess redirect documentation can be found here: [[http://httpd.apache.org/docs/current/rewrite/|Apache Rewrite Guide]].+Official Apache .htaccess redirect documentation can be found here: ((http://httpd.apache.org/docs/current/rewrite/))[[http://httpd.apache.org/docs/current/rewrite/|Apache Rewrite Guide]].
  
  
developmental/how-can-i-use-htaccess-for-redirects.1553524957.txt.gz · Last modified: 2019/03/25 09:42 by Daniel P.