2

Using the regex and the .htaccess file, I want to redirect users to a certain URL.

I want to redirect:

  • http://www.example.com/* but not http://www.example.com/CDadmin/*

I have tried the following:

  • http://www.example.com/CDadmin\/[*-^$CDadmin\/*]

But does not work when I test with regex101.

1
  • Where are you wanting to redirect to? Is /CDadmin a physical directory on the filesystem? This would seem to only require a very simple regex - in fact it might not even require much of a regex at all. Doing this redirect in Apache config (ie. .htaccess) will be much more efficient (and easier) than using PHP.
    – MrWhite
    Commented May 12, 2016 at 10:27

1 Answer 1

1

Since you've not clarified the specifics of your question, I'll make some assumptions:

  • You wish to redirect to a different domain. eg. example.net
  • example.net points to a different server.
  • /CDadmin is a physical directory on the filesystem.

  1. At the top of the .htaccess file in the document root of example.com, place the following:

    RewriteEngine On
    RewriteRule (.*) http://example.net/$1 [R=302,L]
    

    The only regex here is (.*) - which will basically redirect everything. (Read on...)

    This is a temporary (302) redirect. Change to a 301 if you want to make it permanent.

  2. Then, in the .htaccess file in the /CDadmin subdirectory, simply enable the rewrite engine:

    RewriteEngine On
    

    This will effectively override the mod_rewrite directives in the parent .htaccess file, so nothing will be redirected when accessing /CDadmin/<something>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.