问题
I beg you pardon if this is a duplicate question. What I've found on the net didn't satisfy me at all.
As I asked in this question, I successfully rewrote the /public
folder to the root url. So If I visit localhost
or localhost/public
apache redirects me to /public
and hides public in the url. To sum up, I did it in this way:
RewriteEngine on
RewriteCond %{THE_REQUEST} /public/([^\s]+) [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_URI} !^/public
RewriteRule ^(.*)$ /public/$1 [NC,L,QSA]
However in this way everything will be redirected to the subfolder /public
. I need to preserve, or to add some exceptions, to some subdirectories that are not in the /public
folder.
+---assets
+---images
\---jp-pattern.jpg
+---javascripts
+---stylesheets
+---template
+---public
\---products
I need to preserve assets, images, javascripts, stylesheets, and template folders. I need them to be for example localhost/images
and so on.
I tried with the images folder first in this way:
RewriteCond %{REQUEST_URI} !^/images [NC]
or
RewriteCond %{REQUEST_URI} !^/images/?$ [NC]
or
RewriteCond %{REQUEST_URI} !/images [NC]
but none of them worked. The browser always returns me this message:
Not Found
The requested URL /public/images/jp-pattern.jpg was not found on this server.
This means that I couldn't add the exception.
How can I add those exceptions?
回答1:
You can add this block directly after RewriteEngine on
:
RewriteCond %{REQUEST_URI} ^/(images|javascripts|stylesheets|template)/ [NC]
RewriteRule ^ - [L]
This says that if the request begins with either of those directories, don't rewrite. Use of the L
flag makes that the last rule to be checked.
However, I advise that you actually do an existence check instead. Your HTML would likely be requesting an existing file, and so it would skip the public
check anyway.
RewriteCond %{REQUEST_URI} !^/public
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public/$1 [NC,L,QSA]
来源:https://stackoverflow.com/questions/35367895/how-to-redirect-to-a-subfolder-from-root-rewrite-subfolder-link-to-look-like-ro