[英]htaccess multiple rewrite rules multiple subfolders
我无法理解htaccess的工作方式。
这是文件夹结构(近似示例)
# DocumentRoot
# |-- main
# | |-- site
# | | |-- assets
# | | | |-- test.js
# | | |-- index.php
# | |
# | |-- common
# | | |-- assets
# | | | |-- test.js
# | |
# | |-- .htaccess
这些是我希望实现的示例重定向
# http://www.example.com/main/assets/test.js => /main/site/assets/test.js
# http://www.example.com/main/common/assets/test.js => /main/common/assets/test.js
# http://www.example.com/main/common/path/to/file => /main/common/path/to/file
# http://www.example.com/main => /main/site/index.php
# http://www.example.com/main/part/url => /main/site/index.php
我对htaccess文件的尝试如下所示:
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^assets/(.*)$ site/assets/$1 [L]
RewriteRule ^common/(.*)$ common/$1 [L]
RewriteRule ^(.*)$ site/index.php [L]
我的问题:每个网址都路由到index.php。 我理解错了什么?
当前,所有查询和部分URL的处理都在php中进行。 我只需要提供来自两个资产文件夹和任何其他URL的资源即可转到index.php。
抱歉,如果这看起来像是我每天要捞的东西,但学会钓鱼,即尝试却无济于事。
在此先感谢您提供的所有帮助。
RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^assets/(.*)$ site/assets/$1 [L] RewriteRule ^common/(.*)$ common/$1 [L] RewriteRule ^(.*)$ site/index.php [L]
RewriteCond
指令仅适用于随后的第一个RewriteRule
。 因此,第二两个RewriteRule
指令将无条件执行(这就是为什么所有内容都被重写为site/index.php
)。
我不确定RewriteRule ^common/(.*)$ common/$1 [L]
打算做什么-它看起来像一个潜在的重写循环。
这两个条件检查请求是否映射到文件或目录-似乎与您要执行的操作相反。 从您的文件夹结构/main/assets/test.js
不会映射到现有文件,因此需要重写。
请尝试以下操作:
# If a request maps to a file or directory (but not the root directory) then stop
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
# Any assets (that don't exist) are rewritten to the /site/assets/ subdir
RewriteRule ^(assets/.*) site/$1 [L]
# Everything else is rewritten to the site/index.php file
RewriteRule ^ site/index.php [L]
这些是我希望实现的示例重定向
请注意,这些通常称为内部 “重写”,而不是严格地称为“重定向”。 “重定向”表示外部重定向 (即3xx响应)。 尽管Apache文档在这方面有点模棱两可,但在声明时确实会将其量化为“ 内部重定向”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.