{"id":701,"date":"2017-05-04T10:24:35","date_gmt":"2017-05-04T10:24:35","guid":{"rendered":"https:\/\/chemicloud.com\/kb\/?post_type=article&#038;p=701"},"modified":"2021-01-14T21:47:10","modified_gmt":"2021-01-14T21:47:10","slug":"redirect-domain-using-htaccess","status":"publish","type":"ht_kb","link":"https:\/\/chemicloud.com\/kb\/article\/redirect-domain-using-htaccess\/","title":{"rendered":".htaccess 101 &#8211; Rewrite and Redirection"},"content":{"rendered":"<p class=\"h2 title\">This article will explain <strong>how to redirect your domain using a .htaccess file<\/strong> using common redirect rules.<\/p>\n<p><strong>.htaccess<\/strong> is a configuration file for use on web servers running the Apache Web Server software.<\/p>\n<p>Since .htaccess is a hidden system file, please make sure your FTP client is configured to show hidden files. This is usually an option in the program&#8217;s preferences\/options.<\/p>\n    \t\t<div class=\"hts-messages hts-messages--alert  hts-messages--withtitle hts-messages--withicon \"   >\r\n    \t\t\t<span class=\"hts-messages__title\">Note<\/span>    \t\t\t    \t\t\t\t<p>\r\n    \t\t\t\t\tWe&#8217;ll assume that you have\u00a0<code>mod_rewrite<\/code>\u00a0installed and enabled on your server.    \t\t\t\t<\/p>\r\n    \t\t\t    \t\t\t\r\n    \t\t<\/div><!-- \/.ht-shortcodes-messages -->\r\n    \t\t\n<h2 id=\"how-to-redirect-http-requests-to-https\">How to Redirect HTTP Requests to HTTPS<\/h2>\n<p>In order to redirect your website to be opened through HTTPS, you should add the following rewrite rule to your .htaccess file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">RewriteEngine On\r\nRewriteCond %{HTTPS} !=on\r\nRewriteRule .* https:\/\/www.domain.com%{REQUEST_URI} [R,L]<\/pre>\n<p>This will redirect your domain to https:\/\/www.domain.com<\/p>\n<p>If you wish the redirect to work without www, you should remove it from the rewrite rule:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">RewriteEngine On\r\nRewriteCond %{HTTPS} !=on\r\nRewriteRule .* https:\/\/domain.com%{REQUEST_URI} [R,L]<\/pre>\n<p>This will redirect your domain to https:\/\/domain.com<\/p>\n<h2 id=\"how-to-redirect-an-old-domain-to-a-new-domain\">How to Redirect an Old Domain to a New Domain<\/h2>\n<p>In order to redirect your old domain to a new domain name, you should add the following rewrite rule to your .htaccess file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">RewriteEngine on\r\nRewriteBase \/\r\nRewriteRule (.*) http:\/\/www.new-domain.com\/$1 [R=301,L][\/php]<\/pre>\n<h2 id=\"how-to-redirect-from-a-non-www-to-www-url\">How to Redirect From a Non-Www to Www URL<\/h2>\n<p>In order to redirect your site from a non-www to www URL, you should add the following rewrite rule to your .htaccess file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">RewriteEngine on\r\nRewriteBase \/\r\nrewritecond %{http_host} ^domain.com [nc]\r\nrewriterule ^(.*)$ http:\/\/www.domain.com\/$1 [r=301,nc]<\/pre>\n<h2 id=\"how-to-redirect-a-domains-page-to-a-different-page\">How to Redirect a Domain&#8217;s Page to a Different Page<\/h2>\n<p>In order to redirect a specific page or section of your site to a different one, you should add the following rewrite rule to your .htaccess file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Redirect 301 \/page-name http:\/\/domain.com\/new-page<\/pre>\n<h2 id=\"how-to-redirect-an-entire-site-to-a-subfolder\">How to Redirect an Entire Site to a Subfolder<\/h2>\n<p>In order to redirect your entire and for it to load from a subfolder, you should add the following rewrite rule to your .htaccess file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Redirect 301 \/ http:\/\/domain.com\/subfolder-name\/<\/pre>\n<h2 id=\"how-to-redirect-a-subfolder-to-a-different-domain\">How to Redirect a Subfolder to a Different Domain<\/h2>\n<p>In order to redirect a subfolder to a different site, you should add the following rewrite rule to your .htaccess file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Redirect 301 \/subfolder-name http:\/\/different-domain.com\/<\/pre>\n<h2 id=\"how-to-redirect-all-files-with-a-certain-extension-but-retain-the-file-name\">How to Redirect All Files with a Certain Extension but Retain the File Name<\/h2>\n<p>If you want a .html extension to use the same filename but use the .php extension, you should add the following rewrite rule to your .htaccess file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">RedirectMatch 301 (.*)\\.html$ http:\/\/domain.com$1.php<\/pre>\n<h2 id=\"how-to-redirect-one-directory-to-another\">How to Redirect One Directory to Another<\/h2>\n<p>In order to redirect one directory or subfolder to a different one, you should add the following rewrite rule to your .htaccess file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Options +FollowSymLinks\r\nRewriteEngine On\r\nRewriteRule ^(.*)\/old-directory\/(.*)$ $1\/new-directory\/$2 [R,L]<\/pre>\n<h2 id=\"how-to-redirect-from-a-blog-subdomain-to-a-blog-folder\">How to Redirect from a Blog Subdomain to a Blog Folder<\/h2>\n<p>In order to redirect blog.oldsite.com to www.newsite.com\/blog\/ you should add the following rewrite rule to your .htaccess file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Options +FollowSymLinks\r\nRewriteEngine On\r\nRewriteCond %{REQUEST_URI}\/ blog\r\nRewriteRule ^(.*) http:\/\/www.new-domain.com\/%{REQUEST_URI} [R=302,NC]\r\nRewriteRule ^(.*) http:\/\/www.new-domain.com\/blog\/%{REQUEST_URI} [R=302,NC]<\/pre>\n<h2 id=\"alias-clean-urls\">Alias \u201cClean\u201d URLs<\/h2>\n<p>The following snippet lets you use \u201cclean\u201d URLs &#8212; those without an HTML extension, e.g. example.com\/users instead of example.com\/users.html.<\/p>\n<pre>RewriteEngine On\r\nRewriteCond %{SCRIPT_FILENAME} !-d\r\nRewriteRule ^([^.]+)$ $1.html [NC,L]<\/pre>\n<h2 id=\"other-useful-htaccess-redirects-with-examples\">Other Useful .Htaccess Redirects with Examples<\/h2>\n<h3 id=\"rewrite-and-redirect-urls-with-query-parameters-files-placed-in-the-root-directory\">Rewrite and redirect URLs with query parameters (files placed in the root directory)<\/h3>\n<p><strong>Original URL<\/strong>: http:\/\/www.yourdomain.tld\/index.php?id=1<br \/>\n<strong>Desired destination URL<\/strong>: http:\/\/www.yourdomain.tld\/path-to-new-location\/<\/p>\n<p>You should add the following rewrite rule to your .htaccess file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">RewriteEngine on\r\nRewriteCond %{QUERY_STRING} id=1\r\nRewriteRule ^index\\.php$ \/path-to-new-location\/? [L,R=301]\r\nRedirect URLs with query parameters (files placed in subdirectory)<\/pre>\n<h3 id=\"redirect-urls-with-query-parameters-files-placed-in-a-subdirectory\">Redirect URLs with query parameters (files placed in a subdirectory)<\/h3>\n<p><strong>Original URL<\/strong>: http:\/\/www.yourdomain.tld\/sub-directory\/index.php?id=1<br \/>\n<strong>Desired destination URL<\/strong>: \u00a0http:\/\/www.yourdomain.tld\/path-to-new-location\/<\/p>\n<p>You should add the following rewrite rule to your .htaccess file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">RewriteEngine on\r\nRewriteCond %{QUERY_STRING} id=1\r\nRewriteRule ^sub-directory\/index\\.php$ \/path-to-new-location\/? [L,R=301]\r\nRedirect one clean URL to a new clean URL<\/pre>\n<p><strong>That&#8217;s a wrap!<\/strong><\/p>\n<p>Remember that ChemiCloud&#8217;s infrastructure supports Apache rewrite rules out of the box! If you\u2019d like to experience\u00a0<strong>top speed and reliability<\/strong>\u00a0and have access to our amazing <strong>Support Team<\/strong>, try our\u00a0<a href=\"https:\/\/chemicloud.com\/web-hosting\/\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>Web hosting!<\/strong><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article will explain how to redirect your domain using a .htaccess file using common redirect rules. .htaccess is a configuration file for use on web servers running the Apache Web Server software. Since .htaccess is a hidden system file, please make sure your FTP client is configured to show&#8230;<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"ht-kb-category":[190],"ht-kb-tag":[],"class_list":["post-701","ht_kb","type-ht_kb","status-publish","format-standard","hentry","ht_kb_category-domains"],"_links":{"self":[{"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb\/701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb"}],"about":[{"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/comments?post=701"}],"version-history":[{"count":12,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb\/701\/revisions"}],"predecessor-version":[{"id":4364,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb\/701\/revisions\/4364"}],"wp:attachment":[{"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/media?parent=701"}],"wp:term":[{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb-category?post=701"},{"taxonomy":"ht_kb_tag","embeddable":true,"href":"https:\/\/chemicloud.com\/kb\/wp-json\/wp\/v2\/ht-kb-tag?post=701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}