How to Redirect URL Using .htaccess if Contains Specific Word (.html)
In this tutorial, I’m going to share how to redirect URL using .htaccess if it contains specific word. Let’s think our blog URLs contain .html end of the URL. We’re going to redirect the .html URLs to without .html.
The Solution
We can do this using .htaccess rewrite rule. Open .htaccess file and write the rule like this:
RewriteEngine On
# check if url contains .html and then redirect to without .html link
RewriteRule (.*).html$ /$1 [R=301,L]
Let’s see another example. If URL contains foobar
we’ll redirect the URL to index.php. Here’s the rule:
RewriteEngine on
RewriteCond %{REQUEST_URI} foobar
RewriteRule ^ /index.php [L,R]
We also can write the rule like this:
RewriteEngine on
RewriteRule ^(.*)foobar(.*)$ http://www.example.com/index.php [L,R=301]
I hope this article will help you.
Md Obydullah
Software Engineer | Ethical Hacker & Cybersecurity...
Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.