All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Remove PHP Extension From URL Without htaccess

Last Updated : Mar 11, 2024

How To Remove PHP Extension From URL Without htaccess

In this article we will show you the solution of how to remove PHP extension from URL without htaccess, when file extensions are removed from URLs, they will make the URLs more readable. While .php, .html extensions do not harm SEO, they will make the URLs more readable.

Users can navigate more easily with CMS like WordPress because of the curly brackets hidden in URLs.

Through the use of the .htaccess file, you can also do this for other websites (including those built with core PHP).

An Apache configuration file, or .htaccess, is an ASCII file in which you set configuration settings.

It's possible to rewrite URLs, block IP addresses, and create password-protected directories with Access Files.

In this lesson, we will look at how to remove/hide file extensions from URLs using htaccess.

We will now discuss the idea of how to remove php extension from url without htaccess in php with an example.

Step By Step Guide On How To Remove PHP Extension From URL Without htaccess :-

#remove html file extension https://talkerscode.com/page.html
# to https://talkerscode.com/page
RewriteEngine On
RewriteCond %{REQUEST_FILENAME1} !-d
RewriteCond %{REQUEST_FILENAME1} !-f
RewriteRule ^([^\.]+)$ $1.html [NC, L]
  1. You can start the rewrite engine by entering the command "RewriteEngine On". The URL rewrite module is used by Apache when this directive is used.
  2. Two conditions are added to check if the requested URL is not a directory or a file by using "RewriteCond %{REQUEST_FILENAME} !-d" and "RewriteCond %{REQUEST_FILENAME} !-f". In this way, we ensure that only non-existent files will be affected by the rewrite rule.
  3. The rewrite rule can be defined with the command "RewriteRule ^([^.]+)$ $1.html [NC, L]". When URLs don't contain a dot, this rule redirects them to the same URL with an extension of ".html".
  4. The flags "NC" and "L" at the end of the rule indicate "no case" and "last", respectively. Keeping the case-insensitive option "NC", and instructing Apache to stop processing any further rewrite rules once this one has been triggered, makes the rewrite rule case-insensitive.
  5. The Apache server checks whether the relevant directory contains a file named "page.html" when a user requests a URL like https://example.com/page. Whenever the user requests https://example.com/page.html, Apache serves it as if it were a real URL. HTTP return code 404 is returned if the requested file doesn't exist.

Conclusion :-

As a result, we have successfully learned how to remove php extension from url without htaccess in php with example.

The removal of file extensions from URLs can also enhance user experience and SEO, as well as make them appear cleaner and more professional.

With the help of the .htaccess file, you can easily and effectively remove the .php and .html extensions.

I hope this article on how to remove PHP extension from URL without htaccess helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪