Htaccess redirects if url contains specific string

How to write a redirect rule .htaccessif the URL contains a specific word?

eg. if it contains foobar, then redirect toindex.php

+13
source share
4 answers
RewriteCond %{REQUEST_URI} foobar
RewriteRule .* index.php

or several options .

+25
source
RewriteRule ^(.*)foobar(.*)$ http://www.example.com/index.php [L,R=301]

(No space inside your site)

+11
source
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/foobar/i$ index.php [NE,L]
+1

If the url contains the string certen, redirect to index.php. You need to map the variable % {REQUEST_URI} to check if the url contains the string certen.

To redirect example.com/foo/bar to /index.php if uri contains a bar anywhere on the uri line, you can use this:

RewriteEngine on

RewriteCond %{REQUEST_URI} bar
RewriteRule ^ /index.php [L,R]
+1
source

All Articles