简体   繁体   English

.htaccess将特定请求重写到子目录页面

[英].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. 我正在尝试学习如何正确制作CMS并遇到一些问题。

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 localhost/learnCMS/ 这一切都在WAMP上,所以我的域是localhost / learnCMS /

Now, I'm trying to make a .htaccess file that makes rewrite rules for this 'domain'. 现在,我正在尝试制作一个.htaccess文件,该文件为该“域”制定重写规则。 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. 基本上,我需要一个规则来“重定向”如下所示的任何访问: domain / admin + [anystring]到子目录的index.php。 Basically this: 基本上是这样的:

  1. domain/[anything] would go to root's index.php 域/ [任何内容]都将进入根目录的index.php
  2. domain/admin[anything] would go to root/admin/index.php 域/管理员[任何内容]都将转到root / admin / index.php

catch is, it cannot be a fullpath, so I cannot write the D:/wamplocation/www/learnCMS/admin/ catch是,它不能是完整路径,所以我不能写D:/ wamplocation / www / learnCMS / admin /

Here is my .htaccess and a futile attempt, I get an error that says: 这是我的.htaccess和徒劳的尝试,但出现错误消息:
"The requested URL /admin/index.php was not found on this server." “在此服务器上找不到请求的URL /admin/index.php。”

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. ps我是一个初学者,因此,如果您能以易于理解的术语进行解释,我将不胜感激。

Strictly speaking localhost is your domain, not localhost/learnCMS . 严格来说localhost是您的域,而不是localhost/learnCMS From what you've told us, the document root of your web server is the parent directory of the learnCMS directory. 根据您的告诉,Web服务器的文档根目录是learnCMS目录的目录。 Let me call it /path/to/parent for now. 现在让我将其称为/path/to/parent

For absolute URIs (beginning with / ), the web server converts the URI path to a file name by pre-pending the document root. 对于绝对URI(以/开头),Web服务器通过在文档根前面添加前缀,将URI路径转换为文件名。 So /admin/index.php URI gets convert to /path/to/parent/admin/index.php file name. 因此/admin/index.php URI会转换为/path/to/parent/admin/index.php文件名。 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. 对于相对URI(不是以/开头),Web服务器会在预添加文档根之前包含子目录。 To get this, use .. 为此,请使用..

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

(the / in /admin/index.php was removed) /admin/index.php中的/已删除)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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