該賞金到期in 4天。此問題的答案有資格獲得 100聲望獎勵。 安德烈斯 SK想引起更多人對這個問題的關注。
在這個例子中,我管理domain.com。在其中,我在 php 中有一個商店模板,可以動態加載所選商店:
- 域名.com/store1
- 域名.com/store2
- domain.com/store3
漂亮的網址是由.htaccess下面生成的。后面真正加載的是:
- domain.com/store/index.php?store=store1
- domain.com/store/index.php?store=store2
- domain.com/store/index.php?store=store3
每個商店都有內部鏈接,例如:
- domain.com/store1/catalogue
- domain.com/store1/catalogue/instruments
- domain.com/store1/catalogue/instruments/guitars
- domain.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/catalogue
- store1.com/catalogue/instruments => domain.com/store1/catalogue/instruments
- store1.com/catalogue/instruments/guitars => domain.com/store1/catalogue/instruments/guitars
- store1.com/electric-guitar/1337 => domain.com/store1/electric-guitar/1337
You get the idea.
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 in each one of the store's domain root path dinamically or inside the virtualhost .conf file? If so, how?
uj5u.com熱心網友回復:
我修改了您的·htaccess檔案,添加了RewriteCond根據請求域名選擇重寫規則,您可以domain.com根據需要進行修改,并驗證它是否在您的測驗環境中有效。
# permalinks
RewriteEngine on
# errors
ErrorDocument 404 /error.php?error=404
# ignore existing directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
#################################################
# match custom store domains
# if request.domain != domain.com
RewriteCond %{HTTP_HOST} !^domain.com$
# and ENV.Store == ''
RewriteCond %{ENV:Store} ^$
# match store id in domain name, eg: store1.com => store1
RewriteCond %{HTTP_HOST} ^([^.] )\.
# save store id to variable, goto custom domain rules
# S=6 skip 6 rules, goto custom domain rules
RewriteRule .? - [E=Store:%1,S=6]
# domain.com rules
# 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]
# skip custom domain rules
RewriteRule .? - [S=5]
# custom domain rules
# store
RewriteRule ^(/?|index.php)$ store/index.php?store=%{ENV:Store} [QSA,L]
# store: catalogue
RewriteRule ^catalogue/?$ store/catalogue.php?store=%{ENV:Store} [QSA,L]
RewriteRule ^catalogue/([0-9a-zA-Z-]{1,75})/?$ store/catalogue.php?store=%{ENV:Store}&cat=$1 [QSA,L]
RewriteRule ^catalogue/([0-9a-zA-Z-]{1,75})/([0-9a-zA-Z-]{1,75})/?$ store/catalogue.php?store=${ENV:Store}&cat=$1&subcat=$2 [QSA,L]
# store: products
RewriteRule ^([0-9a-zA-Z-]{1,150})/([0-9] )$ store/product.php?store=%{ENV:Store}&slug=$1&id_product=$2 [QSA,L]
uj5u.com熱心網友回復:
確保所有自定義存盤域(store1.com、store2.com等)都ServerAlias在domain.comvHost中定義,因此決議到與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/gongcheng/335021.html
