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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)