{"id":175,"date":"2020-03-28T22:00:00","date_gmt":"2020-03-28T22:00:00","guid":{"rendered":"https:\/\/rishikantsri.in\/blog\/?p=175"},"modified":"2023-09-21T09:59:10","modified_gmt":"2023-09-21T09:59:10","slug":"understanding-htaccess-files-a-comprehensive-guide-with-practical-examples","status":"publish","type":"post","link":"https:\/\/rishikantsri.in\/blog\/understanding-htaccess-files-a-comprehensive-guide-with-practical-examples\/","title":{"rendered":"Understanding .htaccess Files: A Comprehensive Guide with Practical Examples"},"content":{"rendered":"\n<p>When it comes to managing web server configuration and enhancing website functionality, the <code>.htaccess<\/code> file is a powerful tool used by webmasters and developers. This file, which stands for &#8220;Hypertext Access,&#8221; is specific to websites hosted on Apache web servers and allows you to make directory-level configurations and apply various rules. In this comprehensive guide, we&#8217;ll explore the <code>.htaccess<\/code> file in detail, providing explanations and practical examples of its usage.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is <code>.htaccess<\/code>?<\/h2>\n\n\n\n<p><code>.htaccess<\/code> is a configuration file used by the Apache web server to modify server behavior on a per-directory basis. It provides a way to override the server&#8217;s global configuration settings for specific directories and their subdirectories. This flexibility makes it a valuable resource for webmasters and developers who need to customize their websites&#8217; functionality and security.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating and Locating <code>.htaccess<\/code> Files<\/h2>\n\n\n\n<p>To create an <code>.htaccess<\/code> file, you can use a text editor like Notepad or Visual Studio Code. Simply save the file with the name <code>.htaccess<\/code>. You can place this file in the root directory of your website, or in specific subdirectories where you want to apply custom configurations. Ensure that the filename starts with a dot (<code>.<\/code>), and make sure it&#8217;s not saved with an additional extension like <code>.txt<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practical Examples of <code>.htaccess<\/code> Usage<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. URL Rewriting<\/h3>\n\n\n\n<p>URL rewriting is a common use case for <code>.htaccess<\/code>. It allows you to create user-friendly and SEO-friendly URLs by mapping them to internal server paths.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:9.899999618530273px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440\"><span style=\"background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440\">Apache<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"RewriteEngine On\nRewriteRule ^blog\/([a-zA-Z0-9-]+)$ blog.php?slug=$1 [L]\n\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">RewriteEngine<\/span><span style=\"color: #D8DEE9FF\"> On<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteRule<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">^blog\/([a-zA-Z0-9-]+)$<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">blog.php?slug=$1<\/span><span style=\"color: #D8DEE9FF\"> [L]<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>In this example, URLs like <code>example.com\/blog\/my-awesome-post<\/code> will be internally rewritten to <code>example.com\/blog.php?slug=my-awesome-post<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Password Protection<\/h3>\n\n\n\n<p>You can use <code>.htaccess<\/code> to password protect directories, restricting access to authorized users. Create a <code>.htpasswd<\/code> file to store usernames and passwords.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:9.899993896484375px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440\"><span style=\"background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440\">Apache<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"AuthType Basic\nAuthName &quot;Protected Area&quot;\nAuthUserFile \/path\/to\/.htpasswd\nRequire valid-user\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">AuthType<\/span><span style=\"color: #D8DEE9FF\"> Basic<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">AuthName<\/span><span style=\"color: #D8DEE9FF\"> &quot;Protected Area&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">AuthUserFile<\/span><span style=\"color: #D8DEE9FF\"> \/path\/to\/.htpasswd<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">Require<\/span><span style=\"color: #D8DEE9FF\"> valid-user<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Users will be prompted to enter a username and password when accessing the protected directory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Custom Error Pages<\/h3>\n\n\n\n<p>Enhance the user experience by defining custom error pages for common HTTP status codes.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:9.9000244140625px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440\"><span style=\"background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440\">Apache<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"ErrorDocument 404 \/errors\/404.html\nErrorDocument 500 \/errors\/500.html\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">ErrorDocument<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">404<\/span><span style=\"color: #D8DEE9FF\"> \/errors\/<\/span><span style=\"color: #B48EAD\">404<\/span><span style=\"color: #D8DEE9FF\">.html<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">ErrorDocument<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #B48EAD\">500<\/span><span style=\"color: #D8DEE9FF\"> \/errors\/<\/span><span style=\"color: #B48EAD\">500<\/span><span style=\"color: #D8DEE9FF\">.html<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>These directives specify custom error pages for &#8220;Not Found&#8221; (404) and &#8220;Internal Server Error&#8221; (500) status codes. The error pages should be located in the <code>\/errors\/<\/code> directory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Access Control<\/h3>\n\n\n\n<p>Control access to specific files or directories based on IP addresses.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:9.900009155273438px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440\"><span style=\"background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440\">Apache<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"Order Deny,Allow\nDeny from all\nAllow from 192.168.1.100\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">Order<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">Deny<\/span><span style=\"color: #D8DEE9FF\">,<\/span><span style=\"color: #81A1C1\">Allow<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">Deny<\/span><span style=\"color: #D8DEE9FF\"> from all<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">Allow<\/span><span style=\"color: #D8DEE9FF\"> from <\/span><span style=\"color: #B48EAD\">192<\/span><span style=\"color: #D8DEE9FF\">.<\/span><span style=\"color: #B48EAD\">168<\/span><span style=\"color: #D8DEE9FF\">.<\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #D8DEE9FF\">.<\/span><span style=\"color: #B48EAD\">100<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This example allows access to a directory only from the IP address 192.168.1.100. You can specify multiple IP addresses or IP ranges as needed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. Blocking Hotlinking<\/h3>\n\n\n\n<p>Prevent other websites from hotlinking your images by serving them directly from your site.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:9.899999618530273px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440\"><span style=\"background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440\">Apache<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"RewriteEngine On\nRewriteCond %{HTTP_REFERER} !^$\nRewriteCond %{HTTP_REFERER} !^http(s)?:\/\/(www\\.)?example.com [NC]\nRewriteRule \\.(jpg|jpeg|png|gif)$ - [NC,F,L]\n\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">RewriteEngine<\/span><span style=\"color: #D8DEE9FF\"> On<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteCond<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">%{HTTP_REFERER}<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">!^$<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteCond<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">%{HTTP_REFERER}<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">!^http(s)?:\/\/(www\\.)?example.com<\/span><span style=\"color: #D8DEE9FF\"> [NC]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteRule<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">\\.(jpg|jpeg|png|gif)$<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">-<\/span><span style=\"color: #D8DEE9FF\"> [NC,F,L]<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>These rules block hotlinking of image files (JPEG, PNG, GIF) from external websites, allowing access only if the request comes from your domain.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">6. Cache Control<\/h3>\n\n\n\n<p>Optimize website performance by instructing browsers and proxies to cache certain files.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:9.899999618530273px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440\"><span style=\"background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440\">Apache<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"&lt;FilesMatch &quot;\\.(jpg|jpeg|png|gif|css|js)$&quot;&gt;\n    Header set Cache-Control &quot;max-age=604800, public&quot;\n&lt;\/FilesMatch&gt;\n\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">&lt;<\/span><span style=\"color: #D8DEE9FF\">FilesMatch <\/span><span style=\"color: #A3BE8C\">&quot;\\.(jpg|jpeg|png|gif|css|js)$&quot;<\/span><span style=\"color: #81A1C1\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">Header<\/span><span style=\"color: #D8DEE9FF\"> set Cache-Control &quot;max-age=<\/span><span style=\"color: #B48EAD\">604800<\/span><span style=\"color: #D8DEE9FF\">, public&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">&lt;\/<\/span><span style=\"color: #D8DEE9FF\">FilesMatch<\/span><span style=\"color: #81A1C1\">&gt;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This code sets caching headers for image files, CSS, and JavaScript files, indicating that they should be cached for one week (604800 seconds) for public access.<\/p>\n\n\n\n<p>The <code>.htaccess<\/code> file is a versatile tool for configuring and customizing Apache web server behavior. It empowers webmasters and developers to manage URL rewriting, enhance security, create custom error pages, control access, block hotlinking, and improve website performance. When using <code>.htaccess<\/code>, it&#8217;s essential to test changes carefully and follow best practices to ensure a smooth and secure web experience for your users.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A standard example of an <code>.htaccess<\/code> file along with explanations for each directive:<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:19.80000114440918px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440\"><span style=\"background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440\">Apache<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"# Enable the RewriteEngine\nRewriteEngine On\n\n# Redirect requests to HTTPS\nRewriteCond %{HTTPS} off\nRewriteRule ^ https:\/\/%{HTTP_HOST}%{REQUEST_URI} [L,R=301]\n\n# Remove the www prefix from the URL\nRewriteCond %{HTTP_HOST} ^www\\.example\\.com$ [NC]\nRewriteRule ^(.*)$ https:\/\/example.com\/$1 [R=301,L]\n\n# Set the default index page to index.php\nDirectoryIndex index.php\n\n# Deny access to specific file types\n&lt;FilesMatch &quot;\\.config$|\\.log$&quot;&gt;\n    Order allow,deny\n    Deny from all\n&lt;\/FilesMatch&gt;\n\n# Enable GZIP compression for certain file types\n&lt;IfModule mod_deflate.c&gt;\n    AddOutputFilterByType DEFLATE text\/plain\n    AddOutputFilterByType DEFLATE text\/html\n    AddOutputFilterByType DEFLATE text\/xml\n    AddOutputFilterByType DEFLATE text\/css\n    AddOutputFilterByType DEFLATE application\/xml\n    AddOutputFilterByType DEFLATE application\/xhtml+xml\n    AddOutputFilterByType DEFLATE application\/rss+xml\n    AddOutputFilterByType DEFLATE application\/javascript\n    AddOutputFilterByType DEFLATE application\/x-javascript\n&lt;\/IfModule&gt;\n\n# Enable browser caching for images\n&lt;IfModule mod_expires.c&gt;\n    ExpiresActive On\n    ExpiresByType image\/jpeg &quot;access plus 1 year&quot;\n    ExpiresByType image\/png &quot;access plus 1 year&quot;\n    ExpiresByType image\/gif &quot;access plus 1 year&quot;\n&lt;\/IfModule&gt;\n\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #616E88\"># Enable the RewriteEngine<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteEngine<\/span><span style=\"color: #D8DEE9FF\"> On<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Redirect requests to HTTPS<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteCond<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">%{HTTPS}<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">off<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteRule<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">^<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">https:\/\/%{HTTP_HOST}%{REQUEST_URI}<\/span><span style=\"color: #D8DEE9FF\"> [L,R=<\/span><span style=\"color: #B48EAD\">301<\/span><span style=\"color: #D8DEE9FF\">]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Remove the www prefix from the URL<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteCond<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">%{HTTP_HOST}<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">^www\\.example\\.com$<\/span><span style=\"color: #D8DEE9FF\"> [NC]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteRule<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">^(.*)$<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">https:\/\/example.com\/$1<\/span><span style=\"color: #D8DEE9FF\"> [R=<\/span><span style=\"color: #B48EAD\">301<\/span><span style=\"color: #D8DEE9FF\">,L]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Set the default index page to index.php<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">DirectoryIndex<\/span><span style=\"color: #D8DEE9FF\"> index.php<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Deny access to specific file types<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">&lt;<\/span><span style=\"color: #D8DEE9FF\">FilesMatch <\/span><span style=\"color: #A3BE8C\">&quot;\\.config$|\\.log$&quot;<\/span><span style=\"color: #81A1C1\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">Order<\/span><span style=\"color: #D8DEE9FF\"> allow,deny<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">Deny<\/span><span style=\"color: #D8DEE9FF\"> from all<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">&lt;\/<\/span><span style=\"color: #D8DEE9FF\">FilesMatch<\/span><span style=\"color: #81A1C1\">&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Enable GZIP compression for certain file types<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">&lt;<\/span><span style=\"color: #D8DEE9FF\">IfModule <\/span><span style=\"color: #A3BE8C\">mod_deflate.c<\/span><span style=\"color: #81A1C1\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">AddOutputFilterByType<\/span><span style=\"color: #D8DEE9FF\"> DEFLATE text\/plain<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">AddOutputFilterByType<\/span><span style=\"color: #D8DEE9FF\"> DEFLATE text\/html<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">AddOutputFilterByType<\/span><span style=\"color: #D8DEE9FF\"> DEFLATE text\/xml<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">AddOutputFilterByType<\/span><span style=\"color: #D8DEE9FF\"> DEFLATE text\/css<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">AddOutputFilterByType<\/span><span style=\"color: #D8DEE9FF\"> DEFLATE application\/xml<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">AddOutputFilterByType<\/span><span style=\"color: #D8DEE9FF\"> DEFLATE application\/xhtml+xml<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">AddOutputFilterByType<\/span><span style=\"color: #D8DEE9FF\"> DEFLATE application\/rss+xml<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">AddOutputFilterByType<\/span><span style=\"color: #D8DEE9FF\"> DEFLATE application\/javascript<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">AddOutputFilterByType<\/span><span style=\"color: #D8DEE9FF\"> DEFLATE application\/x-javascript<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">&lt;\/<\/span><span style=\"color: #D8DEE9FF\">IfModule<\/span><span style=\"color: #81A1C1\">&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #616E88\"># Enable browser caching for images<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">&lt;<\/span><span style=\"color: #D8DEE9FF\">IfModule <\/span><span style=\"color: #A3BE8C\">mod_expires.c<\/span><span style=\"color: #81A1C1\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">ExpiresActive<\/span><span style=\"color: #D8DEE9FF\"> On<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">ExpiresByType<\/span><span style=\"color: #D8DEE9FF\"> image\/jpeg &quot;access plus <\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #D8DEE9FF\"> year&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">ExpiresByType<\/span><span style=\"color: #D8DEE9FF\"> image\/png &quot;access plus <\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #D8DEE9FF\"> year&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D8DEE9FF\">    <\/span><span style=\"color: #81A1C1\">ExpiresByType<\/span><span style=\"color: #D8DEE9FF\"> image\/gif &quot;access plus <\/span><span style=\"color: #B48EAD\">1<\/span><span style=\"color: #D8DEE9FF\"> year&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">&lt;\/<\/span><span style=\"color: #D8DEE9FF\">IfModule<\/span><span style=\"color: #81A1C1\">&gt;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Explanation of Each Directive<\/strong>:<\/p>\n\n\n\n<ol>\n<li><code>RewriteEngine On<\/code>: This directive enables the Apache <code>mod_rewrite<\/code> module, allowing URL rewriting.<\/li>\n\n\n\n<li>Redirect to HTTPS:\n<ul>\n<li><code>RewriteCond %{HTTPS} off<\/code>: This condition checks if the request is not using HTTPS.<\/li>\n\n\n\n<li><code>RewriteRule ^ https:\/\/%{HTTP_HOST}%{REQUEST_URI} [L,R=301]<\/code>: If the condition is met, this rule redirects the request to the HTTPS version of the same URL with a permanent (301) redirect.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Remove www Prefix:\n<ul>\n<li><code>RewriteCond %{HTTP_HOST} ^www\\.example\\.com$ [NC]<\/code>: This condition checks if the request includes the <code>www<\/code> prefix.<\/li>\n\n\n\n<li><code>RewriteRule ^(.*)$ https:\/\/example.com\/$1 [R=301,L]<\/code>: If the condition is met, this rule removes the <code>www<\/code> prefix and redirects to the non-www version of the URL with a permanent redirect.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><code>DirectoryIndex index.php<\/code>: This directive sets the default index file to <code>index.php<\/code>. If a directory is accessed, Apache will look for and serve <code>index.php<\/code> if it exists.<\/li>\n\n\n\n<li>Deny Access to Specific File Types:\n<ul>\n<li><code>&lt;FilesMatch \"\\.config$|\\.log$\"&gt;<\/code>: This block applies rules to files with extensions <code>.config<\/code> or <code>.log<\/code>.<\/li>\n\n\n\n<li><code>Order allow,deny<\/code> and <code>Deny from all<\/code>: These lines deny access to files matching the specified extensions.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Enable GZIP Compression:\n<ul>\n<li><code>&lt;IfModule mod_deflate.c&gt;<\/code>: This block checks if the <code>mod_deflate<\/code> module is available.<\/li>\n\n\n\n<li>The subsequent lines specify which file types should be compressed with GZIP compression. It improves page load times by reducing file sizes during transmission.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Enable Browser Caching for Images:\n<ul>\n<li><code>&lt;IfModule mod_expires.c&gt;<\/code>: This block checks if the <code>mod_expires<\/code> module is available.<\/li>\n\n\n\n<li>The <code>ExpiresByType<\/code> lines specify how long (in this case, 1 year) certain image types should be cached in the user&#8217;s browser. This reduces the need for re-downloading images on subsequent visits.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>Please note that you should adapt and test these directives according to your specific server configuration and website needs. Additionally, always make backups before modifying <code>.htaccess<\/code> files, and ensure your server supports the necessary modules (e.g., <code>mod_rewrite<\/code>, <code>mod_deflate<\/code>, <code>mod_expires<\/code>) for these directives to work correctly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to enable : mod_rewrite, mod_deflate, mod_expires<\/h2>\n\n\n\n<p>To enable the Apache modules <code>mod_rewrite<\/code>, <code>mod_deflate<\/code>, and <code>mod_expires<\/code>, you&#8217;ll need to use the Apache server&#8217;s configuration files. The process can vary slightly depending on your operating system and Apache version, but here are the general steps:<\/p>\n\n\n\n<p><strong>1. Open Terminal or Command Prompt:<\/strong><\/p>\n\n\n\n<ul>\n<li>On Linux\/Unix-based systems, you may need root or superuser privileges to edit Apache&#8217;s configuration files. You can use the <code>sudo<\/code> command to gain those privileges.<\/li>\n\n\n\n<li>On Windows, open Command Prompt as an administrator.<\/li>\n<\/ul>\n\n\n\n<p><strong>2. Locate the Apache Configuration Directory:<\/strong><\/p>\n\n\n\n<ul>\n<li>On Linux\/Unix-based systems, Apache configuration files are usually stored in the <code>\/etc\/apache2\/<\/code> directory. The main configuration file is often named <code>httpd.conf<\/code>, <code>apache2.conf<\/code>, or <code>httpd.conf<\/code>.<\/li>\n\n\n\n<li>On Windows, you&#8217;ll typically find the configuration files in the <code>conf<\/code> directory of your Apache installation, like <code>C:\\Program Files\\Apache Group\\Apache2\\conf<\/code>.<\/li>\n<\/ul>\n\n\n\n<p><strong>3. Edit the Apache Configuration File:<\/strong><\/p>\n\n\n\n<ul>\n<li>Open the Apache configuration file in a text editor. You may need administrative privileges to do this.<\/li>\n<\/ul>\n\n\n\n<p><strong>On Linux\/Unix-based systems<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:9.899999618530273px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440\"><span style=\"background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440\">Apache<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"sudo nano \/etc\/apache2\/apache2.conf\n\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">sudo nano \/etc\/apache2\/apache2.conf<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>On Windows<\/strong>: You can use a text editor like Notepad to open the file. Right-click on the editor and select &#8220;Run as administrator&#8221; if needed.<\/p>\n\n\n\n<p><strong>4. Enable the Modules:<\/strong><\/p>\n\n\n\n<ul>\n<li>Search for the lines that start with <code>LoadModule<\/code> in the configuration file. You&#8217;ll need to uncomment (remove the <code>#<\/code> symbol) or add lines for the modules you want to enable.<\/li>\n<\/ul>\n\n\n\n<p>For <code>mod_rewrite<\/code>, you should find a line like:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:9.899993896484375px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440\"><span style=\"background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440\">Apache<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"LoadModule rewrite_module modules\/mod_rewrite.so\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">LoadModule<\/span><span style=\"color: #D8DEE9FF\"> rewrite_module modules\/mod_rewrite.so<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>For <code>mod_deflate<\/code>, you should find a line like:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:9.899993896484375px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440\"><span style=\"background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440\">Apache<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"LoadModule deflate_module modules\/mod_deflate.so\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">LoadModule<\/span><span style=\"color: #D8DEE9FF\"> deflate_module modules\/mod_deflate.so<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>For <code>mod_expires<\/code>, you should find a line like:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:9.899993896484375px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440\"><span style=\"background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440\">Apache<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"LoadModule expires_module modules\/mod_expires.so\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">LoadModule<\/span><span style=\"color: #D8DEE9FF\"> expires_module modules\/mod_expires.so<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>5. Save and Exit:<\/strong><\/p>\n\n\n\n<ul>\n<li>After making the necessary changes, save the configuration file and exit the text editor.<\/li>\n<\/ul>\n\n\n\n<p><strong>6. Restart Apache:<\/strong><\/p>\n\n\n\n<ul>\n<li>To apply the changes, you need to restart the Apache web server. Use the following command:<\/li>\n<\/ul>\n\n\n\n<p><strong>On Linux\/Unix-based systems<\/strong>:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:9.899999618530273px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440\"><span style=\"background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440\">Apache<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"sudo systemctl restart apache2\n\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">sudo systemctl restart apache2<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>On Windows<\/strong>: You can use the Apache Service Monitor or restart Apache via the Windows Services interface.<\/p>\n\n\n\n<p><strong>7. Verify Module Activation:<\/strong><\/p>\n\n\n\n<ul>\n<li>To ensure that the modules are enabled, you can run the following command (on Linux\/Unix systems) to list the loaded modules:<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:9.900009155273438px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440\"><span style=\"background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440\">Apache<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"sudo apache2ctl -M\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D8DEE9FF\">sudo apache2ctl -M<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This command should display a list of loaded modules, including <code>rewrite_module<\/code>, <code>deflate_module<\/code>, and <code>expires_module<\/code>.<\/p>\n\n\n\n<p>After following these steps, the Apache modules <code>mod_rewrite<\/code>, <code>mod_deflate<\/code>, and <code>mod_expires<\/code> should be enabled and ready to use on your server. You can now configure your <code>.htaccess<\/code> files as needed to make use of these modules.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Other Examples<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:19.800003051757812px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440\"><span style=\"background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440\">Apache<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"&lt;IfModule mod_rewrite.c&gt;\n\nRewriteEngine On\n\nRewriteCond %{HTTP_HOST} ^www\\.(.*)$ [NC]\nRewriteRule ^(.*)$ https:\/\/%1\/$1 [R=301,L]\n\nRewriteCond %{HTTPS} !on\nRewriteRule (.*) https:\/\/%{HTTP_HOST}%{REQUEST_URI} [R=301,L]\n\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteCond %{REQUEST_FILENAME}\\.php -f\nRewriteRule ^(.*)$ $1.php [NC,L] \n\nRewriteRule ^([^\/.]+)$ $1.php [L]\nRewriteRule ^blog\/([^\/\\.]+)\/?$ blog.php?title=$1 [L]\nRewriteRule ^blog1\/([^\/\\.]+)\/?$ blog1.php?title=$1 [L]\n&lt;\/IfModule&gt;\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">&lt;<\/span><span style=\"color: #D8DEE9FF\">IfModule <\/span><span style=\"color: #A3BE8C\">mod_rewrite.c<\/span><span style=\"color: #81A1C1\">&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteEngine<\/span><span style=\"color: #D8DEE9FF\"> On<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteCond<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">%{HTTP_HOST}<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">^www\\.(.*)$<\/span><span style=\"color: #D8DEE9FF\"> [NC]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteRule<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">^(.*)$<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">https:\/\/%1\/$1<\/span><span style=\"color: #D8DEE9FF\"> [R=<\/span><span style=\"color: #B48EAD\">301<\/span><span style=\"color: #D8DEE9FF\">,L]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteCond<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">%{HTTPS}<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">!on<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteRule<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">(.*)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">https:\/\/%{HTTP_HOST}%{REQUEST_URI}<\/span><span style=\"color: #D8DEE9FF\"> [R=<\/span><span style=\"color: #B48EAD\">301<\/span><span style=\"color: #D8DEE9FF\">,L]<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteCond<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">%{REQUEST_FILENAME}<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">!-d<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteCond<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">%{REQUEST_FILENAME}\\.php<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">-f<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteRule<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">^(.*)$<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">$1.php<\/span><span style=\"color: #D8DEE9FF\"> [NC,L] <\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteRule<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">^([^\/.]+)$<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">$1.php<\/span><span style=\"color: #D8DEE9FF\"> [L]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteRule<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">^blog\/([^\/\\.]+)\/?$<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">blog.php?title=$1<\/span><span style=\"color: #D8DEE9FF\"> [L]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteRule<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">^blog1\/([^\/\\.]+)\/?$<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">blog1.php?title=$1<\/span><span style=\"color: #D8DEE9FF\"> [L]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">&lt;\/<\/span><span style=\"color: #D8DEE9FF\">IfModule<\/span><span style=\"color: #81A1C1\">&gt;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>I&#8217;ll explain the contents of <code>.htaccess<\/code> file step by step:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:9.899999618530273px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440\"><span style=\"background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440\">Apache<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"&lt;IfModule mod_rewrite.c&gt;\n\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">&lt;<\/span><span style=\"color: #D8DEE9FF\">IfModule <\/span><span style=\"color: #A3BE8C\">mod_rewrite.c<\/span><span style=\"color: #81A1C1\">&gt;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This line starts a conditional block that checks if the <code>mod_rewrite<\/code> module is available and enabled. The directives within this block will only be executed if the module is available.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:9.899999618530273px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440\"><span style=\"background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440\">Apache<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"RewriteEngine On\n\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">RewriteEngine<\/span><span style=\"color: #D8DEE9FF\"> On<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>This line enables the <code>mod_rewrite<\/code> engine, allowing you to use URL rewriting rules.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:9.899999618530273px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440\"><span style=\"background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440\">Apache<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"RewriteCond %{HTTP_HOST} ^www\\.(.*)$ [NC]\nRewriteRule ^(.*)$ https:\/\/%1\/$1 [R=301,L]\n\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">RewriteCond<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">%{HTTP_HOST}<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">^www\\.(.*)$<\/span><span style=\"color: #D8DEE9FF\"> [NC]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteRule<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">^(.*)$<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">https:\/\/%1\/$1<\/span><span style=\"color: #D8DEE9FF\"> [R=<\/span><span style=\"color: #B48EAD\">301<\/span><span style=\"color: #D8DEE9FF\">,L]<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>hese lines handle redirecting from <code>www<\/code> to the non-www version of your website and enforce the use of HTTPS:<\/p>\n\n\n\n<ul>\n<li><code>RewriteCond %{HTTP_HOST} ^www\\.(.*)$ [NC]<\/code> checks if the request&#8217;s <code>HTTP_HOST<\/code> starts with &#8220;<a href=\"http:\/\/www\/\">www<\/a>.&#8221;<\/li>\n\n\n\n<li><code>RewriteRule ^(.*)$ https:\/\/%1\/$1 [R=301,L]<\/code> redirects the request to the non-www version of the same URL using HTTPS (if not already) and issues a permanent (301) redirect.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:9.899999618530273px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440\"><span style=\"background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440\">Apache<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"RewriteCond %{HTTPS} !on\nRewriteRule (.*) https:\/\/%{HTTP_HOST}%{REQUEST_URI} [R=301,L]\n\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">RewriteCond<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">%{HTTPS}<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">!on<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteRule<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">(.*)<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">https:\/\/%{HTTP_HOST}%{REQUEST_URI}<\/span><span style=\"color: #D8DEE9FF\"> [R=<\/span><span style=\"color: #B48EAD\">301<\/span><span style=\"color: #D8DEE9FF\">,L]<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>These lines ensure that all requests are served over HTTPS:<\/p>\n\n\n\n<ul>\n<li><code>RewriteCond %{HTTPS} !on<\/code> checks if the request is not using HTTPS.<\/li>\n\n\n\n<li><code>RewriteRule (.*) https:\/\/%{HTTP_HOST}%{REQUEST_URI} [R=301,L]<\/code> redirects the request to the same URL using HTTPS and issues a permanent (301) redirect.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:9.899999618530273px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440\"><span style=\"background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440\">Apache<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"RewriteCond %{REQUEST_FILENAME} !-d\nRewriteCond %{REQUEST_FILENAME}\\.php -f\nRewriteRule ^(.*)$ $1.php [NC,L]\n\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">RewriteCond<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">%{REQUEST_FILENAME}<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">!-d<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteCond<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">%{REQUEST_FILENAME}\\.php<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">-f<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteRule<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">^(.*)$<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">$1.php<\/span><span style=\"color: #D8DEE9FF\"> [NC,L]<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>These lines perform URL rewriting for requests that don&#8217;t end with a slash:<\/p>\n\n\n\n<ul>\n<li><code>RewriteCond %{REQUEST_FILENAME} !-d<\/code> checks if the request is not for a directory.<\/li>\n\n\n\n<li><code>RewriteCond %{REQUEST_FILENAME}\\.php -f<\/code> checks if appending &#8220;.php&#8221; to the request&#8217;s filename results in an existing file.<\/li>\n\n\n\n<li><code>RewriteRule ^(.*)$ $1.php [NC,L]<\/code> rewrites the URL to append &#8220;.php&#8221; to the request if both conditions are met. This enables you to access PHP files without specifying the file extension in the URL.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1.125rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#d8dee9ff;--cbp-line-number-width:9.899999618530273px;line-height:1.625rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:16px 0 0 16px;width:100%;text-align:left;background-color:#2e3440\"><span style=\"background:#c8d0e0;padding:0.3rem 0.5rem 0.2rem;border-radius:1rem;font-size:0.8em;line-height:1;height:1.25rem;text-align:center;display:inline-flex;align-items:center;justify-content:center;color:#2e3440\">Apache<\/span><\/span><span role=\"button\" tabindex=\"0\" data-code=\"RewriteRule ^([^\/.]+)$ $1.php [L]\nRewriteRule ^blog\/([^\/\\.]+)\/?$ blog.php?title=$1 [L]\nRewriteRule ^blog1\/([^\/\\.]+)\/?$ blog1.php?title=$1 [L]\n\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">RewriteRule<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">^([^\/.]+)$<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">$1.php<\/span><span style=\"color: #D8DEE9FF\"> [L]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteRule<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">^blog\/([^\/\\.]+)\/?$<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">blog.php?title=$1<\/span><span style=\"color: #D8DEE9FF\"> [L]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">RewriteRule<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #EBCB8B\">^blog1\/([^\/\\.]+)\/?$<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #A3BE8C\">blog1.php?title=$1<\/span><span style=\"color: #D8DEE9FF\"> [L]<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p>These lines provide additional URL rewriting rules:<\/p>\n\n\n\n<ul>\n<li>The first rule <code>RewriteRule ^([^\/.]+)$ $1.php [L]<\/code> appears to be a generic rule that appends &#8220;.php&#8221; to URLs without slashes.<\/li>\n\n\n\n<li>The next two rules rewrite URLs in the form of <code>\/blog\/something<\/code> and <code>\/blog1\/something<\/code> to corresponding PHP files with the <code>title<\/code> parameter.<\/li>\n<\/ul>\n\n\n\n<p>Overall, this <code>.htaccess<\/code> file is configured to handle redirects from <code>www<\/code> to non-www, enforce HTTPS, and perform URL rewriting to simplify the URLs by appending &#8220;.php&#8221; or passing parameters to PHP files. It seems to be set up for clean and user-friendly URLs.<\/p>\n\n\n\n<p>Thanks! <\/p>\n\n\n\n<p>Happy Coding! \ud83d\ude42<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When it comes to managing web server configuration and enhancing website functionality, the .htaccess file is a powerful tool used by webmasters and developers. This file, which stands for &#8220;Hypertext Access,&#8221; is specific to websites hosted on Apache web servers and allows you to make directory-level configurations and apply various rules. In this comprehensive guide, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,13],"tags":[20],"_links":{"self":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts\/175"}],"collection":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/comments?post=175"}],"version-history":[{"count":1,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts\/175\/revisions"}],"predecessor-version":[{"id":176,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/posts\/175\/revisions\/176"}],"wp:attachment":[{"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/media?parent=175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/categories?post=175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rishikantsri.in\/blog\/wp-json\/wp\/v2\/tags?post=175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}