doufenpaiyu63706 2019-01-28 18:25
浏览 182
已采纳

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

I have trouble understanding the way htaccess works.

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:

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. What have I understood wrong?

Currently all processing for the query and partial url is handled in php. I only need to serve resources from the two asset folders and any other urls to go to the index.php.

Sorry if this looks like I'm trying to get my fish for the day but learning to fish i.e. experimenting is getting me nowhere.

Thanks in advance for all the help.

  • 写回答

1条回答 默认 最新

  • duankui6150 2019-01-28 19:45
    关注
    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. So, the 2nd two RewriteRule directives are going to execute unconditionally (which is why everything is being rewritten to site/index.php).

    I'm not sure what RewriteRule ^common/(.*)$ common/$1 [L] is intended to do - it looks like a potential rewrite loop.

    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.

    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). Although the Apache docs are a little ambiguous in this regard, they do quantify them as "internal redirects" when stated.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么