{"id":23,"date":"2017-08-04T22:48:00","date_gmt":"2017-08-04T22:48:00","guid":{"rendered":"https:\/\/www.barryhubbard.com\/?p=23"},"modified":"2023-12-31T01:53:50","modified_gmt":"2023-12-31T01:53:50","slug":"playing-with-mod_rewrite","status":"publish","type":"post","link":"https:\/\/barryhubbard.com\/index.php\/2017\/08\/04\/playing-with-mod_rewrite\/","title":{"rendered":"Playing with mod_rewrite"},"content":{"rendered":"\n<p><strong>What is mod_rewrite<\/strong><\/p>\n\n\n\n<p>mod_rewrite is a useful Apache module that allows for visible and invisible redirecting of websites. It can be used to create search engine and user friendly URL&#8217;s in place of complicated php URL&#8217;s, complete with variables. The rules can be placed in either the <strong>httpd.conf<\/strong> file or the local <strong>.htaccess<\/strong> file. Here&#8217;s an example file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Allow Override all\nRewriteEngine on\nRewriteRule ^resources \/index.php?option=com_content&amp;amp;view=article&amp;amp;id=172&amp;amp;Itemid=53\nRewriteRule ^(&#91;a-zA-z0-9]+)$ \/index.php<\/code><\/pre>\n\n\n\n<p>Here&#8217;s what we did:<\/p>\n\n\n\n<p><strong>Allow Override<\/strong><\/p>\n\n\n\n<p>If you would like to be able to put Rewrite rules within a particular directory, you must first allow .htaccess files to have rewrite rules. You can do this by having the following code in your httpd.conf file:<\/p>\n\n\n\n<p>AllowOverride all<\/p>\n\n\n\n<p>You can limit this in any way you would like. The above code would allow any directive to be included in the .htaccess file. You may wish to be more restrictive for security reasons.<\/p>\n\n\n\n<p><strong>Turn Rewrite engine on<\/strong><\/p>\n\n\n\n<p>In the directory where you would like the redirecting to take place, either create or edit your .htaccess file and add the following line:<\/p>\n\n\n\n<p>RewriteEngine on<\/p>\n\n\n\n<p><strong>Create Rules<\/strong><\/p>\n\n\n\n<p>In the .htaccess file, create a rewrite rule for each page you would like to redirect. You can use regular expressions if you have multiple similar pages to redirect. The basic rule has the following syntax:<\/p>\n\n\n\n<p>RewriteRule ^MYLINK \/index.php?myvar=myval or as an example RewriteRule ^resources \/index.php?option=com_content&amp;view=article&amp;id=172&amp;Itemid=53<\/p>\n\n\n\n<p>The above example will create an invisible link, meaning that the user will only see yoursite\/resources in the address bar, and not the fully resolved link. If you would like to have a visible link, because the site has permanently moved the page, then you can either include an [R] at the end of the line, or include the full path, starting with http in the link, instead of starting with the base directory, ie: RewritRule ^resources http:\/\/mydomain.com\/index.php?option=com_content&amp;view=article&amp;id=172&amp;Itemid=53 The above Rule will only be visible on newer versions of Apache.<\/p>\n\n\n\n<p><strong>Create Catch All Rule<\/strong> It might be useful to have a catch all rule to catch other &#8220;friendly&#8221; URL&#8217;s which cannot be properly resolved. The following rule will redirect any pages which aren&#8217;t resolved back to index.php<\/p>\n\n\n\n<p>RewriteRule ^([a-zA-z0-9]+)$ \/index.php<\/p>\n\n\n\n<p>That&#8217;s all it takes. Happy Redirecting!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is mod_rewrite mod_rewrite is a useful Apache module that allows for visible and invisible redirecting of websites. It can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[4],"tags":[],"class_list":["post-23","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/barryhubbard.com\/index.php\/wp-json\/wp\/v2\/posts\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barryhubbard.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barryhubbard.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barryhubbard.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barryhubbard.com\/index.php\/wp-json\/wp\/v2\/comments?post=23"}],"version-history":[{"count":1,"href":"https:\/\/barryhubbard.com\/index.php\/wp-json\/wp\/v2\/posts\/23\/revisions"}],"predecessor-version":[{"id":24,"href":"https:\/\/barryhubbard.com\/index.php\/wp-json\/wp\/v2\/posts\/23\/revisions\/24"}],"wp:attachment":[{"href":"https:\/\/barryhubbard.com\/index.php\/wp-json\/wp\/v2\/media?parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barryhubbard.com\/index.php\/wp-json\/wp\/v2\/categories?post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barryhubbard.com\/index.php\/wp-json\/wp\/v2\/tags?post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}