简体   繁体   中英

.htaccess rewrite specific requests to a sub-directory page

I am trying to learn how to properly make a CMS and have run into some issues.

Folder structure:

Root: (folder named learnCMS)
        admin (directory)
            index.php
        js (directory)
        css (directory)
        index.php
        .htaccess

This is all on WAMP, so my domain is

Now, I'm trying to make a .htaccess file that makes rewrite rules for this 'domain'. So far there are only two rules I need, but I cannot get one of them to work, which is why I am asking. Basically I need a rule that will 'redirect' any access that goes as follows: domain/admin+[anystring] to a subdirectory's index.php. Basically this:

  1. domain/[anything] would go to root's index.php
  2. domain/admin[anything] would go to root/admin/index.php

catch is, it cannot be a fullpath, so I cannot write the D:/wamplocation/www/learnCMS/admin/

Here is my .htaccess and a futile attempt, I get an error that says:
"The requested URL /admin/index.php was not found on this server."

RewriteEngine on
    RewriteRule ^admin /admin/index.php [QSA,L]
    RewriteRule ^([^./]{3}[^.]*)$ index.php?page=$1 [QSA,L]

ps I'm a beginner, so I'd appreciate it if you explained in easy-to-understand terms if possible.

Strictly speaking localhost is your domain, not localhost/learnCMS . From what you've told us, the document root of your web server is the parent directory of the learnCMS directory. Let me call it /path/to/parent for now.

For absolute URIs (beginning with / ), the web server converts the URI path to a file name by pre-pending the document root. So /admin/index.php URI gets convert to /path/to/parent/admin/index.php file name. As the error message says, this is not found on your server.

For relative URIs (not beginning with / ), the web server includes the sub-directories before pre-pending the document root. To get this, use ..

RewriteRule ^admin admin/index.php [QSA,L]

(the / in /admin/index.php was removed)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM