简体   繁体   English

使用mod_rewrite将请求重写到父目录

[英]Rewriting a request to a parent directory with mod_rewrite

Doing some local PHP development with WAMP. 使用WAMP进行一些本地PHP开发。 Routing all requests to index.php, except requests for assets like stylesheets and images which are in a parent directory outside the document root. 将所有请求路由到index.php, 对样式表和图像之类的资产的请求除外,这些请求位于文档根目录之外的父目录中。 Directories are laid out as below: 目录布局如下:

C:\WAMP\www\
    ↳ mySite
        ↳ public
            ↳ .htaccess
            ↳ index.php
        ↳ assets
            ↳ style
                ↳ desktop.css

.htaccess 的.htaccess

RewriteEngine on
RewriteBase /mySite/
RewriteCond %{REQUEST_URI} \.css$ [NC]
RewriteRule ^(.*)$ assets/$1 [L]
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^(.+)\/?$ index.php?url=$1 [L,QSA]

mySite.conf mySite.conf

Alias /mySite/ "C:/WAMP/www/mySite/public/"

<Directory "C:/WAMP/www/mySite/public/">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all
        Order allow,deny
    Allow from all
</Directory>

Results 结果

localhost/mySite/ - returns index.php as expected localhost/mySite/ -按预期返回index.php

localhost/mySite/style/desktop.css returns an Internal Server Error localhost/mySite/style/desktop.css返回内部服务器错误

What have I missed? 我错过了什么?

Thanks 谢谢

It is probably due to looping. 这可能是由于循环。

Try this code to fix this: 尝试使用以下代码来解决此问题:

RewriteEngine on
RewriteBase /mySite/

RewriteRule ^((?!assets/).+?\.css)$ assets/$1 [L,NC]

RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^(.+)\/?$ index.php?url=$1 [L,QSA]

As Anubhava below answered in another question: 正如下面的Anubhava在另一个问题中回答的那样:

Just to clarify any web site cannot access files above its DOCUMENT_ROOT folder level. 只是为了澄清任何网站都无法访问其DOCUMENT_ROOT文件夹级别以上的文件。

So I should have set the document root as mySite/ and routed all requests into public/ to achieve the same security I was aiming for by making public the document root. 因此,我应该将文档根目录设置为mySite/ ,并将所有请求都路由到public/以实现与公开文档根目录相同的安全性。

Now that assets/ is inside the document root, I am able to rewrite asset requests to this directory as I desired in the OP. 既然assets/位于文档根目录下,我就可以在OP中根据需要将资产请求重写到此目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM