![](/img/trans.png)
[英]how to redirect to a subfolder and then rewrite subfolder link to root in htaccess?
[英]how to redirect to a subfolder from root, rewrite subfolder link to look like root, and then add some exceptions in htaccess?
如果這是一個重復的問題,請原諒。 我在網上找到的東西根本讓我不滿意。
正如我在此問題中所提出的 ,我成功地將/public
文件夾重寫為根URL。 因此,如果我訪問localhost
或localhost/public
apache將我重定向到/public
並將公共隱藏在URL中。 總結起來,我是這樣做的:
RewriteEngine on
RewriteCond %{THE_REQUEST} /public/([^\s]+) [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_URI} !^/public
RewriteRule ^(.*)$ /public/$1 [NC,L,QSA]
但是,通過這種方式,所有內容都將重定向到子文件夾/public
。 我需要保留或向/public
文件夾中沒有的某些子目錄添加一些例外。
+---assets
+---images
\---jp-pattern.jpg
+---javascripts
+---stylesheets
+---template
+---public
\---products
我需要保留資產,圖像,javascript,樣式表和模板文件夾。 我需要它們是例如localhost/images
等。
我首先用這種方式嘗試了images文件夾:
RewriteCond %{REQUEST_URI} !^/images [NC]
要么
RewriteCond %{REQUEST_URI} !^/images/?$ [NC]
要么
RewriteCond %{REQUEST_URI} !/images [NC]
但他們都不起作用。 瀏覽器總是向我返回以下消息:
Not Found
The requested URL /public/images/jp-pattern.jpg was not found on this server.
這意味着我無法添加例外。
如何添加這些例外?
您可以在RewriteEngine on
之后直接添加此塊:
RewriteCond %{REQUEST_URI} ^/(images|javascripts|stylesheets|template)/ [NC]
RewriteRule ^ - [L]
這表示如果請求以這些目錄中的任何一個開始,請不要重寫。 使用L
標志使要檢查的最后一條規則。
但是,我建議您實際上進行一個存在性檢查。 您的HTML可能會請求一個現有文件,因此無論如何都會跳過public
檢查。
RewriteCond %{REQUEST_URI} !^/public
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public/$1 [NC,L,QSA]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.