简体   繁体   English

htaccess多个重写规则多个子文件夹

[英]htaccess multiple rewrite rules multiple subfolders

I have trouble understanding the way htaccess works. 我无法理解htaccess的工作方式。

This is the folder structure (approximate sample) 这是文件夹结构(近似示例)

# DocumentRoot
# |-- main
# |   |-- site
# |   |   |-- assets
# |   |   |   |-- test.js
# |   |   |-- index.php
# |   |
# |   |-- common
# |   |   |-- assets
# |   |   |   |-- test.js
# |   |
# |   |-- .htaccess

These are the sample redirects I'm hoping to achieve 这些是我希望实现的示例重定向

# 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

My attempt at htaccess file looks like this: 我对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]

My Problem: Every Url gets routed to index.php. 我的问题:每个网址都路由到index.php。 What have I understood wrong? 我理解错了什么?

Currently all processing for the query and partial url is handled in php. 当前,所有查询和部分URL的处理都在php中进行。 I only need to serve resources from the two asset folders and any other urls to go to the index.php. 我只需要提供来自两个资产文件夹和任何其他URL的资源即可转到index.php。

Sorry if this looks like I'm trying to get my fish for the day but learning to fish ie experimenting is getting me nowhere. 抱歉,如果这看起来像是我每天要捞的东西,但学会钓鱼,即尝试却无济于事。

Thanks in advance for all the help. 在此先感谢您提供的所有帮助。

 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 directives only apply to the first RewriteRule that follows. RewriteCond指令仅适用于随后的第一个RewriteRule So, the 2nd two RewriteRule directives are going to execute unconditionally (which is why everything is being rewritten to site/index.php ). 因此,第二两个RewriteRule指令将无条件执行(这就是为什么所有内容都被重写为site/index.php )。

I'm not sure what RewriteRule ^common/(.*)$ common/$1 [L] is intended to do - it looks like a potential rewrite loop. 我不确定RewriteRule ^common/(.*)$ common/$1 [L]打算做什么-它看起来像一个潜在的重写循环。

The two conditions check that the request maps to a file or directory - which seems to be the opposite of what you want to do. 这两个条件检查请求是否映射到文件或目录-似乎与您要执行的操作相反。 From your folder structure /main/assets/test.js does not map to an existing file, so needs to be rewritten. 从您的文件夹结构/main/assets/test.js 不会映射到现有文件,因此需要重写。

Try the following instead: 请尝试以下操作:

 # 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]

These are the sample redirects I'm hoping to achieve 这些是我希望实现的示例重定向

Note that these are more commonly referred to as internal "rewrites", not strictly "redirects". 请注意,这些通常称为内部 “重写”,而不是严格地称为“重定向”。 "Redirects" implies external redirects (ie. 3xx response). “重定向”表示外部重定向 (即3xx响应)。 Although the Apache docs are a little ambiguous in this regard, they do quantify them as " internal redirects" when stated. 尽管Apache文档在这方面有点模棱两可,但在声明时确实会将其量化为“ 内部重定向”。

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

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