JoBe
JoBe

Reputation: 407

Redirect to special subdirectory if url contains a special word

There see a lot of instructions about rewrite in .htaccess, I know the following instructions in .htaccess would create a redirection

RewriteEngine on

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

And

RewriteEngine on

RewriteCond %{REQUEST_URI} foobar
RewriteRule ^ /index.php [L,R]

But I don't really understand all /<* stuff.. And i haven't seen any instructions how to send them to a url containing the original requested one..

Basicly I want to redirect any url that contains /foobar/ or /foobar only, not if it's /foobarone/ or /testfoobar ..

And I want it to be rewritten to example.com/404/(the path they wrote)

So accessing example.com/comments/foobar (with or without the ending slash) it would be redirected to example.com/404/comments/foobar

That way it would trigger a 404 and also log the event, including the path they tried to access..

Upvotes: 1

Views: 67

Answers (1)

anubhava
anubhava

Reputation: 785246

You may use this rule as your topmost rule for this:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/404/
RewriteRule (?:^|/)foobar(?:/|$) /404%{REQUEST_URI} [L,NC,R=301]

Upvotes: 2

Related Questions