htaccess redirect if url contains

Htaccess Redirect If URL Contains

Sometimes you may need to redirect if URL contains a specific word or string in requested URL. You can easily do this using .htaccess file in Apache web server. In this article, we will look at how to redirect if URL contains certain string or word. It is very useful if you want to redirect many different URLs containing specific string or word, that are located in different folders, at one go. Instead of writing separate rewrite rules for each folder, you can make Apache server directly search requested URL for specific word/string and redirect matching ones.


Htaccess Redirect If URL Contains

Here are the steps to redirect if URL contains specific word or string.


1. Enable mod_rewrite (.htacces)

Here are the steps to enable mod_rewrite (.htaccess) according to your Linux system.

Ubuntu/Debian

Open terminal and run the following command to enable mod_rewrite.

$ sudo a2enmod rewrite


Redhat/CentOS/Fedora

Open Apache configuration file in a text editor.

$ sudo vi /etc/apache2/httpd.conf
OR
$ sudo vi /etc/httpd/httpd.conf

Look for the following line.

#LoadModule rewrite_module modules/mod_rewrite.so

Uncomment it by removing # at its beginning. If you don’t find this line, add it afresh.

Also look for the following Directory tag and change AllowOverride from None to All.

. . .
<Directory /var/www/html>
. . .
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride All
. . .
</Directory>
. . .


2. Open .htaccess file

Open terminal and run the following command to open .htaccess file

$ sudo vi /var/www/html/.htaccess


3. Redirect if URL contains

If you want to redirect all requests which contain the word ‘data’ in them then add the following lines to your .htaccess file.

RewriteCond %{REQUEST_URI} data 
RewriteRule .* index.php

In the above code, Apache server searches the requested URL, stored in REQUEST_URI server variable, for ‘data’ word and then redirects matching URLs to index.php.

You can replace the specific word data with other word, and redirect URL from index.php to something else, as per your requirement.


4. Restart Apache server

Restart Apache server to apply changes.

$ sudo service apache2 restart

That’s it. Now if you open browser and visit any URL to your website that contains the word ‘data’ in it, then you will be redirected to your website’s home page.

In this article, we have looked at how to conditionally redirect URLs based on presence of specific words and strings. It is very useful if you want to redirect URLs based on a string or word present in them.

Also read:

How to Change Root Password in MySQL
How to Run Shell Script as Cron Job
How to Create Cron Job using Shell Script
How to Get Current Directory in Shell Script
How to Get Current Directory in Python

One thought on “Htaccess Redirect If URL Contains

  1. Very usefull information indeed. I got some troubles to understand it all, because .htaccess is something new for me, but now i know. Thanks. Bookmarked.

    p.s. Can you put some more examples I think it will be usefull.

Leave a Reply

Your email address will not be published. Required fields are marked *