skip to Main Content

I am trying to do a simple Apache Redirect that redirects a single page to a new URL, but not the child pages. E.g.

Redirect www.mysite.com/old to www.mysite.com/new

I do NOT want to redirect www.mysite.com/old/page1 to www.mysite.com/new/page1 or any childpages of /old.

Using a redirect like the following DOES include child pages:

Redirect 302 /old /new

How can I only redirect the top/parent page but not the child pages?

2

Answers


  1. Chosen as BEST ANSWER

    This is what I was looking for:

    RedirectMatch permanent ^/old$ /new
    

  2. Use RewriteCond and RewriteRule, like so:

    RewriteCond %{REQUEST_URI} ^/old$
    RewriteRule "(.*)" "http://www.example.com/new" [R=302,L]
    

    This way you verify that the requested URI is “/old”, and nothing else.

    ^/old$ :

    • ^ starts with
    • $ ends with
    • R=302: you can put another status if you want, like 301
    • L: last rewrite to verify, do not apply further rewrites

    If you try http://www.example.com/old/somepage.html, it will not match the RewriteCond since it does not match ^/old$ and will not redirect.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search