apache - 如何将 .htaccess 重定向到两个 MVC 树

标签 apache .htaccess mod-rewrite redirect

我尝试使用 apache mod rewrite 实现的目标如下:

  1. 当我访问 localhost/system/this/is/a/test/时,.htaccess mod 重写应该重定向到我的 index.php
  2. 当我访问 localhost/system/api/this/is/a/test/时,.htaccess mod 重写应该重定向到我的 api.php

我已经尝试了几个小时,但我似乎无法让它按照我想要的方式工作。目前所有访问仍然指向索引,无论我如何尝试,我似乎都无法让 .htaccess 根据 URL 指向 api 和索引。

以下是我当前的 .htaccess 文件:

ExpiresActive On

ExpiresByType image/png "access plus 2 weeks"
ExpiresByType image/jpg "access plus 2 weeks"
ExpiresByType image/jpeg "access plus 2 weeks"

AddType application/x-httpd-php .xml

Options +FollowSymLinks
Options -Indexes 

RewriteEngine On

RewriteBase /system/

# file access exclusions
RewriteRule ^(?!sitemap\.xml$).(.*)\.xml$ [R=404,L]
RewriteRule ^(.*)\.log$ [R=404,L]
RewriteRule ^(.*)\.txt$ [R=404,L]

# Rewrite the request to API
RewriteCond ${REQUEST_URI} ^api/(.*)$ [NC]
RewriteRule ^(.*)$ api.php/$1 [L]

# Rewrite the request to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]

你们中有人可以向我解释一下我做错了什么吗?这将是一个很大的帮助,我们将不胜感激。

提前谢谢

最佳答案

将您的规则替换为:

RewriteEngine On

RewriteBase /system/

# file access exclusions
RewriteRule ^(?!sitemap\.xml$).(.*)\.xml$ [R=404,L]
RewriteRule ^(.*)\.(txt|log)$ - [R=404,L]

# Rewrite the request to API
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^api/(.*)$ api.php/$1 [L]

# Rewrite the request to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]

关于apache - 如何将 .htaccess 重定向到两个 MVC 树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20990035/

相关文章:

php - PHP 的 HTTP 身份验证究竟是如何工作的?

apache - 当获取没有index.php的目录时,htaccess不会重写URL

php - htaccess - 重写具有相同名称目录的文件

php - 从 shell 或 apache 服务器运行 PHP 脚本

apache - .htaccess 破坏了所有的 css 和 js,尽管我设置了绝对路径

php - Google 对索引重定向 URL 的 react 是什么?

php - Apache Mod Rewrite : RewriteRule with L argument. 怎么了?

variables - Mod_Rewrite 用于具有多个变量的 URL

php - 如果客户端浏览器丢失或关闭连接,事件脚本请求是否完成?

apache - 安装 mod_jk 后 squirrelmail 不工作