简体   繁体   中英

Rewriting a request to a parent directory with mod_rewrite

Doing some local PHP development with WAMP. Routing all requests to index.php, except requests for assets like stylesheets and images which are in a parent directory outside the document root. Directories are laid out as below:

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

.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

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/style/desktop.css returns an Internal Server Error

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:

Just to clarify any web site cannot access files above its DOCUMENT_ROOT folder level.

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.

Now that assets/ is inside the document root, I am able to rewrite asset requests to this directory as I desired in the OP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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