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 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分