Search code examples
.htaccessmod-rewriteurl-rewriting

htaccess rule for any keyword to a physical sub-folder


I am struggeling with something I am trying to do regarding folder. I have a sub-folder called "admin" so I can access the site as mydomain.com/admin.

However I want that if I say mydomain.com/foobar or mydomain.com/28y282djdhjd, then it should always assume it is "admin".

Of course the rule should not apply to existing files and folders but it is specifically to the first folder from the domain root.

It's as if I want the admin to be accessed no matter what I call it (as long as it is not a physical folder or file)...

I am not good with htaccess if someone could guide me I would truly appreciate it.


Solution

  • So, you need to:

    1. Check that the request isn't for a file that already exists;
    2. Check that the request isn't for a directory that already exists;
    3. Only redirect the request, if it refers to the first folder-level from the domain root;

    If that's all correct, something like this should do it:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^\/]*)\/?$ http://example.com/admin [L]