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.

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

报告相同问题?