我正在嘗試使用重寫規則訪問兩個目錄。我從網上和其他SO帖子中抓取並測試了很多代碼,但是結果仍然無法正常工作。這是我想要做的:htaccess重寫爲兩個目錄
1)如果頁面請求用於/ home,/ test,/ 404,/ etc,請從/ app目錄加載文件並像這樣遍歷:/ home,/ news/topics/post,/ contact /,/ blog/category/date/post
2)如果頁面請求僅用於/ admin,則從/ admin目錄加載文件並能夠像這樣遍歷:/admin// dashboard,/ admin/page/edit,/ admin/page/delete,/ admin/page/create,/ admin/section/delete/
發生了什麼? 如果我去/家或只是/,工作得很好!但是,如果我到/測試/接觸,/管理,/等我拿到沒有找到
文件結構的404文件(雖然可以改變):
/admin
--dashboard.php
/app
--index.php
.favicon.png
.favicon.ico
.htaccess
我開始想,它可能會更好,只是硬編碼重寫這樣,而不是:
RewriteRule ^home$ /atlas.php [NC,L]
RewriteRule ^admin$ /admin/dashboard.php [NC,L]
...但我想我會看到,如果SO社會可以幫助之前,我在這一個認輸。
有什麼建議嗎?
我當前的代碼:
Options -Indexes
Options -MultiViews
Options +FollowSymLinks
# Set the default site page
DirectoryIndex /app/index.php home
# Rewrite Rules and Conditions
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase/
# Remove www
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L,NC]
# Assets are in /app
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.(gif|jpg|png|jpeg|css|js|svg|swf)$ /app/$1.$2 [L,NC]
# First, check if request is to site pages, which is in /app/
RewriteCond %{DOCUMENT_ROOT}/app%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/app%{REQUEST_URI} -d
RewriteRule ^(.*)$ /app/index.php?/$1 [NC,L]
# Second, check if request is to /admin, which is in /admin/
RewriteCond %{DOCUMENT_ROOT}/admin%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/admin%{REQUEST_URI} -d
RewriteRule ^(.*)$ /admin/dashboard.php?/$1 [NC,L]
# Default to /app
RewriteCond ${REQUEST_URI} !^/app/
RewriteRule ^(\w+)$ /app/$1 [L]
</IfModule>
我不使用這種方法: ?path = $ 1&$ 2&$ 3 – JsusSalv
沒有'$ 2'和'$ 3','$ 1'已經匹配所有內容。您格式不正確的查詢字符串有點奇怪,我不會推薦。 – Halcyon
我保持管理文件與網站或應用程序分開,因爲可能有多個應用程序。我不喜歡混合管理代碼,可能會被開發人員搞亂的應用程序只有 – JsusSalv