mod_rewrite - I can't make it to work like I w

2019-08-10 01:01发布

问题:

Thanks for reading. First of all, I got one .htaccess inside my root directory with the following statement :

# Mod Rewrite
<IfModule mod_rewrite.c>
    RewriteEngine on

    RewriteRule     ^$              /root/              [L]
    RewriteRule     (.*)            /root/$1            [L]
</IfModule>

and inside root folder I got this file :

# Mod Rewrite
<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^(.*)$          /root/index.php/$1 [PT,L]
</IfModule>

Also, in the folder /root, I got three folders : css, js and images.

My application is designed to be separated from my MVC framework (a bit like CodeGgniter is). So here's the tree :

- application
    - my application files are there
- root
    - css
    - js
    - images
- grecko
    - my mvc framework files are there

Like this, I can update my framework without touching the entire website who run under the application folder.

Anyway, what I would like to do now is to move the three folder I said above ( css, js and images) and move them inside /application/assets/. But since everything is redirected to /root/, I will need a rule that allow request like that http://www.domain.tld/css/blabla.css to be mapped to /application/assets/css/blabla.css, etc.

I did try but without success.

Thanks.

回答1:

Put this code in $DOCUMENT_ROOT/.htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /

    RewriteRule ^(css|js|images)(/.*|)$ application/assets/$1$2 [L,NC]

    RewriteCond %{ENV:REDIRECT_STATUS} !200
    RewriteRule ^ root${REQUEST_URI} [L]
</IfModule>


回答2:

Try putting this in the .htaccess of your web root (not root folder):

RewriteEngine on

RewriteRule ^(css|js|images)(.*) application/assets/$1$2 [L]

RewriteCond %{REQUEST_URI} !^/application/assets [NC]
RewriteRule (.*) root/$1 [L]

Avoid extraneous spaces.