{"id":657,"date":"2024-02-27T10:46:38","date_gmt":"2024-02-27T18:46:38","guid":{"rendered":"https:\/\/blog.iabsolute.com\/?p=657"},"modified":"2024-02-27T10:46:38","modified_gmt":"2024-02-27T18:46:38","slug":"creating-redirects-in-an-htaccess-file","status":"publish","type":"post","link":"https:\/\/blog.iabsolute.com\/?p=657","title":{"rendered":"creating redirects in an .htaccess file"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Common uses of .htaccess file<\/h2>\n\n\n\n<p>The&nbsp;<code>.htaccess<\/code>&nbsp;file has several use cases. The most common examples include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Redirections for certain URLs<\/li>\n\n\n\n<li>Load custom error pages, like 404 pages<\/li>\n\n\n\n<li>Force your website to HTTPS instead of HTTP<\/li>\n\n\n\n<li>Allow or deny specific IP addresses access to your website<\/li>\n\n\n\n<li>Password-protect certain directories on your server<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">When not to use .htaccess?<\/h2>\n\n\n\n<p>The&nbsp;<code>.htaccess<\/code>&nbsp;file is commonly used when you don&#8217;t have access to the main server configuration file&nbsp;<code>httpd.conf<\/code>&nbsp;or virtual host configuration, which only happens if you have purchased shared hosting. You can achieve all of the above-mentioned use cases by editing the main server configuration file(s) (e.g.,&nbsp;<code>httpd.conf<\/code>) or virtual host configuration files, so you should not use&nbsp;<code>.htaccess<\/code>&nbsp;when you have access to those files. Any configuration that you need to put in a&nbsp;<code>.htaccess<\/code>&nbsp;file can just as effectively be added in a&nbsp;<strong>&lt;Directory&gt;<\/strong>&nbsp;section in your main server or virtual host configuration files.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reasons to avoid using .htaccess<\/h2>\n\n\n\n<p>There are two reasons to avoid the use of&nbsp;<code>.htaccess<\/code>&nbsp;files. Let&#8217;s take a closer look at them.<\/p>\n\n\n\n<p><strong>First<\/strong>: Performance &#8211; When&nbsp;<strong>AllowOverride<\/strong>&nbsp;is set to allow the use of&nbsp;<code>.htaccess<\/code>&nbsp;files,&nbsp;<code>httpd<\/code>&nbsp;will look for&nbsp;<code>.htaccess<\/code>&nbsp;files in every directory starting from the parent directory. This will cause a performance impact, whether you&#8217;re using it or not. The&nbsp;<code>.htaccess<\/code>&nbsp;file is loaded every time a document is requested from a directory.<\/p>\n\n\n\n<p>To have a full view of the directives that it must apply,&nbsp;<code>httpd<\/code>&nbsp;will always look for&nbsp;<code>.htaccess<\/code>&nbsp;files starting with the parent directory until it reaches the target sub-directory. If a file is requested from directory&nbsp;<code>\/public_html\/test_web\/content<\/code>,&nbsp;<code>httpd<\/code>&nbsp;must look for the following files:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\/.htaccess<\/code><\/li>\n\n\n\n<li><code>\/public_html\/.htaccess<\/code><\/li>\n\n\n\n<li><code>\/public_html\/test_web\/.htaccess<\/code><\/li>\n\n\n\n<li><code>\/public_html\/test_web\/content\/.htaccess<\/code><\/li>\n<\/ul>\n\n\n\n<p>So, four file-system accesses were performed for each file access from a sub-directory content even if the file is not present.<\/p>\n\n\n\n<p><strong>Second<\/strong>: Security &#8211; granting users permission to make changes in&nbsp;<code>.htaccess<\/code>&nbsp;files gives them full control over the server configuration of that particular website or virtual host. Any directive in the&nbsp;<code>.htaccess<\/code>&nbsp;file has the same effect as any placed in the&nbsp;<code>httpd<\/code>&nbsp;configuration file itself, and changes made to this file are live instantly without a need to restart the server. This can become risky in terms of the security of a webserver and a website.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Enable the .htaccess file<\/h2>\n\n\n\n<p>To enable the&nbsp;<code>.htaccess<\/code>&nbsp;file, you need to have sudo\/root privileges on the server.<\/p>\n\n\n\n<p>Open the&nbsp;<code>httpd<\/code>&nbsp;configuration file of your website:<\/p>\n\n\n\n<p><code>\/etc\/httpd\/conf\/test.conf<\/code><\/p>\n\n\n\n<p>You should add the following configuration directive in the server&#8217;s virtual host file to allow the&nbsp;<code>.htaccess&nbsp;<\/code>file in the&nbsp;<strong>DocumentRoot<\/strong>&nbsp;directory. If the following lines are not added, the&nbsp;<code>.htaccess<\/code>&nbsp;file will not work:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;\/VirtualHost&gt;\n&lt;Directory \/var\/www\/test.com\/public_html&gt;\n&nbsp;&nbsp; &nbsp;<strong>Options<\/strong> Indexes FollowSymLinks\n&nbsp;&nbsp; &nbsp;<strong>AllowOverride<\/strong> All\n&nbsp;&nbsp; &nbsp;<strong>Require<\/strong> all granted\n&lt;\/Directory&gt;<\/code><\/pre>\n\n\n\n<p>In the case of shared hosting, this is already allowed by the hosting service providers. All you need to do is to create a&nbsp;<code>.htaccess<\/code>&nbsp;file in the&nbsp;<code>public_html<\/code>&nbsp;directory to which the service provider has given you access and to which you will upload your website files.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Redirect URLs<\/h2>\n\n\n\n<p>If your goal is to simply redirect one URL to another, the&nbsp;<strong>Redirect<\/strong>&nbsp;directive is the best option you can use. Whenever a request comes from a client on an old URL, it forwards it to a new URL at a new location.<\/p>\n\n\n\n<p>If you want to do a complete redirect to a different domain, you can set the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em># Redirect to a different domain<\/em>\nRedirect 301 \"\/service\" \"https:\/\/newdomain.com\/service\"<\/code><\/pre>\n\n\n\n<p>If you just want to redirect an old URL to a new URL on the same host:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em># Redirect to a URL on the same domain or host<\/em>\nRedirect 301 \"\/old_url.html\" \"\/new_url.html\"\nLoad a custom 404 Error page<\/code><\/pre>\n\n\n\n<p>For a better user experience, load a custom error page when any of the links on your website point to the wrong location or the document has been deleted.<\/p>\n\n\n\n<p>To create a custom 404 page, simply create a web page that will work as a 404 page and then add the following code to your&nbsp;<code>.htaccess<\/code>&nbsp;file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>ErrorDocument<\/strong> 404 \/error\/pagenotfound.html<\/code><\/pre>\n\n\n\n<p>You should change&nbsp;<code>\/error\/pagenotfound.html<\/code>&nbsp;to the location of your 404 page.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Force the use of HTTPS instead of HTTP for your website<\/h2>\n\n\n\n<p>If you want to force your website to use HTTPS, you need to use the&nbsp;<strong>RewriteEngine<\/strong>&nbsp;module in the&nbsp;<code>.htaccess<\/code>&nbsp;file. First of all, you need to turn on the&nbsp;<strong>RewriteEngine<\/strong>&nbsp;module in the&nbsp;<code>.htaccess<\/code>&nbsp;file and then specify the conditions you want to check. If those conditions are satisfied, then you apply rules to those conditions.<\/p>\n\n\n\n<p>The following code snippet rewrites all the requests to HTTPS:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em># Turn on the rewrite engine<\/em>\n<strong>RewriteEngine<\/strong> On\n\n<em># Force HTTPS and WWW<\/em>\n<strong>RewriteCond<\/strong> %{HTTP_HOST} !^www\\.(.*)$ &#91;OR,NC]\n<strong>RewriteCond<\/strong> %{https} off &nbsp;\n<strong>RewriteRule<\/strong> ^(.*)$ https:\/\/www.test-website.com\/$1 &#91;R=301,L]<\/code><\/pre>\n\n\n\n<p>Let&#8217;s go through each line.<\/p>\n\n\n\n<p><strong>RewriteEngine on<\/strong>&nbsp;turns on the&nbsp;<strong>RewriteEngine<\/strong>&nbsp;module. This is required; otherwise, conditions and rules won&#8217;t work.<\/p>\n\n\n\n<p>The first condition checks if&nbsp;<code>www<\/code>&nbsp;is entered.&nbsp;<strong>[OR, NC]<\/strong>&nbsp;stands for&nbsp;<em>no case<\/em>, which means even if the entered URL has a mix of upper or lowercase case letters.<\/p>\n\n\n\n<p>Next, it checks if the HTTPS protocol was already entered by the user.&nbsp;<strong>%{https} off<\/strong>&nbsp;means that HTTPS protocol was not used.<\/p>\n\n\n\n<p>When the&nbsp;<strong>RewriteCond<\/strong>&nbsp;is satisfied, we use&nbsp;<strong>RewriteRule<\/strong>&nbsp;to redirect the URL to HTTPS. Note that in this case, all URLs will be redirected to HTTPS whenever any request is made.<\/p>\n\n\n\n<p><em><strong>[ A free guide from Red Hat:&nbsp;<a href=\"https:\/\/www.redhat.com\/en\/engage\/5-steps-to-automate-your-business-ebook?intcmp=701f20000012ngPAAQ\" target=\"_blank\" rel=\"noreferrer noopener\">5 steps to automate your business<\/a>. ]&nbsp;<\/strong><\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrap up<\/h2>\n\n\n\n<p>Website owners often use the&nbsp;<code>.htaccess<\/code>&nbsp;file to control the behavior of their website. In this article, we have covered the basics of the&nbsp;<code>.htaccess<\/code>&nbsp;file and some common use cases in place on most of the websites.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Common uses of .htaccess file The&nbsp;.htaccess&nbsp;file has several use cases. The most common examples include: When not to use .htaccess? The&nbsp;.htaccess&nbsp;file is commonly used when you don&#8217;t have access to the main server configuration file&nbsp;httpd.conf&nbsp;or virtual host configuration, which only &hellip; <a href=\"https:\/\/blog.iabsolute.com\/?p=657\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,2,3],"tags":[],"class_list":["post-657","post","type-post","status-publish","format-standard","hentry","category-centos","category-my-linux","category-ubuntu"],"_links":{"self":[{"href":"https:\/\/blog.iabsolute.com\/index.php?rest_route=\/wp\/v2\/posts\/657","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iabsolute.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iabsolute.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iabsolute.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iabsolute.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=657"}],"version-history":[{"count":1,"href":"https:\/\/blog.iabsolute.com\/index.php?rest_route=\/wp\/v2\/posts\/657\/revisions"}],"predecessor-version":[{"id":658,"href":"https:\/\/blog.iabsolute.com\/index.php?rest_route=\/wp\/v2\/posts\/657\/revisions\/658"}],"wp:attachment":[{"href":"https:\/\/blog.iabsolute.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iabsolute.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iabsolute.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}