Categories: htaccess, PHP3.3 min read

To format a PHP URL without the .php extension, you can use a URL rewriting engine such as Apache’s mod_rewrite. This allows you to specify custom rules for rewriting URLs, so that users can access your PHP pages without needing to include the .php extension in the URL.

Here is an example of how you can use mod_rewrite to remove the .php extension from your URLs:

Enable mod_rewrite

First, enable mod_rewrite in your Apache configuration file by adding the following line:

LoadModule rewrite_module modules/mod_rewrite.so

What is Mod_rewrite

Mod_rewrite is an Apache module that allows you to rewrite URLs in a clean and user-friendly way. It provides a flexible and powerful way to manage URLs, making it easier for users to access your content and for search engines to index your pages.

mod_rewrite uses a set of rules known as rewrite rules to determine which URLs should be rewritten and how they should be rewritten. These rules are specified in a .htaccess file that is placed in the root directory of your website.

For example, you can use mod_rewrite to convert a URL with a query string, such as example.com/page.php?id=123, into a clean and readable URL, such as example.com/page/123. This makes it easier for users to understand and remember the URL, and can also improve the search engine ranking of your pages.

In addition to rewriting URLs, mod_rewrite can also be used to redirect users to different pages, block access to certain pages, and perform other types of URL manipulations. It is a powerful and flexible tool that can help you improve the user experience and SEO of your website.

Create a .htaccess file

Next, create a .htaccess file in the root directory of your PHP website and add the following rules:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

These rules tell Apache to rewrite any URL that does not correspond to an existing directory and ends with .php to remove the .php extension. For example, a request to example.com/page.php would be rewritten as example.com/page.

Keep in mind that this approach only works for URLs that are accessed directly. If your PHP pages are linked to from other pages that include the .php extension, users will still see the extension in the URL when they click on the link. To avoid this, you can use a relative URL without the .php extension in your links.

What is a .Htaccess File

An .htaccess file is a configuration file that is used by the Apache web server to control the behavior of your website. The .htaccess file is placed in the root directory of your website, and it can be used to specify a wide range of settings and options that affect how your website behaves.

One common use of the .htaccess file is to enable or disable certain features of the Apache web server, such as the mod_rewrite module which is used for URL rewriting. You can also use the .htaccess file to set custom error pages, restrict access to certain pages, or control the behavior of other Apache modules.

In addition to the settings and options that you can specify in the .htaccess file, you can also include rewrite rules that are used by the mod_rewrite module to rewrite URLs in a clean and user-friendly way. For example, you can use rewrite rules to remove the .php extension from your URLs, or to convert a URL with a query string into a more readable URL.

Overall, the .htaccess file is a powerful and flexible tool that can help you customize and control the behavior of your Apache web server and your website. It is an essential part of the Apache web server, and it is worth learning how to use it to get the most out of your web server.

Blazing fast CDN - keycdn
Up to 75% off Hosting - Hostinger
Namecheap coupon promo code