Apache's Articles

Apache is open-source and most widely used web server in the world. It provides dynamic modules, and powerful support to website.

How to Redirect to Different URLs with .htaccess file
Apache is the first layer of your website security. With Apache cinfiguration, you can do so many stuff like blocking specific request, limit request from specific server, block automatic crawlers, redirect to specific website or urls etc. Below are few examples how you can redirect with Apache web server to improve better user experience. .htaccess file is the Apache configuration file where you can change settings. So put the below lines to change the apache settings. Redirect all routes to specific URL Put the below line to .htaccess to redirect all routes to specific routes, e.g.  RewriteEngine  on Redirect 301 / Redirect only specific portion of site If you only want to redirect specific portion of sites, add below lines. Redirect "/admin/" "" Redirecting a single URL If you want to redirect only single URL to new page. Redirect /path/to/old/url Redirect from directory to an HTML file If you want to add .html extension in URL, here is it RedirectMatch 301 ^/about Redirecting error messages If you want to redirect to specific pages in case of 404 error page, ErrorDocument 404 Forcefully redirect to HTTPS RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] Forcefully add www in the URL If you forcefully want to add WWW in the URL. RewriteCond %{HTTP_HOST} ^ RewriteRule (.*)$1 [R=301,L] Remove WWW from the URL RewriteCond %{HTTP_HOST} ^ RewriteRule (.*)$1 [R=301,L] I hope it will help you.
How to remove .php file extension from URL in apache
You may have seen some .php and .asp extension in url link. These extensions sometimes disclose the backend technology of website. You may want to hide these file extensions for SEO friendly url or you may want to change backend technology in fuure, and at that time it doen't affect your urls if you have already removed file extension. In this article, I will show you how you can remove specific file extension from url using apache. Your project must contains .htaccess file at the root of project directory with apache option. In this file, there is mod_rewrite directive. If there is no any file in the root directory, you need to create it. Remove .html extension from url RewriteEngine on RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC] RewriteRule ^ /%1 [NC,L,R] RewriteCond %{REQUEST_FILENAME}.html -f RewriteRule ^ %{REQUEST_URI}.html [NC,L] In the above code, first we need to on RewriteEngine. Then we have added new url with html removed. We have also redirect .html url to non .html urls. This will prevent search engines to generate duplicate links of the same url. Now if your site link is and when you try to access it, it will be 301 redirect to url. Removing .php extension from URL .php extension is used for php web application. Same way you can remove .php extension from the url. Any url with .php will redirect to non .php url. For example, if you visit website it will redirect to RewriteEngine on RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC] RewriteRule ^ /%1 [NC,L,R] RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^ %{REQUEST_URI}.php [NC,L] Add trailing slash(/) .htaccess Sometimes you may want to add trailing slash at the end of link. Here is how you can add it. So if you go to it will be redirect to RewriteCond %{REQUEST_URI} !(/$|\.)  RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]  I hope you liked the article and help in your web development.