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 Using htaccess

Last Updated : Mar 11, 2024

How To Remove PHP Extension From URL Using htaccess

In this article we will show you the solution of how to remove php extension from url using htaccess, .htaccess: htaccess files actually Apache server configuration files provide a way to make configuration changes without modifying the main configuration file.

It manages - Redirecting/Rewriting, Frond End Appearance, URL blocking, Directory Listing, Caching, Authorization

We can use .htaccess to remove both php and html extensions.

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

PHP code

<!DOCTYPE html>
<html lang = " en " >
<head>
    <meta charset = " UTF - 8" >
    <meta http-equiv = " X-UA-Compatible " content = " IE=edge " >
    <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " >
    <title> how to remove php extension from url using htaccess </title>
</head>
<body>
    <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1>
    <h2> how to remove php extension from url using htaccess </h2>
</body>
</html>
    <?php
echo "Welcome to Talkerscode" ;
?>
  1. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here and also adding the inline CSS here.
  7. Now we close the HTML file with </html> tag.
  8. we write <?php tag to write PHP within it.
  9. Using Echo to display a string
  10. ?> to close the php code.
  11. Now let us see the code for .htaccess file now.

.htaccess code

<ifModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>
  1. Create e .htacess file as the same folder as the php file.
  2. Open the file <ifModule mod_rewrite.c>
  3. enable Rewrite Engine on
  4. Rewrite Conditionn for Directory
  5. Rewrite condition for File
  6. Rewrite Rule
  7. Close with the tag </ifModule>

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to remove php extension from URL using .htaccess using PHP.

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