Teshan N.
Teshan N.

Reputation: 2545

301 Redirect URLs starting with a specific keyword

I need to redirect all URLs staring with a specific word using RedirectMatch.

Let's say I need to redirect any URL that starts with the keyword "pdf" should be redirected to the base URL.

Requirement:

http://www.example.com/pdf         -> http://www.example.com/
http://www.example.com/pdf-444-abc -> http://www.example.com/
http://www.example.com/blog/pdf-abc-1234          -> must not redirect
http://www.example.com/images/web/pdf/1234568.jpg -> must not redirect

I have used the following 301 redirect rule and it redirects all above examples.

RedirectMatch 301 /pdf(.*)$ http://www.example.com/

How can I achieve this?

Upvotes: 1

Views: 1079

Answers (1)

Abhishek Gurjar
Abhishek Gurjar

Reputation: 7476

For matching only starting characters use '^' (caret)

RedirectMatch 301 ^/pdf(.*) http://www.example.com/

Upvotes: 1

Related Questions