douchen1924 2016-12-27 18:12
浏览 92
已采纳

.htaccess重写根目录禁止

I have a document structure in which I have a .htaccess file in the parent directory called Fort. In that same directory, I have a folder named public. I have in that public directory a bootstrap.php script.

Here is my .htaccess code:

RewriteEngine On

RewriteBase /Fort

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.+)$ bootstrap.php?url=$1 [QSA,L]

I am using mamp and the default url is set to localhost:8888/Fort/.

My bootstrap.php script has this code:

<?php

echo $_GET['url'];

For some reason when I pass a url say localhost:8888/Fort/foo/bar, my bootstrap.php script returns the url as foo/bar. But if I pass the url localhost:8888/Fort/, I get a 403 Forbidden error, and the bootstrap.php script is not loaded.

  • 写回答

1条回答 默认 最新

  • douti0687 2016-12-27 19:42
    关注

    Good question! Thanks for all the info.

    Change the last line to this:

    RewriteRule ^(.*)$ bootstrap.php?url=$1 [QSA,L]
    

    When you visit /Fort/ there is an empty string for the rewrite to match, and you were matching + which means one or more characters, when there are in fact no characters, so it wasn't getting triggered. Changing to * says zero or more characters which fixes the problem and will now match /Fort/ as expected.

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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化