.htaccesshttp-redirectdirectadmin

Redirect root/dir to root/new/dir with .htaccess


I have a directadmin host and domain, and I create directories in host and in the every directory is one index.html with url like this:

example.com/sdeds

Later, I installed a WordPress on the hosts and folded new directories like this:

example.com/2018/asdfa

But my old directories are in the root and very sloppy. I want create new directory and move them but don't miss the address that customers have, since addresses are provided as qr code to the customer. Is there a solution to this problem?


Solution

  • If you want to redirect everything from root, excluding the /2018/asdfa directory (that contains WordPress), into a new subdirectory then you can do something like the following at the top of your .htaccess file:

    RewriteEngine On
    
    RewriteRule !^2018/asdfa /newdirectory%{REQUEST_URI} [R=302,L]
    

    UPDATE: What if I have more than one directory to excluding? like: 2018, 2019, 2020

    If it's just directories like you mention, then it would be easiest to just use alternation in the regex. For example:

    RewriteRule !^20(18|19|20)/asdfa /newdirectory%{REQUEST_URI} [R=302,L]
    

    The (18|19|20) subpattern matches either 18, 19 or 20.

    If you wanted to match anything that looks-like a "recent" year then you can use a more generalized pattern. For example:

    RewriteRule !^20[12]\d/asdfa /newdirectory%{REQUEST_URI} [R=302,L]
    

    20[12]\d matches the strings (ie. directories) 2010 to 2029 (inclusive). [12] is a character class that matches either 1 or 2. And \d is a shorthand character class that matches any digit 0-9 (the same as [0-9]).