{"id":6283,"date":"2021-05-21T15:23:07","date_gmt":"2021-05-21T15:23:07","guid":{"rendered":"https:\/\/oxhosting.com\/blog\/2021\/05\/21\/how-to-set-up-rules-and-redirects-in-htaccess-hosting\/"},"modified":"2021-05-21T15:23:07","modified_gmt":"2021-05-21T15:23:07","slug":"how-to-set-up-rules-and-redirects-in-htaccess-hosting","status":"publish","type":"post","link":"https:\/\/oxhosting.com\/blog\/2021\/05\/21\/how-to-set-up-rules-and-redirects-in-htaccess-hosting\/","title":{"rendered":"How to set up rules and redirects in .htaccess &#8211; Hosting"},"content":{"rendered":"<p> <script data-ad-client=\"ca-pub-3214842754935876\" async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<\/p>\n<div id=\"article\">&#13;<br \/>\n\t\t<b>.htaccess<\/b> is a directory-level configuration file supported by the Apache web server. It is used to alter web server configuration (enable or disable additional features) for the specific account without changing global server settings.<\/p>\n<p>The .htaccess file takes effect over the entire directory it is placed in, including all files and subdirectories. The changes made in this file will be implemented immediately and no server restart is required.&#13;<br \/>\n&#13;<br \/>\n&#13;\n<\/p>\n<p>How to locate .htaccess file<br \/>List of commonly used .htaccess rules:<\/p>\n<p>Disabling existing .htaccess rules<\/p>\n<p>To access the main .htaccess file of your hosting account, follow the steps below:&#13;<br \/>\n&#13;\n<\/p>\n<p>1. \tLog into your <b>cPanel<\/b>.&#13;<br \/>\n<br \/>2. \tNavigate to the section <b>Files<\/b> &gt;&gt; <b>File Manager<\/b>:&#13;<br \/>\n&#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/How-to-set-up-rules-and-redirects-in-htaccess.png\"\/><\/p>\n<p>3. If you wish to edit .htaccess file for your main domain, navigate to <b>public_html<\/b> folder. If you need to make some changes to the addon domain, move to <i><b>public_html\/youraddondomain.com<\/b><\/i> folder.&#13;<br \/>\n&#13;<br \/>\n&#13;\n<\/p>\n<p>Once there, make sure that <b>Show Hidden files (dotfiles)<\/b> option is enabled in <b>Settings<\/b> menu:&#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621610586_175_How-to-set-up-rules-and-redirects-in-htaccess.png\"\/><\/p>\n<p>4. Locate <b>.htaccess<\/b> file, right-click &gt;&gt; <b>Edit<\/b>:&#13;<br \/>\n&#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621610586_990_How-to-set-up-rules-and-redirects-in-htaccess.png\"\/><\/p>\n<p>5. If there is no .htaccess file located in your File manager, feel free to create a new one using <b>File<\/b> option:&#13;<br \/>\n&#13;\n<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621610586_278_How-to-set-up-rules-and-redirects-in-htaccess.png\"\/><\/p>\n<p>You are ready to add your own configuration rules and save them.&#13;<br \/>\n&#13;<br \/>\n&#13;\n<\/p>\n<div>The <b>common usage<\/b> rules of an .htaccess file are listed below: <\/div>\n<p><b>Authorization\/authentication<\/b> \u2013 specifies security restrictions for a directory\/subdirectory. <br \/>You can password-protect a directory, or several of them, and any time a visitor tries to access it, username and password will be required. &#13;<br \/>\n&#13;<\/p>\n<p>To set up such protection, you need to:&#13;<br \/>\n&#13;\n<\/p>\n<p>1. Create the directory you want to protect in <i>\/home\/cpanel_user\/.htpasswds\/<\/i> folder (e.g., for <i>public_html\/test<\/i> the path will be <i>.htpasswds\/public_html\/test\/).<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/i><br \/>2. Create a <b>passwd<\/b> file in this directory and add hashed access details using this online generator.&#13;<br \/>\n&#13;\n<\/p>\n<p>3. Add the following directives to .htaccess:&#13;<br \/>\n&#13;\n<\/p>\n<pre class=\"prettyprint\">AuthType Basic<br\/>AuthName \"Directory Name\"&#13;\nAuthUserFile \"\/home\/cpanel_user\/.htpasswds\/public_html\/test\/passwd\"&#13;\nrequire valid-user<\/pre>\n<p><b>Blocking<\/b> \u2013 blocks users by IP address or domain. It is very useful to block unwanted visitors or to allow accessing certain sections of the website by its owner, administration area, for example. &#13;<br \/>\n&#13;<br \/>\n<br \/>To set up certain blocking rules, create an .htaccess file with the following text:&#13;<br \/>\n&#13;\n<\/p>\n<ul>\n<li>to allow access to everybody else and block users with an X.X.X.X IP address<\/li>\n<\/ul>\n<pre class=\"prettyprint\">&lt;RequireAll&gt;<br\/>Require all granted<br\/>Require not ip X.X.X.X<br\/>&lt;\/RequireAll&gt;<\/pre>\n<p><\/p>\n<ul>\n<li>to block all the visitors except for the specific X.X.X.X and Y.Y.Y.Y IPs<\/li>\n<\/ul>\n<pre class=\"prettyprint\">&lt;RequireAll&gt;<br\/>Require ip X.X.X.X Y.Y.Y.Y<br\/>&lt;\/RequireAll&gt; <\/pre>\n<p><b>NOTE:<\/b> Do not mix the deprecated <i>Allow<\/i>, <i>Deny<\/i>, and <i>Order <\/i>directives with the new <i>Require <\/i>directive<i>.<\/i><\/p>\n<div><b>Custom Error Pages<\/b> \u2013 allows creating custom error pages for a site. This option is very  useful as it allows you to show website visitors an error message matching your website theme if a URL on your website does not work. This helps to avoid the default &#8216;404 File Not Found&#8217; error for example and allows you to display a customly designed error with the guiding directions back into your website content, rather than leaving puzzled.<\/p>\n<\/div>\n<p>To set up a custom error document, create an .htaccess file with the following text below:&#13;<br \/>\n&#13;<\/p>\n<pre class=\"prettyprint\">ErrorDocument 404 \/404.html<\/pre>\n<p>Whenever a 404 (File Not Found) error appears, this line tells the Apache Web server to load an 404.html file located in the directory root of the domain you set the error page for.&#13;<br \/>\n&#13;<\/p>\n<p><b>NOTE<\/b>: To set up a document for other errors (403, 500, etc.), just replace 404 with the corresponding error code and \/404.html with the path to the error file.<\/p>\n<p><b>Mod_Rewrite<\/b> \u2013 specifies how web pages and URLs are displayed to the visitors.<\/p>\n<p>We would like to draw your attention to the usage of Mod_Rewrite rules in .htaccess file. <\/p>\n<p>By default, Mod_Rewrite maps a URL to a filesystem path. However, it can also be used to redirect one URL to another URL.&#13;\n<\/p>\n<p>Before creating a redirect, you should choose the redirection type which would be more preferable for you:&#13;<br \/>\n &#13;\n<\/p>\n<ul>\n<li><b>Permanent redirect<\/b> has a status code of 301, and unlike the temporary one, it is cached in the browser memory. It implies that the page has been moved and requests all search engines and user agent coming to the page to update the URL in their database. This is the most common type of redirect.<\/li>\n<li><b>Temporary redirect<\/b> means that the page is sending status code 302 to the browser. Code 302 tells the browser not to cache this redirect into its saved data. It will redirect the visitor or search engine, but the search engine will continue to index to the original page. This is the recommended type of redirect, unless you are absolutely sure that you will never change it in the future.&#13;<br \/>\n &#13;<br \/>\n<\/li>\n<\/ul>\n<p>The list of the most common and useful redirects, which can be set through the .htaccess file, can be found below (the domains specified in the examples should be replaced with your own ones):<\/p>\n<p><b>Permanent redirect from<\/b> example.com <b>to<\/b> domain.com&#13;<br \/>\n &#13;\n<\/p>\n<pre class=\"prettyprint\">RewriteEngine On<br\/>RewriteCond %HTTP_HOST ^example.com$ [OR]<br\/>RewriteCond %HTTP_HOST ^www.example.com$<br\/>RewriteRule ^(.*)$ \"http:\/\/domain.com\/$1\" [R=301,L]<\/pre>\n<p><b>Temporary redirect from<\/b> example.com <b>to<\/b> domain.com&#13;<br \/>\n &#13;<\/p>\n<pre class=\"prettyprint\">RewriteEngine On<br\/>RewriteCond %HTTP_HOST ^example.com$ [OR]<br\/>RewriteCond %HTTP_HOST ^www.example.com$<br\/>RewriteRule ^(.*)$ \"http:\/\/domain.com\/\" [R=302,L]<\/pre>\n<p><b>NOTE<\/b>: Below are the examples of permanent redirects. Temporary one can be defined by replacing [R=301,L] with [R=302,L] in the end of the code (where necessary).&#13;<\/p>\n<p><b>Redirect from<\/b>  example.com\/subfolder  <b>to<\/b> domain.com<\/p>\n<pre class=\"prettyprint\">RewriteEngine On<br\/>RewriteCond %HTTP_HOST ^example.com$ [OR]<br\/>RewriteCond %HTTP_HOST ^www.example.com$<br\/>RewriteRule ^subfolder$ \"http:\/\/domain.com\/\" [R=301,L]<\/pre>\n<p><b>Redirect from HTTP to HTTPS<\/b> for example.com<\/p>\n<pre class=\"prettyprint\">RewriteEngine On<br\/>RewriteCond %HTTPS !=on<br\/>RewriteRule .* https:\/\/example.com\/%REQUEST_URI [R,L]<\/pre>\n<p>or<\/p>\n<pre class=\"prettyprint\">RewriteCond %SERVER_PORT 80\u00a0\u00a0\u00a0\u00a0\u00a0 <br\/>RewriteCond %HTTP_HOST ^example.com$ [OR]<br\/>RewriteCond %HTTP_HOST ^www.example.com$ <br\/>RewriteRule ^(.*)$ https:\/\/www.example.com\/$1 [R,L]<\/pre>\n<p><b>Redirect from non-WWW to WWW<\/b><\/p>\n<ul>\n<li>for any domain .htaccess takes effect on:&#13;\n<\/li>\n<\/ul>\n<pre class=\"prettyprint\">RewriteEngine On<br\/>RewriteCond %HTTP_HOST !^www.<br\/>RewriteRule ^(.*)$ http:\/\/www.%HTTP_HOST\/$1 [R=301,L]<\/pre>\n<p><\/p>\n<ul>\n<li>for a certain domain, example.com:&#13;<br \/>\n &#13;\n<\/li>\n<\/ul>\n<pre class=\"prettyprint\">RewriteEngine On\u00a0\u00a0\u00a0\u00a0 <br\/>RewriteCond %HTTP_HOST ^example.com$ [NC]<br\/>RewriteRule ^(.*)$ http:\/\/www.example.com\/$1 [R=301,L]<\/pre>\n<p><b>Redirect from WWW to non-WWW<\/b><\/p>\n<ul>\n<li>for any domain .htaccess takes effect on:&#13;<br \/>\n &#13;\n<\/li>\n<\/ul>\n<pre class=\"prettyprint\">RewriteEngine On<br\/>RewriteCond %HTTP_HOST ^www.(.*)$ [NC]<br\/>RewriteRule ^(.*)$ http:\/\/%1\/$1 [R=301,L]<\/pre>\n<p><\/p>\n<ul>\n<li>for a certain domain, example.com:&#13;<br \/>\n&#13;<br \/>\n&#13;\n<\/li>\n<\/ul>\n<pre class=\"prettyprint\">RewriteEngine On<br\/>RewriteCond %HTTP_HOST ^www.example.com [NC]<br\/>RewriteRule (.*) http:\/\/example.com\/$1 [R=301,L]<\/pre>\n<p><b>Redirect all example.com pages to the corresponding domain.com pages<\/b><\/p>\n<pre class=\"prettyprint\">RedirectMatch 301 ^\/(.*)$ http:\/\/domain.com\/$1<\/pre>\n<p><b>NOTE<\/b>: All the pages\u2019 names have to match on both domains or the redirect will lead to a &#8220;Page not Found&#8221; message on the target website.<\/p>\n<p><b>Redirect one page to a new URL<\/b><\/p>\n<div><b><br \/><\/b><\/p>\n<pre class=\"prettyprint\">Redirect 301 \/old_page.html http:\/\/www.domain.com\/new_page.html&#13;\n<\/pre>\n<p><b>NOTE<\/b>: This might be useful when you want to redirect a deleted page to a 404 error or for SEO purposes after the content references updates.<\/div>\n<p><b>Changes the directory root for the main domain to public_html\/subfolder<\/b><\/p>\n<pre class=\"prettyprint\">RewriteEngine on<br\/>RewriteCond %HTTP_HOST ^(www.)?example.com$<br\/>RewriteCond %REQUEST_URI !^\/subfolder\/<br\/>RewriteCond %REQUEST_FILENAME !-f<br\/>RewriteCond %REQUEST_FILENAME !-d<br\/>RewriteRule ^(.*)$ \/subfolder\/$1<br\/>RewriteCond %HTTP_HOST ^(www.)?example.com$<br\/>RewriteRule ^(\/)?$ subfolder\/index.php [L]<\/pre>\n<p><b>NOTE<\/b>: The .htaccess file should be located in the directory root of the domain you wish to configure certain rules for.&#13;<br \/>\n&#13;<br \/>\n&#13;<\/p>\n<p><b>Disabling existing .htaccess rules<\/b><\/p>\n<p>If you need to disable some of the existing rules, for example, for testing purposes, you can simply comment them out. In order to do so, add the pound sign <b>#<\/b> at the beginning of each line of the rule. <\/p>\n<p>Also, it is possible to disable the line or even the block of lines by selecting ones and using the <b>Ctrl + \/<\/b> shortcut.<\/p>\n<p><img class=\"kb-image\" src=\"https:\/\/oxhosting.com\/blog\/wp-content\/uploads\/2021\/05\/1621610586_435_How-to-set-up-rules-and-redirects-in-htaccess.png\"\/><\/p>\n<p>That&#8217;s it!<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Need any help? Contact our HelpDesk<\/p>\n<\/div>\n<script data-ad-client=\"ca-pub-3214842754935876\" async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n","protected":false},"excerpt":{"rendered":"<p>&#13; .htaccess is a directory-level configuration file supported by the Apache web server. It is used to alter web server configuration (enable or disable additional&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/oxhosting.com\/blog\/wp-json\/wp\/v2\/posts\/6283"}],"collection":[{"href":"https:\/\/oxhosting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/oxhosting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/oxhosting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oxhosting.com\/blog\/wp-json\/wp\/v2\/comments?post=6283"}],"version-history":[{"count":0,"href":"https:\/\/oxhosting.com\/blog\/wp-json\/wp\/v2\/posts\/6283\/revisions"}],"wp:attachment":[{"href":"https:\/\/oxhosting.com\/blog\/wp-json\/wp\/v2\/media?parent=6283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/oxhosting.com\/blog\/wp-json\/wp\/v2\/categories?post=6283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/oxhosting.com\/blog\/wp-json\/wp\/v2\/tags?post=6283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}