{"id":849,"date":"2018-10-18T16:09:32","date_gmt":"2018-10-18T16:09:32","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=849"},"modified":"2018-10-21T00:35:24","modified_gmt":"2018-10-21T00:35:24","slug":"how-to-setup-mod_rewrite-in-apache","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/18\/how-to-setup-mod_rewrite-in-apache\/","title":{"rendered":"How To Setup mod_rewrite In Apache"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxadmin.io\/wp-content\/uploads\/2017\/08\/mod_rewrite.png\" alt=\"Mod_rewrite on Apache\" width=\"667\" height=\"223\" \/><\/p>\n<p>mod_rewrite is a <a href=\"https:\/\/httpd.apache.org\/\">Apache <\/a>module installed on linux servers to manipulate URLs submitted in the browser to perform other functions than it appears. Mod_rewrite can improve SEO appearing to give it a static appearance.<\/p>\n<p>This guide assumes you already have Apache installed, if you do not please see <a href=\"https:\/\/linuxadmin.io\/compile-apache-2-4-source\/\">How to Install Apache<\/a><\/p>\n<h2>Enable mod_rewrite<\/h2>\n<p>You will want to edit the main Apache configuration file<\/p>\n<p>nano \/etc\/httpd\/conf\/httpd.conf<\/p>\n<p>Add or un-comment the following line<\/p>\n<p>LoadModule rewrite_module modules\/mod_rewrite.so<\/p>\n<p>Once you have saved the file you can go ahead and restart Apache<\/p>\n<p>systemctl restart httpd<\/p>\n<p>or in CentOS 6 or below<\/p>\n<p>service httpd restart<\/p>\n<p>You should now see the module loaded by doing the following command<\/p>\n<p># httpd -M 2&gt;&amp;1|grep rewrite<br \/>\nrewrite_module (shared)<\/p>\n<p>That is for enabling the module. Mod_rewrite rules can either be inserted directly into the VirtualHost block for a specific domain or in a .htaccess for that given domain.<\/p>\n<h2>Mod_rewrite Examples<\/h2>\n<p>Rewrite domain.com to www.domain.com<\/p>\n<p>RewriteEngine On<br \/>\nRewriteCond % !^www. [NC]<br \/>\nRewriteRule ^(.*)$ http:\/\/www.%% [R=301,L]<\/p>\n<p>The above redirect will take all requests to the non-www domain and redirect them with a 301 code to the www.domain.com url and appendedthe rest of the url to it.<\/p>\n<p>Redirect all requests to https \/ SSL<\/p>\n<p>RewriteEngine On<br \/>\nRewriteCond % ^domain.com [NC]<br \/>\nRewriteCond % off<br \/>\nRewriteRule ^(.*)$ https:\/\/%%<\/p>\n<p>The above redirect will take all non-ssl requests and redirect them to https:\/\/ URLs.<\/p>\n<p>Redirect request from one directory to another<\/p>\n<p>RewriteRule ^subdirectory\/(.*)$ \/anotherdirectory\/$1 [R=301,NC,L]<\/p>\n<p>The above redirect will take any requests towards a single directory and redirect it to another directory with the rest of URL appended to it.<\/p>\n<p>Redirect one domain to another<\/p>\n<p>RewriteEngine On<br \/>\nRewriteCond % ^olddomain.com [NC,OR]<br \/>\nRewriteCond % ^www.olddomain.com [NC]<br \/>\nRewriteRule ^(.*)$ http:\/\/newdomain.com\/$1 [L,R=301,NC]<\/p>\n<p>This will redirect any requests with the destination of the olddomain and change them to the new domain. There are numerous redirects you can perform with mod_rewrite these are just a couple of common examples.<\/p>\n<p>Sep 5, 2017LinuxAdmin.io<\/p>\n<p><a href=\"https:\/\/linuxadmin.io\/mod_rewrite-apache-centos\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>mod_rewrite is a Apache module installed on linux servers to manipulate URLs submitted in the browser to perform other functions than it appears. Mod_rewrite can improve SEO appearing to give it a static appearance. This guide assumes you already have Apache installed, if you do not please see How to Install Apache Enable mod_rewrite You &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/18\/how-to-setup-mod_rewrite-in-apache\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How To Setup mod_rewrite In Apache&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-849","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/849","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=849"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/849\/revisions"}],"predecessor-version":[{"id":1029,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/849\/revisions\/1029"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=849"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=849"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=849"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}