doushun1870 2014-04-03 14:59
浏览 53
已采纳

子目录中的htaccess rewritebase

in a project i have .htaccess files in many Subdirectories.

here is an example Structure:

project/.htaccess

project/admin/.htaccess

project/admin/pdf/.htaccess

in the last one i successfully use the following:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /project/admin/pdf/
RewriteRule ^([pdf\.actions]+)$ pdf.out.actions.php [QSA]

this works fine, if the Project Folder is placed directly in the root directory. all other subdirs have same htaccess logic, now if the user needs to put the project in any other directory rather than root, he/she has to change all RewriteBase in all htaccess Files in all subdirectories of the project.

my question: how to rewrite my htaccess content, so i get the same result as mentioned above but with "dynamic" base or without Base...etc? i tried somany things without success.

i removed the line RewriteBase /project/admin/pdf/ and tested it, i get site not found...etc!

any Idea?

thanks

  • 写回答

1条回答 默认 最新

  • doucong6884 2014-04-03 16:08
    关注

    You can try adding something like this to the top of your rules:

    RewriteCond %{ENV:URI} ^$
    RewriteRule ^(.*)$ - [ENV=URI:$1]
    
    RewriteCond %{ENV:BASE} ^$
    RewriteCond %{ENV:URI}::%{REQUEST_URI} ^(.*)::(.*?)\1$
    RewriteRule ^ - [ENV=BASE:%2]
    

    And then instead of using RewriteBase, you'll have to include the BASE environment variable everywhere. For example:

    RewriteRule ^([pdf\.actions]+)$ %{ENV:BASE}/pdf.out.actions.php [QSA]
    

    The conditions do a couple of things, but both rules change nothing in the URI and only add environment variables.

    The first condition is necessary because it grabs the requested URI before anything is done to it, and stores it. This is important because the (.*) grouping in the rule has the base stripped off. We want the unaltered URI with the base stripped off. So the URI environment variable is the URI with the base stripped off.

    The second condition is necessary because it compares the URI environment variable with the %{REQUEST_URI}, which is the entire URI, including the base. That comparison yields us the part that was stripped off, or the base, and that'll be stored in the BASE environment variable.

    The conditions which match against ^$ is simply ensuring that this is the first time through the rules (meaning neither of the environment variables have been set). The rewrite engine will loop so we only want to set these the first time.


    EDIT: actually, now that I'm looking at it, you could probably leave the first one out:

    RewriteCond %{ENV:BASE} ^$
    RewriteCond $1::%{REQUEST_URI} ^(.*)::(.*?)\1$
    RewriteRule ^(.*)$ - [ENV=BASE:%2]
    

    Using the $1 backreference which matches the rule, noting that the rule itself is partly evaluated (pattern applied) before any of the conditions are checked.

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

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?