{"id":7001,"date":"2017-09-12T15:06:01","date_gmt":"2017-09-12T07:06:01","guid":{"rendered":"http:\/\/rmohan.com\/?p=7001"},"modified":"2017-09-12T15:06:01","modified_gmt":"2017-09-12T07:06:01","slug":"mod_rewriting-an-entire-site","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7001","title":{"rendered":"MOD_REWRITING AN ENTIRE SITE"},"content":{"rendered":"<p>Using mod_rewrite to redirect all pages to one central PHP page\u2019.<\/p>\n<p>On my site, I decided to use an all-index structure, as that\u2019s how I prefer to do things \u2013 it means that the scripting language is more hidden from the end user than if you linked to pages such as \u201csomething-bizarre.jsp\u201d and means that if the scripting language used to create the pages was changed the names of the pages wouldn\u2019t have to be.<br \/>\nIn using mod_rewrite to modify an entire website, the following points needed to be addressed:<\/p>\n<p>Images and CSS files should not be rewritten<br \/>\nSince the only subdomain used by the site is \u2018www\u2019, if the user does not enter it then it should be added automatically and visibly for them.<br \/>\nAll versions of a webpage should be automatically and visibly rewritten to a single URL. i.e. \u2018www.example.com\/somepage\/\u2019, \u2018example.com\/somepage\/\u2019, \u2018www.example.com\/somepage\u2019 and \u2018example.com\/somepage\u2019 should all resolve to \u2018www.example.com\/somepage\/\u2019<br \/>\nOnce all visible rewriting has been completed, the URL should be invisibly redirected to a master page which is able to interpret the URL which the user requested and serve up the correct content.<br \/>\nThe following is what I came up with. Please refer to \u201cmod_rewrite, a beginner\u2019s guide (with examples)\u201d if you need any extra pointers as to what anything means.<\/p>\n<p>###################################################<br \/>\n# Turn the RewriteEngine on.                      #<br \/>\n###################################################<\/p>\n<p>RewriteEngine on<\/p>\n<p>###################################################<br \/>\n# Add a leading www to domain if one is missing.  #<br \/>\n###################################################<br \/>\n# If this rule is used, the rewriting stops here  #<br \/>\n# and then restarts from the beginning with the   #<br \/>\n# new URL                                         #<br \/>\n###################################################<\/p>\n<p>RewriteCond %{HTTP_HOST} !^www\\.<br \/>\nRewriteRule ^(.*)$ http:\/\/www.%{HTTP_HOST}\/$1 [R=301,L]<\/p>\n<p>###################################################<br \/>\n# Do not process images or CSS files further      #<br \/>\n###################################################<br \/>\n# No more processing occurs if this rule is       #<br \/>\n# successful                                      #<br \/>\n###################################################<\/p>\n<p>RewriteRule \\.(css|jpe?g|gif|png)$ &#8211; [L]<\/p>\n<p>###################################################<br \/>\n# Add a trailing slash if needed                  #<br \/>\n###################################################<br \/>\n# If this rule is used, the rewriting stops here  #<br \/>\n# and then restarts from the beginning with the   #<br \/>\n# new URL                                         #<br \/>\n###################################################<\/p>\n<p>RewriteCond %{REQUEST_URI} ^\/[^\\.]+[^\/]$<br \/>\nRewriteRule ^(.*)$ http:\/\/%{HTTP_HOST}\/$1\/ [R=301,L]<\/p>\n<p>###################################################<br \/>\n# Rewrite web pages to one master page            #<br \/>\n###################################################<br \/>\n# \/somepage\/            => master.php             #<br \/>\n#                            ?page=somepage       #<br \/>\n# \/somesection\/somepage => master.php             #<br \/>\n#                            ?section=somesection #<br \/>\n#                            &#038;page=somepage       #<br \/>\n# \/somesection\/somesub\/somepage\/                  #<br \/>\n#                       => master.php             #<br \/>\n#                            ?section=somesection #<br \/>\n#                            &#038;subsection=somesub  #<br \/>\n#                            &#038;page=somepage       #<br \/>\n###################################################<br \/>\n# Variables are accessed in PHP using             #<br \/>\n# $_GET[&#8216;section&#8217;], $_GET[&#8216;subsection&#8217;] and       #<br \/>\n# $_GET[&#8216;page&#8217;]                                   #<br \/>\n###################################################<br \/>\n# No more processing occurs if any of these rules #<br \/>\n# are successful                                  #<br \/>\n###################################################<\/p>\n<p>RewriteRule ^([^\/\\.]+)\/?$ \/master.php?page=$1 [L]<br \/>\nRewriteRule ^([^\/\\.]+)\/([^\/\\.]+)\/?$ \/master.php?section=$1&#038;page=$2 [L]<br \/>\nRewriteRule ^([^\/\\.]+)\/([^\/\\.]+)\/([^\/\\.]+)\/?$ \/master.php?section=$1&#038;subsection=$2&#038;page=$3 [L]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using mod_rewrite to redirect all pages to one central PHP page\u2019.<\/p>\n<p>On my site, I decided to use an all-index structure, as that\u2019s how I prefer to do things \u2013 it means that the scripting language is more hidden from the end user than if you linked to pages such as \u201csomething-bizarre.jsp\u201d and means that [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7001"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7001"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7001\/revisions"}],"predecessor-version":[{"id":7002,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7001\/revisions\/7002"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}