2

I need help with setting up a rule inside .htaccess that does the following:

Whenever the requested URL contains keyword love then redirect the user to another URL or maybe show a 404 page.

Clarifying a bit more: If someone tries to access this URL --> www.example.com/love or www.example.com/?love or any URL that contains the word love should be redirected to a 404 page.

MrWhite
  • 11,643
  • 4
  • 25
  • 40
Johan Larsson
  • 87
  • 2
  • 12

3 Answers3

2

You need to deal with matching something in the path and matching something in the query string separately, since Apache does not provide a variable containing both. The first RewriteRule here matches "love" in the path, and the second rule limited by the RewriteCond matches "love" in the query string.

RewriteRule .*love.* - [R=404]

RewriteCond %{QUERY_STRING} .*love.*
RewriteRule .* - [R=404]
mgorven
  • 30,036
  • 7
  • 76
  • 121
0

Whenever the requested URL contains keyword "love"

If you want to exclude words that contain these letters as a sub-word. eg. "glove", "slovenly", "cloves", etc. then you need to incorporate word boundaries in the regex, eg. \blove\b.

You can also do this with a single rule by checking against THE_REQUEST server variable, which contains both the URL-path and query string.

For example:

RewriteEngine On

RewriteCond %{THE_REQUEST} \blove\b
RewriteRule ^ - [R=404]
MrWhite
  • 11,643
  • 4
  • 25
  • 40
-1
RewriteRule ^(.+?)www.domain.com.*\.love/.*$ - [R=404]
GioMac
  • 4,444
  • 3
  • 24
  • 41
  • This will never match since the `RewriteRule` _pattern_ matches the URL-path only, not the hostname. However, the dot is escaped to match a literal dot and you've introduced a slash that isn't present in the example URL in the question. – MrWhite Aug 18 '20 at 23:50