This content originally appeared on HackerNoon and was authored by Emmanuel Okolie
Introduction
There was a time I wanted to remove my (.PHP) extension from my project URL. It became an extreme challenge for me, and I was bored cause I wished my .php not to show, then I stumbled into modrewrite. Ever since then, I have appreciated modrewrite functionality.
I was able to remove my extension in my project using my .htaccess. This tutorial will explain everything you should know about removing the php extension.
\ Before then, if you have no idea about php modrewrite, I’ll advise you to read my tutorial titled: What You Should Know About PHP ModRewrite And Mod_Security. It will help you have an idea about Mod rewrite, and having this idea will make this tutorial more understandable for you.
\ Now, without wasting much of your time, let’s jump right into this guide!
What is a .htaccess
the .htaccess is a file used for configuration by the Apache web server to control the behavior of the websites. It is frequently used to handle error pages, redirect online traffic, enforce security regulations, limit access to a website, and carry out other website-related duties.
Note: the root directory of a website is where the .htaccess file is stored, and it’s typically hidden from view. The .htaccess file can be edited with a text editor, and all the changes will take effect immediately, which makes the file a flexible and powerful tool for web administrators.
Features Of The .htacess File
Below are the things the .htaccess file enables you to do
URL Redirection: Users can be redirected from an outdated URL to a new URL using the .htaccess file.
Password Protection: A website, directory, or particular file can be password-protected using the .htaccess file.
Restricting Access to Specific Files and Directories: The .htaccess file may be used to limit access to specific files or directories.
Custom Error Pages: You may make custom error pages for HTTP error codes like 404 (Not Found) and 500 using the .htaccess file (Internal Server Error).
Rewrite Rules: To make URLs more user- or SEO-friendly, rewrite them using the .htaccess file.
MIME Types: You may define the MIME type of a file in the .htaccess file.
Prevent Hotlinking: You may stop other websites from creating hotlinks to your photos and other media assets by using the .htaccess file.
Activate Compression: The .htaccess file may be used to activate GZIP compression, which can accelerate a website's loading time.
Compel HTTPS: The .htaccess file may be used to force HTTPS encryption for all traffic to a website.
Block Spam Bots: To stop spam bots from visiting a website, the .htaccess file may be used to block particular IP addresses or user agents.
In this tutorial, we’ll be focusing only on rewriting URLs.
Removing The .php Extension
You must first enable mod rewrite on the Apache web server before moving on. The procedures to activate mod_rewrite (.htaccess) on the Apache web server are listed below first.
\
- Open htaccess File
Run the following command in a terminal window to view the .htaccess file. Please modify the file path of the .htaccess file according to your needs.
$ sudo nano /var/www/html/.htaccess
\
- Eliminating the.php Extension from the URL
To remove the .php extension from the URL of your website, you can use the following code in your .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
With the help of this code, the Apache web server will be instructed to drop the .php extension from the URL while still serving the proper .php file. For instance, the Apache web server will deliver the file page.php without displaying the.php suffix in the URL if a user attempts to visit the URL http://example.com/page.
\ Remember: Before making changes, always create a backup of your .htaccess file. Your website may malfunction or become unavailable if you make incorrect modifications to the .htaccess file.
\
- Restart the Apache web server
The Apache server may be restarted using the following command.
$ sudo systemctl restart httpd
Or you can restart it on your xampp as well.
Adding a trailing slash at the end
Many people asked me how to add a trailing slash at the end, for instance: yoursite.com/page/
Put the code below in and disregard the first snippet.
The removal of the extension is addressed in the first four lines, and the insertion of the trailing slash and redirection is addressed in the remaining lines.
Server-side redirects:
\
Apache: You can use the
.htaccess
file to add a rewrite rule to redirect the URL. Here is an example:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]
\
- Nginx: You can use the following configuration in your
nginx.conf
file:
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /$1/ permanent;
}
}
\
- Redirects that happen on the client side: You can use JavaScript to change the URL in the browser. Here's an illustration:
if (location.pathname.substr(-1) !== '/') {
location.pathname = location.pathname + '/' + location.search + location.hash;
}
Note: Depending on your unique use case and configuration, you should apply the abovementioned approaches.
Conclusion
We’ve come to the end of this tutorial. Hopefully, you’ve been enlightened on How to use mod_rewrite to remove your (.php) extension from your URL.
We’ve learned how to remove the extensions using mod_rewrite in the .htaccess file. And how to add a trailing slash and lots more.
We’ve seen a lot by just talking about this tutorial. So feel free to drop a comment! And if you want more instructions, please follow me.
Till next time, have a wonderful day!
About The Author
Emmanuel Okolie is a full-stack laravel developer with 2+
years of experience in the software development industry. He has developed full-fledged capabilities by combining software development, writing, and teaching others what he does. His stacks contain ReactJs,
Laravel,
PHP,
JavaScript,
and more.
He works as a freelancer, creating websites for customers and technical guides to show others how to accomplish his work.
Emmanuel Okolie is ready and willing to talk with you. Kindly visit and follow him on Linked-In, Facebook, Github, Twitter, or his Website.
This content originally appeared on HackerNoon and was authored by Emmanuel Okolie
Emmanuel Okolie | Sciencx (2024-10-21T18:03:15+00:00) How To Use Mod_Rewrite To Remove Your (.PHP) Extension From Your Work. Retrieved from https://www.scien.cx/2024/10/21/how-to-use-mod_rewrite-to-remove-your-php-extension-from-your-work/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.