在這個例子中,我管理domain.com. 在其中,我在 php 中有一個商店模板,可以動態加載所選商店:
domain.com/store1domain.com/store2domain.com/store3
漂亮的網址是由.htaccess下面生成的。后面真正加載的是:
domain.com/store/index.php?store=store1domain.com/store/index.php?store=store2domain.com/store/index.php?store=store3
每個商店都有內部鏈接,例如:
domain.com/store1/cataloguedomain.com/store1/catalogue/instrumentsdomain.com/store1/catalogue/instruments/guitarsdomain.com/store1/electric-guitar/1337
它完全按預期作業。這些是.htaccess使其在domain.com. 每個中的store查詢字串 ( store1, store2, store3)RewriteRule確定正在加載哪個存盤:
# permalinks
RewriteEngine on
# errors
ErrorDocument 404 /error.php?error=404
# ignore existing directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
#################################################
# store
RewriteRule ^([0-9a-zA-Z-]{2,50})/?$ store/index.php?store=$1 [QSA,L]
# store: catalogue
RewriteRule ^([0-9a-zA-Z-]{2,50})/catalogue/?$ store/catalogue.php?store=$1 [QSA,L]
RewriteRule ^([0-9a-zA-Z-]{2,50})/catalogue/([0-9a-zA-Z-]{1,75})/?$ store/catalogue.php?store=$1&cat=$2 [QSA,L]
RewriteRule ^([0-9a-zA-Z-]{2,50})/catalogue/([0-9a-zA-Z-]{1,75})/([0-9a-zA-Z-]{1,75})/?$ store/catalogue.php?store=$1&cat=$2&subcat=$3 [QSA,L]
# store: products
RewriteRule ^([0-9a-zA-Z-]{2,50})/([0-9a-zA-Z-]{1,150})/([0-9] )$ store/product.php?store=$1&slug=$2&id_product=$3 [QSA,L]
現在,棘手的部分來了。一些客戶端,想使用他們自己的域,以便store1.com/*加載相同的內容domain.com/store1/*(不使用重定向)。
例子:
store1.com=>domain.com/store1/store1.com/catalogue=>domain.com/store1/cataloguestore1.com/catalogue/instruments=>domain.com/store1/catalogue/instrumentsstore1.com/catalogue/instruments/guitars=>domain.com/store1/catalogue/instruments/guitarsstore1.com/electric-guitar/1337=>domain.com/store1/electric-guitar/1337
你明白了。
All the domains (domain.com, store1.com, store2.com, store3.com) are configured in the same Apache Web Server environment (using virtual hosts).
The question is: Can this be implemented in a .htaccess file in each one of the store's domain root path dynamically or inside the virtualhost .conf file? If so, how?
uj5u.com熱心網友回復:
確保所有的自定義“商店”領域(如store1.com,store2.com等)被定義為ServerAlias在domain.com虛擬主機,因此決心在同一個地方domain.com。
然后,您可以重寫自定義商店域的請求,以使用商店 ID(域名)作為 URL-path 的前綴,然后不加更改地使用現有規則。
例如,對于一個請求store1.com/catalogue/instruments被內部被重寫到/store1/catalogue/instruments,然后通過像往常一樣的現有規則處理。
以下指令應在現有# store規則之前:
# Internally rewrite the request when a custom domain is used
# - the URL-path is prefixed with the domain name
RewriteCond %{HTTP_HOST} !^(www\.)?domain\. [NC]
RewriteCond %{REQUEST_URI} !\.\w{2,4}$
RewriteCond %{HTTP_HOST} ^([^.] )
RewriteCond %{REQUEST_URI}@%1 !^/([^/] )/.*@\1
RewriteRule (.*) %1/$1
基本上就是這樣,盡管您也可能決定實施規范重定向 - 見下文。
上述指令的解釋:
第一個條件簡單地排除了對 main 的請求
domain.com(它應該已經有相關的store-id前綴到 URL-path)。%{REQUEST_URI} !\.\w{2,4}$- 第二個條件避免重寫對靜態資源(影像、JS、CSS 等)的請求。具體來說,它排除任何以 - 看起來像 - 檔案擴展名結尾的請求。%{HTTP_HOST} ^([^.] )- 第三個條件在 TLD 之前捕獲請求的域名,然后可以使用%1反向參考訪問該域名,并用于作為 URL 路徑的前綴。這假設沒有 www 子域(如評論中所述)。例如。給定對store1.comor的請求store2.co.uk,store1或store2分別被捕獲。%{REQUEST_URI}@%1 !^/([^/] )/.*@\1- 第四個條件檢查 URL 路徑是否還沒有以域名為前綴(上面捕獲的)。這主要是為了確保重寫的請求不會再次被重寫,從而導致重寫回圈。這是使用內部反向參考 (\1)實作的,該內部反向參考 ( ) 將 URL 路徑中的第一個路徑段與先前捕獲的域名 (%1) 進行比較。(.*) %1/$1- 最后,請求在內部被重寫,在請求的 URL 路徑前加上域名前綴。
忽略現有檔案(可能不需要)
# ignore existing directories RewriteCond %{REQUEST_FILENAME} -d RewriteRule .* - [L]
您尚未說明如何參考靜態資源(影像、JS、CSS 等),但您可能需要修改此規則以匹配對現有檔案的請求。(雖然我在上面的規則中添加的條件可能已經足以排除這些請求。)例如:
# ignore existing directories or files
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
規范重定向(可選)
您還應該考慮將表單的任何直接請求重定向store1.com/store1/catalogue/instruments回規范 URL store1.com/catalogue/instruments- 如果這些 URL 曾經被公開/發現過。表單請求store1.com/store2/catalogue/instruments自然會導致 404,因此不是問題。
例如,以下將緊跟在ErrorDocument現有規則中的指令之后:
# Redirect to remove the "/store1" URL-prefix when the "store1.com" domain is requested.
# - Only applies to direct requests.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^domain\. [NC]
RewriteCond %{HTTP_HOST} ^([^.] )
RewriteCond %{REQUEST_URI}@%1 ^/([^/] )/.*@\1
RewriteRule ^(?:[^/] )(/.*) $1 [R=301,L]
首先使用 302(臨時)重定向進行測驗以避免潛在的快取問題。
這基本上與上述重寫相反,但僅適用于直接請求。對REDIRECT_STATUSenv var的檢查可確保僅處理來自客戶端的直接請求,而不會處理由稍后重寫的重寫請求。
概括
兩個規則塊就位后......
# permalinks
RewriteEngine on
# errors
ErrorDocument 404 /error.php?error=404
# Redirect to remove the "/store1" URL-prefix when the "store1.com" domain is requested.
# - Only applies to direct requests.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^domain\. [NC]
RewriteCond %{HTTP_HOST} ^([^.] )
RewriteCond %{REQUEST_URI}@%1 ^/([^/] )/.*@\1
RewriteRule ^(?:[^/] )(/.*) $1 [R=301,L]
# ignore existing directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Internally rewrite the request when a custom domain is used
# - the URL-path is prefixed with the domain name
RewriteCond %{HTTP_HOST} !^(www\.)?domain\. [NC]
RewriteCond %{REQUEST_URI} !\.\w{2,4}$
RewriteCond %{HTTP_HOST} ^([^.] )
RewriteCond %{REQUEST_URI}@%1 !^/([^/] )/.*@\1
RewriteRule (.*) %1/$1
#################################################
# store
:
: existing directives follow
:
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/341375.html
標籤:apache .htaccess mod-rewrite
下一篇:無法使用CMD或入口點覆寫命令
