How To Remove PHP Extension From URL Using htaccess
Last Updated : Mar 11, 2024
IN - PHP htaccess | Written & Updated By - Dikshita
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" ; ?>
- First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- 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.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here and also adding the inline CSS here.
- Now we close the HTML file with </html> tag.
- we write <?php tag to write PHP within it.
- Using Echo to display a string
- ?> to close the php code.
- 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>
- Create e .htacess file as the same folder as the php file.
- Open the file <ifModule mod_rewrite.c>
- enable Rewrite Engine on
- Rewrite Conditionn for Directory
- Rewrite condition for File
- Rewrite Rule
- 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.