简体   繁体   中英

Best way to mod_rewrite images to /public

I'm trying to figure out the best way to have everything on my website point at index.php except for any images/css/javascript which are all located in /public.

These are the two sections methods I've tried in my .htaccess but would like to know if there are any better methods or ways to improve upon either of these.

Also, I'm not sure which of these would be better on the server (less overhead).

RewriteEngine on

RewriteBase /subfolder/
RewriteRule !\.(js|ico|txt|gif|jpg|png|css|pdf)$ index.php
RewriteRule ^.*/public/(.*)$ /subfolder/public/$1

#RewriteBase /subfolder
#RewriteCond %{REQUEST_URI} ^(?!/public/).+ [NC]
#RewriteRule !\.(js|ico|txt|gif|jpg|png|css|pdf)$ index.php [L]

Anyone have any suggestions on how to do this the best way?

Here's what I've done for my own framework (the " STATIC " environment variable is very complex to calculate so I've removed the "hard" part that may be too complex for now):

# if the site is static (i.e. xx.static.mydomain.com)
RewriteCond %{ENV:STATIC} 1
# Try to assign the extension into an EXT variable:
RewriteRule (.*)(\.(css|js|htc|pdf|jpg|jpeg|gif|png|ico)){1}$ $1$2 [NC,QSA,E=EXT:$3]
# (!) if it's an image...
RewriteCond %{ENV:EXT} (jpg|jpeg|gif|png|ico) [NC]
# ...no matter the extension, change it to 'img' :
RewriteRule (.*) - [QSA,E=EXT:img]

# if it's static...
RewriteCond %{ENV:STATIC} 1
# ...and its extension is not empty:
RewriteCond %{ENV:EXT} !^$
# ...and the filename exists in the good dir, i.e. :
# /web/htdocs/mydomain/css/xx.css
# /web/htdocs/mydomain/js/yy.js
# /web/htdocs/mydomain/img/aa.jpg
# /web/htdocs/mydomain/img/bb.gif
# and so on...
RewriteCond %{DOCUMENT_ROOT}/%{ENV:EXT}%{REQUEST_FILENAME}  -f
# file exists => override filename and stop:
RewriteRule  ^(.+) %{DOCUMENT_ROOT}/%{ENV:EXT}%{REQUEST_FILENAME} [QSA,L]

This way you can do a very nice organisation, and when it comes to get one "static" file, if you put the rules above at the very beginning, they'll be filtered properly, and never redirected to index.php .

Thus, my directory structure looks like this (which is very clean I think), and you could do the same easily:

/web/htdocs/
    |-- css
    |-- js
    |-- img
    |   `-- prettyPhoto
    |       |-- ...
    |       `-- light_square
    |-- js
    |   `-- openid
    `-- htm

If all of the non-resource requests are pretty "virtual" urls, without corresponding files then the best way is

#only if it is a request for a file that does not exist
#i.e exlcude resources css etc
RewriteCond %{REQUEST_FILENAME} !-f
#send it to index.php
RewriteRule .* index.php [L]

#send image requests to subfolder public
#if you can modify original resource urls to point to subfolder, this will not be necessary
RewriteRule /public/(.*)$ /subfolder/public/$1 [L]

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