我尝试使用 apache mod rewrite 实现的目标如下:
- 当我访问 localhost/system/this/is/a/test/时,.htaccess mod 重写应该重定向到我的 index.php
- 当我访问 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/