Apache Rewrite redirect all pages except for specific

9,344

Looks like you want to make use of Apache's RewriteCond directive to conditionally redirect requests based on a pattern match, like so:

RewriteCond "%{REQUEST_URI}" "! /testcode.jsp" [OR]
RewriteCond "%{REQUEST_URI}" "! /search/*" [OR]
{ditto for your other requirements...}
RewriteRule .* /retired [R,L]

Which should explicitly allow anything in your regex pattern through, while redirecting everything else. See also, https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewritecond For more details.

Share:
9,344

Related videos on Youtube

Hansilog
Author by

Hansilog

Updated on September 18, 2022

Comments

  • Hansilog
    Hansilog almost 2 years

    I am retiring a site and would like to display a message when anyone tries to access any page on the site EXCEPT for a couple of pages. Some pages have certain pageid parameters. So basically everything should redirect to /retired except for:

    • /testcode.jsp

    • /search/*

    • /codes/*

    • /resources/blogs/*

    • /status/*

    • /wiki/article?pageid=178973-ghgh-98089

    • /wiki/article?pageid=354973-aaaa-80879

    • /wiki/article?pageid=334224-sada-20293

    • /wiki/article?pageid=546665-qasq-34491

    • Admin
      Admin about 6 years
      Are you intending to use .htaccess? Or is this all in the server config? Do you have any existing directives? How you implement this can be dependent on your existing directives. For instance, are some of these URLs routed through afront-controller? I'd also question why you want to "redirect" to /retired, rather than serve a custom 404/410, since presumably you want to remove these pages from the index if the site is being "retired"?
    • Admin
      Admin about 6 years
      @MrWhite ... It will be in server config, straight-forward config. The site as a whole will be redirected to /retired and will have custom message, except for some pages, static (html/jpg/js) and dynamic(served by an app server --- those wiki and /resources/blogs, /codes, /search and /status pages).
  • Daywalker
    Daywalker over 3 years
    For me the CondPattern "! /testcode.jsp" did not work with the quotes. I had to remove the quotes to make it work. Otherwhise EVERYTHING matched. no idea why...