.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.

我有一个文档结构,我在父目录中有一个 .htaccess code>文件 叫 Fort code>。 在同一目录中,我有一个名为 public code>的文件夹。 我在 public code>目录中有一个 bootstrap.php code>脚本。 p>

这是我的 .htaccess code>代码: p>

  RewriteEngine On 
 
RewriteBase / Fort 
 \  nRewriteCond%{REQUEST_FILENAME}!-f 
RewriteCond%{REQUEST_FILENAME}!-d 
 
RewriteRule ^(。+)$ bootstrap.php?url = $ 1 [QSA,L] 
  code>  pre> \  n 
 

我正在使用mamp,默认网址设置为 localhost:8888 / Fort / code>。 p>

我的 bootstrap.php code>脚本有以下代码: p>

 &lt;?php 
 
echo $  _GET ['url']; 
  code>  pre> 
 
 

出于某种原因,当我传递一个网址时,请说 localhost:8888 / Fort / foo / bar code>, 我的 bootstrap.php code>脚本将URL返回为 foo / bar code>。 但是,如果我传递url localhost:8888 / Fort / code>,我会收到 403 Forbidden code>错误,并且未加载 bootstrap.php code>脚本 。 p> div>

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.