visit
Before then, if you have no idea about php mod_rewrite, I’ll advise you to read my tutorial titled: What You Should Know About PHP Mod_Rewrite 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.
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.
$ sudo nano /var/www/html/.htaccess
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 //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.
$ sudo systemctl restart httpd
Or you can restart it on your xampp as well.
Many people asked me how to add a trailing slash at the end, for instance: yoursite.com/page/
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_HOST}/$1/ [L,R=301]
nginx.conf
file: location / {
if (!-e $request_filename){
rewrite ^(.*)$ /$1/ permanent;
}
}
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.
We’ve seen a lot by just talking about this tutorial. So feel free to drop a comment! And if you want more , please follow me.
Till next time, have a wonderful day!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.
Emmanuel Okolie is ready and willing to talk with you. Kindly visit and follow him on , , , , or his .