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 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)