{"id":14186,"date":"2023-05-09T18:47:11","date_gmt":"2023-05-09T18:47:11","guid":{"rendered":"https:\/\/webhostinggeeks.com\/howto\/?p=14186"},"modified":"2023-06-24T15:32:52","modified_gmt":"2023-06-24T15:32:52","slug":"how-to-enable-mod_rewrite-apache-module-on-centos","status":"publish","type":"post","link":"https:\/\/webhostinggeeks.com\/howto\/how-to-enable-mod_rewrite-apache-module-on-centos\/","title":{"rendered":"How to Enable mod_rewrite Apache module on CentOS"},"content":{"rendered":"<p><img decoding=\"async\" data-src=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/05\/How-to-Enable-mod_rewrite-Apache-module-on-CentOS-1024x768.jpg\" alt=\"How to Enable mod_rewrite Apache module on CentOS\" width=\"1024\" height=\"768\" class=\"alignnone size-large wp-image-16096 lazyload\" data-srcset=\"https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/05\/How-to-Enable-mod_rewrite-Apache-module-on-CentOS-1024x768.jpg 1024w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/05\/How-to-Enable-mod_rewrite-Apache-module-on-CentOS-300x225.jpg 300w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/05\/How-to-Enable-mod_rewrite-Apache-module-on-CentOS-1536x1152.jpg 1536w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/05\/How-to-Enable-mod_rewrite-Apache-module-on-CentOS-2048x1536.jpg 2048w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/05\/How-to-Enable-mod_rewrite-Apache-module-on-CentOS-128x96.jpg 128w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/05\/How-to-Enable-mod_rewrite-Apache-module-on-CentOS-420x315.jpg 420w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/05\/How-to-Enable-mod_rewrite-Apache-module-on-CentOS-540x405.jpg 540w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/05\/How-to-Enable-mod_rewrite-Apache-module-on-CentOS-720x540.jpg 720w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/05\/How-to-Enable-mod_rewrite-Apache-module-on-CentOS-960x720.jpg 960w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/05\/How-to-Enable-mod_rewrite-Apache-module-on-CentOS-1140x855.jpg 1140w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/05\/How-to-Enable-mod_rewrite-Apache-module-on-CentOS-1320x990.jpg 1320w, https:\/\/webhostinggeeks.com\/howto\/wp-content\/uploads\/2023\/05\/How-to-Enable-mod_rewrite-Apache-module-on-CentOS-1440x1080.jpg 1440w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/768;\" \/><\/p>\n<p>The mod_rewrite module is an integral part of the <a href=\"https:\/\/webhostinggeeks.com\/blog\/apache-http-server-explained\/\">Apache HTTP server<\/a>, offering a powerful and flexible way to manipulate URLs. It provides a robust and reliable solution for a wide range of URL-related tasks, such as URL redirection and URL shortening.<\/p>\n<p>This tutorial will guide you through the process of enabling the mod_rewrite module on a CentOS system.<\/p>\n<h2>Step 1: Open the Terminal<\/h2>\n<p>The first step is to open the terminal on your CentOS machine. You can do this by clicking on the terminal icon or by using the keyboard shortcut &#8220;Ctrl + Alt + T&#8221;.<\/p>\n<h2>Step 2: Check if mod_rewrite is Already Enabled<\/h2>\n<p>CentOS 7 and later versions come with mod_rewrite installed and enabled by default. To check if mod_rewrite is enabled on your system, open the terminal and run the following command:<\/p>\n<pre>\r\nsudo httpd -M | grep rewrite\r\n<\/pre>\n<p>If mod_rewrite is already enabled, you will see an output similar to the following:<\/p>\n<pre>\r\nrewrite_module (shared)\r\n<\/pre>\n<p>If you do not see any output or if mod_rewrite is not listed, it means that it is not enabled.<\/p>\n<h2>Step 3: Enable mod_rewrite<\/h2>\n<p>To enable mod_rewrite, you need to edit the Apache configuration file. You can do this by using the following command to open the file in a text editor:<\/p>\n<pre>\r\nsudo vi \/etc\/httpd\/conf\/httpd.conf\r\n<\/pre>\n<p>In the file, look for the following line:<\/p>\n<pre>\r\n#LoadModule rewrite_module modules\/mod_rewrite.so\r\n<\/pre>\n<p>Remove the &#8220;#&#8221; at the beginning of the line to uncomment it:<\/p>\n<pre>\r\nLoadModule rewrite_module modules\/mod_rewrite.so\r\n<\/pre>\n<p>Save the file and exit the text editor.<\/p>\n<h2>Step 4: Create .htaccess File in Apache<\/h2>\n<p>If you want, you can also create a .htaccess file to set up URL rewrite rules. Open a new .htaccess file with the following command:<\/p>\n<pre>sudo vi \/var\/www\/html\/.htaccess<\/pre>\n<p>At the top of the file, add the following line to enable the rewrite engine:<\/p>\n<pre>RewriteEngine On<\/pre>\n<p>Below this line, you can add URL rewrite rules. For example, if you want users to be able to access the \/about page on your website without typing the .html extension (\/about.html), you can add the following rule:<\/p>\n<pre>RewriteRule ^about$ about.html [NC]<\/pre>\n<p>Save and exit the .htaccess file, then restart the Apache server.<\/p>\n<h2>Step 5: Restart Apache<\/h2>\n<p>After enabling mod_rewrite, you need to restart Apache for the changes to take effect. You can do this by using the following command:<\/p>\n<pre>\r\nsudo service httpd restart\r\n<\/pre>\n<h2>Step 6: Verify the Installation<\/h2>\n<p>To verify that mod_rewrite is enabled, you can create a test file with some rewrite rules and test it in your web browser. For example, you can create a file named &#8220;test.php&#8221; with the following content:<\/p>\n<pre>\r\n&lt;?php\r\necho \"Hello, World!\";\r\n?&gt;\r\n<\/pre>\n<p>Then, create a file named &#8220;.htaccess&#8221; in the same directory with the following content:<\/p>\n<pre>\r\nRewriteEngine On\r\nRewriteRule ^hello$ test.php\r\n<\/pre>\n<p>This will rewrite the URL &#8220;http:\/\/yourdomain.com\/hello&#8221; to &#8220;http:\/\/yourdomain.com\/test.php&#8221;.<\/p>\n<p>Save the file and access &#8220;http:\/\/yourdomain.com\/hello&#8221; in your web browser. If mod_rewrite is enabled, you should see the message &#8220;Hello, World!&#8221;.<\/p>\n<h2>Commands Mentioned:<\/h2>\n<ul>\n<li><span class=\"fw-bold\">sudo<\/span> &#8211; a command that allows users to run programs with the security privileges of another user, typically the superuser.<\/li>\n<li><span class=\"fw-bold\">httpd<\/span> &#8211; the Apache web server daemon.<\/li>\n<li><span class=\"fw-bold\">vi<\/span> &#8211; a text editor for Unix-like systems.<\/li>\n<li><span class=\"fw-bold\">service<\/span> &#8211; a command to control system services.<\/li>\n<\/ul>\n<h2>Conclusion:<\/h2>\n<p>In this guide, we have gone through the steps to enable mod_rewrite on CentOS. By enabling mod_rewrite, you can use URL rewriting to modify URLs before the request is processed by the server. If you have any questions or suggestions, feel free to comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The mod_rewrite module is an integral part of the Apache HTTP server, offering a powerful and flexible way to manipulate URLs. It provides a robust and reliable solution for a&#8230;<\/p>\n","protected":false},"author":6,"featured_media":16096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wds_primary_category":0,"footnotes":""},"categories":[1103,2055,996],"tags":[1199,2079,1244,2053,1914],"class_list":["post-14186","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-apache","category-centos","category-linux","tag-apache","tag-apache-module","tag-centos","tag-mod_rewrite","tag-web-server"],"_links":{"self":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/14186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/comments?post=14186"}],"version-history":[{"count":0,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/14186\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media\/16096"}],"wp:attachment":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media?parent=14186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/categories?post=14186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/tags?post=14186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}