{"id":1410,"date":"2015-01-05T20:28:42","date_gmt":"2015-01-06T02:28:42","guid":{"rendered":"http:\/\/blog.supportpro.com\/?p=1410"},"modified":"2018-10-03T06:30:18","modified_gmt":"2018-10-03T12:30:18","slug":"simple-redirection-using-apache-htaccess-redirection-rules","status":"publish","type":"post","link":"https:\/\/www.supportpro.com\/blog\/simple-redirection-using-apache-htaccess-redirection-rules\/","title":{"rendered":"Simple Redirection Using Apache .htaccess Redirection Rules"},"content":{"rendered":"<p>The Redirect feature allows you to send all of the users that access a domain or a particular page to a different URL. This can be done by using cPanel\u2019s \u201cRedirects\u201d feature or by using .htaccess file if the cPanel redirection does not work properly.<\/p>\n<p>In here we shall discuss few helpful .htaccess codes<\/p>\n<p><strong><span style=\"text-decoration: underline;\">Non-www to www Redirection<\/span><\/strong><\/p>\n<p>RewriteEngine on<br \/>\nRewriteCond %{HTTP_HOST} ^domain.com [NC]<br \/>\nRewriteRule ^(.*)$ http:\/\/www.domain.com\/$1 [L,R=301]<\/p>\n<p><!--more--><br \/>\nR=301 &gt;&gt; The Permanent (301) option will notify the visitor\u2019s browser to update its records.<br \/>\nR=302 &gt;&gt; The Temporary (302) option will not update the visitor\u2019s bookmarks.<\/p>\n<p>This .htaccess rule will redirect the naked domain name to its www domain name. Replace the domain.com with your own domain name.<\/p>\n<p><strong><span style=\"text-decoration: underline;\">http to https Redirection<\/span><\/strong><\/p>\n<p>Following rules will redirect all users to https instead of the ordinary http, based on the port numbers (80 or 443).<\/p>\n<p>RewriteEngine On<br \/>\nRewriteCond %{SERVER_PORT} 80<br \/>\nRewriteRule ^(.*)$ https:\/\/www.domain.com\/$1 [R,L]<\/p>\n<p><strong>OR<\/strong><\/p>\n<p>RewriteEngine On<br \/>\nRewriteCond %SERVER_PORT !443<br \/>\nRewriteRule (.*) https:\/\/www.domain.com\/ [R]<\/p>\n<p>For a Better redirection, we can use the following .htaccess rule which is not based on Port no. This seems to be the most reliable redirection, since it forces all users to https site, with no Port checking or any other extra steps.<\/p>\n<p>RewriteEngine On<br \/>\nRewriteCond %{HTTPS} off<br \/>\nRewriteRule (.*) https:\/\/%{HTTP_HOST}%{REQUEST_URI}<\/p>\n<p><strong><span style=\"text-decoration: underline;\">Redirect Your Domain To A Subfolder<\/span><\/strong><\/p>\n<p>Primary Domain on most hosting services usually use \u201cpublic_html\u201d folder (or root folder) to store its files. The following is a simple way to redirect your primary domain to a subfolder on that domain.<\/p>\n<p>For example anything for \u201cwww.domain.com\u201d or \u201cdomain.com\u201d would be forwarded to a specific folder, requests with other subdomains will be ignored.<\/p>\n<p>This is useful if your WordPress blog is installed to a subfolder and you want the main domain to forward to it.<\/p>\n<p>RewriteEngine on<br \/>\nRewriteCond %{HTTP_HOST} ^domain.com$ [NC,OR]<br \/>\nRewriteCond %{HTTP_HOST} ^www.domain.com$<br \/>\nRewriteCond %{REQUEST_URI} !folder\/<br \/>\nRewriteRule (.*) \/folder\/$1 [L]<\/p>\n<p>Please try to add the following line in the top of your .htaccess file if you are getting a \u201cContinuous Redirect Loop\u201d error in the web browser.<\/p>\n<p><em>ErrorDocument 401 default<\/em><\/p>\n<p>If you are trying to redirect the CMS websites such as Wordpress, Joomla or Magento to its www or SSL websites and are not working neither by using cPanel redirection nor by .htaccess file, then you can try to change the site URL and home URL to the destination address and clear the CMS and browser cache.<\/p>\n<p>If you require help, <a href=\"https:\/\/www.supportpro.com\/requestquote.php\">contact SupportPRO Server Admin<\/a><\/p>\n<p style=\"text-align: center;\"><!--HubSpot Call-to-Action Code --><span id=\"hs-cta-wrapper-9d590242-d641-4383-94b4-8cfd62f0af6b\" class=\"hs-cta-wrapper\"><span id=\"hs-cta-9d590242-d641-4383-94b4-8cfd62f0af6b\" class=\"hs-cta-node hs-cta-9d590242-d641-4383-94b4-8cfd62f0af6b\"><!-- [if lte IE 8]><\/p>\n\n\n\n\n\n<div id=\"hs-cta-ie-element\"><\/div>\n\n\n<![endif]--><a href=\"https:\/\/cta-redirect.hubspot.com\/cta\/redirect\/2725694\/9d590242-d641-4383-94b4-8cfd62f0af6b\"><img decoding=\"async\" id=\"hs-cta-img-9d590242-d641-4383-94b4-8cfd62f0af6b\" class=\"hs-cta-img\" style=\"border-width: 0px;\" src=\"https:\/\/no-cache.hubspot.com\/cta\/default\/2725694\/9d590242-d641-4383-94b4-8cfd62f0af6b.png\" alt=\"Server not running properly? Get A FREE Server Checkup By Expert Server Admins - $125 Value\" \/><\/a><\/span><script charset=\"utf-8\" src=\"https:\/\/js.hscta.net\/cta\/current.js\"><\/script><script type=\"text\/javascript\"> hbspt.cta.load(2725694, '9d590242-d641-4383-94b4-8cfd62f0af6b', {}); <\/script><\/span><!-- end HubSpot Call-to-Action Code --><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Redirect feature allows you to send all of the users that access a domain or a particular page to a different URL. This can be done by using cPanel\u2019s&hellip;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[5,6],"tags":[],"class_list":["post-1410","post","type-post","status-publish","format-standard","hentry","category-general-topics","category-linux-basics"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/1410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/comments?post=1410"}],"version-history":[{"count":6,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/1410\/revisions"}],"predecessor-version":[{"id":3098,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/posts\/1410\/revisions\/3098"}],"wp:attachment":[{"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/media?parent=1410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/categories?post=1410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.supportpro.com\/blog\/wp-json\/wp\/v2\/tags?post=1410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}