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.

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

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮