Redirect user if URL contains a specific keyword with htaccess

.htaccesshttp-status-code-404url

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.

Best Answer

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]