{"id":5104,"date":"2020-04-24T18:49:46","date_gmt":"2020-04-24T18:49:46","guid":{"rendered":"https:\/\/lamp.ciscoar.com\/?page_id=5104"},"modified":"2020-04-28T15:04:11","modified_gmt":"2020-04-28T15:04:11","slug":"apache-url-redirection","status":"publish","type":"page","link":"https:\/\/lamp.ciscoar.com\/?page_id=5104","title":{"rendered":"Apache URL redirection"},"content":{"rendered":"\n\n\n<h2 class=\"wp-block-heading\">The Big Picture<\/h2>\n\n\n\n<p>Do you agree on that &#8220;<strong>A picture is worth a thousand words<\/strong>&#8220;? If so, then you&#8217;ll find the following image is the best way for you to see the big picture of what an Apache URL redirections does, all the involved files are exposed in there.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/apache-mod_rewrite_linux_centos_wordpress.png\"><img loading=\"lazy\" decoding=\"async\" width=\"899\" height=\"504\" src=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/apache-mod_rewrite_linux_centos_wordpress.png\" alt=\"\" class=\"wp-image-5124\" srcset=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/apache-mod_rewrite_linux_centos_wordpress.png 899w, https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/apache-mod_rewrite_linux_centos_wordpress-300x168.png 300w, https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/apache-mod_rewrite_linux_centos_wordpress-768x431.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/a><figcaption>Apache URL redirection &#8211; the big picture<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisite<\/h2>\n\n\n\n<p>There is one prerequisite to comply before starting playing around with the Apache&#8217;s URL redirection, the Apache&#8217;s <strong>mod_rewrite <\/strong>must be enabled.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/linux-apache-mod_rewrite-on-off_300x300.png\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"300\" src=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/linux-apache-mod_rewrite-on-off_300x300.png\" alt=\"\" class=\"wp-image-5152\" srcset=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/linux-apache-mod_rewrite-on-off_300x300.png 300w, https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/linux-apache-mod_rewrite-on-off_300x300-150x150.png 150w, https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/linux-apache-mod_rewrite-on-off_300x300-100x100.png 100w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>This step is mandatory and because of that it was covered on a previous article.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" href=\"\/how-to-enable-apache-mod_rewrite\/\" target=\"_blank\">Enable Apache mod_rewrite<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Actual files<\/h2>\n\n\n\n<p>In this particular case, for our simple test of Apache mod_rewrite, three of the files you saw in the big picture are located in the same place, see image below.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/apache-mod_rewrite_Linux_Centos7_01b.png\"><img loading=\"lazy\" decoding=\"async\" width=\"662\" height=\"271\" src=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/apache-mod_rewrite_Linux_Centos7_01b.png\" alt=\"\" class=\"wp-image-5133\" srcset=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/apache-mod_rewrite_Linux_Centos7_01b.png 662w, https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/apache-mod_rewrite_Linux_Centos7_01b-300x123.png 300w\" sizes=\"auto, (max-width: 662px) 100vw, 662px\" \/><\/a><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">vHost.conf file<\/h2>\n\n\n\n<p>Apache will seek into the defined directory to find the .htaccess file, if the file exist and the &#8220;AllowOverride All&#8221; directive is present on the Virtual Host file, then Apache will read and apply the rule\/s.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/apache-mod_rewrite_Linux_Centos7_02_virtual_host.png\"><img loading=\"lazy\" decoding=\"async\" width=\"724\" height=\"393\" src=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/apache-mod_rewrite_Linux_Centos7_02_virtual_host.png\" alt=\"\" class=\"wp-image-5127\" srcset=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/apache-mod_rewrite_Linux_Centos7_02_virtual_host.png 724w, https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/apache-mod_rewrite_Linux_Centos7_02_virtual_host-300x163.png 300w\" sizes=\"auto, (max-width: 724px) 100vw, 724px\" \/><\/a><figcaption>&#8220;AllowOverride All&#8221; directive and directory where it applies<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">.htaccess file<\/h2>\n\n\n\n<p>As you can see in the image below, the &#8220;redirection rule&#8221; is defined inside the .htaccess file.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/apache-mod_rewrite_Linux_Centos7_03_rule_htaccess.png\"><img loading=\"lazy\" decoding=\"async\" width=\"647\" height=\"384\" src=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/apache-mod_rewrite_Linux_Centos7_03_rule_htaccess.png\" alt=\"\" class=\"wp-image-5130\" srcset=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/apache-mod_rewrite_Linux_Centos7_03_rule_htaccess.png 647w, https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/apache-mod_rewrite_Linux_Centos7_03_rule_htaccess-300x178.png 300w\" sizes=\"auto, (max-width: 647px) 100vw, 647px\" \/><\/a><figcaption>Redirection rule<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Regular expressions used in the rewrite rule<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"apacheconf\" class=\"language-apacheconf\">RewriteEngine On\n\nRewriteRule ^index.html$ test.html<\/code><\/pre>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-text-align-center is-layout-flow wp-block-quote-is-layout-flow\"><p>The caret symbol &#8220;^&#8221; means \u201cthe string <strong>starts <\/strong>with\u201d <\/p><cite><em>Caret symbol<\/em> meaning<\/cite><\/blockquote>\n\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-text-align-center is-layout-flow wp-block-quote-is-layout-flow\"><p>T<em>he dollar sign $ means \u201cthe string <strong>ends <\/strong>with\u201d<\/em><\/p><cite>Dollar sign meaning<\/cite><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">index.html file &#8211; code<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"markup\" class=\"language-markup\">&lt;!DOCTYPE html>\n&lt;html lang=\"en\">\n  &lt;head>\n    &lt;meta charset=\"utf-8\">\n    &lt;title>Welcome Index&lt;\/title>\n  &lt;\/head>\n  &lt;body>\n    &lt;h1>Index.htm Success! &lt;\/h1>\n  &lt;\/body>\n&lt;\/html><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">index.html file &#8211; page<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/index.html-page.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"653\" height=\"269\" src=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/index.html-page.jpg\" alt=\"\" class=\"wp-image-5140\" srcset=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/index.html-page.jpg 653w, https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/index.html-page-300x124.jpg 300w\" sizes=\"auto, (max-width: 653px) 100vw, 653px\" \/><\/a><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">test.html file &#8211; code<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"markup\" class=\"language-markup\">&lt;!DOCTYPE html>\n&lt;html lang=\"en\">\n  &lt;head>\n    &lt;meta charset=\"utf-8\">\n    &lt;title>Test&lt;\/title>\n  &lt;\/head>\n  &lt;body>\n    &lt;h1>Mod_Rewrite first URL rewrite&lt;\/h1>\n  &lt;\/body>\n&lt;\/html><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">test.html file &#8211; page<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/apache-mod_rewrite_Linux_Centos7_04_sucess.png\"><img loading=\"lazy\" decoding=\"async\" width=\"653\" height=\"269\" src=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/apache-mod_rewrite_Linux_Centos7_04_sucess.png\" alt=\"\" class=\"wp-image-5136\" srcset=\"https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/apache-mod_rewrite_Linux_Centos7_04_sucess.png 653w, https:\/\/lamp.ciscoar.com\/wp-content\/uploads\/2020\/04\/apache-mod_rewrite_Linux_Centos7_04_sucess-300x124.png 300w\" sizes=\"auto, (max-width: 653px) 100vw, 653px\" \/><\/a><figcaption>Success!!! Apache URL redirection completed<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Making use of the <strong>mod_rewrite <\/strong>module, and the related files and configurations, Apache was able to redirect the default URL pointing to &#8220;index.html&#8221; to the &#8220;test.html&#8221; file, and this concludes our article covering the use of <strong>mod_rewrite <\/strong>making a simple URL redirection.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Big Picture Do you agree on that &#8220;A picture is worth a thousand words&#8220;? If so, then you&#8217;ll find the following image is the best way for you to see the big picture of what an Apache URL redirections does, all the involved files are exposed in there. Prerequisite There is one prerequisite to &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/lamp.ciscoar.com\/?page_id=5104\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Apache URL redirection&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5104","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/lamp.ciscoar.com\/index.php?rest_route=\/wp\/v2\/pages\/5104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lamp.ciscoar.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/lamp.ciscoar.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/lamp.ciscoar.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lamp.ciscoar.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5104"}],"version-history":[{"count":43,"href":"https:\/\/lamp.ciscoar.com\/index.php?rest_route=\/wp\/v2\/pages\/5104\/revisions"}],"predecessor-version":[{"id":5273,"href":"https:\/\/lamp.ciscoar.com\/index.php?rest_route=\/wp\/v2\/pages\/5104\/revisions\/5273"}],"wp:attachment":[{"href":"https:\/\/lamp.ciscoar.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}