【问题标题】:apache mod rewrite on a subdirectoryapache mod重写子目录
【发布时间】:2012-03-14 16:21:37
【问题描述】:

当我已经在 webroot 上有一个重写规则以删除“index.php”时,关于在子​​目录上重写 mod 的快速问题

我的 .htaccess 看起来像这样:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

我有一个 API 目录 (/webroot/api/index.php),我想要一个 RESTful API (Restler) 来为应用程序提供请求 - 所以我的问题是......

要发出 RESTful 请求,我需要使用以下 URL 格式:

/webroot/api/index.php/user/get/ (to get all the users)

我想要实现的是以下形式:

/webroot/api/user/get/ (return a list of users)

仍然保留主网站的基本重写规则

/webroot/members/  (display a list of users in the main applications)

非常感谢, 贾斯汀

【问题讨论】:

    标签: mod-rewrite apache


    【解决方案1】:

    你的基础重写看起来很奇怪。

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    

    这表示如果我们匹配index.php,我们就完成了(并且不会发生重写)。那是你要的吗?如果请求正好是针对/index.php 且没有尾随材料,则不处理。

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    

    删除 index.php 你想要类似的东西:

    RewriteRule ^index.php/(.*) $1 [L]
    

    例如index.php/foo 转到 foo(相对重写:然后 RewriteBase 被添加到 /foo URL 以反馈到请求管道。)

    【讨论】:

    • 您好,我想要的实际上是两个重写规则.. 1) if url = /index.php then remove "index.php" 和 2) if url = /api/index.php then remove "index.php" .. 不确定这是否可行,但看起来会更干净跨度>
    猜你喜欢
    • 2011-06-15
    • 2021-04-26
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    相关资源
    最近更新 更多