簡體   English   中英

通過.htaccess的通配符子域

[英]Wildcard subdomains through .htaccess

這是我所擁有的:

每個狀態對應於數字:-1,-2,-3,-4等,它們也是$ row [countryid]

每個城市對應於數字:1、2、3、4等,它們也是$ row [cityid]

(例如:qwikad.com/-1或qwikad.com/1)

$ xcountryname表示州和城市的名稱,$ xcityname表示城市的名稱

我想將所有通過htaccess的鏈接轉換為更用戶友好的子域。

因此,qwikad.com / -1應該變成alabama.qwikad.com,而qwikad.com/1應該變成auburn.qwikad.com等,等等,等等。

有什么想法嗎?

您將需要指向服務器的所有子域,然后可以使用RewriteCond%{HTTP_HOST}上進行匹配,以確定是否應該進行重寫。 如果子域數量有限,則可以按照以下說明手動進行操作。

RewriteEngine On
RewriteBase /
# Does it start with "alabama", if so do the rewrite on the next line
RewriteCond %{HTTP_HOST} ^alabama
RewriteRule (.*) /-1/$1 [L]
# Does it start with "auburn", if so do the rewrite on the next line
RewriteCond %{HTTP_HOST} ^auburn
RewriteRule (.*) /1/$1 [L]
# ... and so on

對於大量重定向,您應該使用RewriteMap來保存重寫的ID /名稱關系。 這也為您提供了相當容易地以編程方式更新此文件的選項。 本示例使用([^\\.]*)\\. 匹配主機名中第一個之前的內容. 然后使用它從具有%1引用的文本字段中查找ID。

RewriteEngine On
RewriteBase /
RewriteMap subdomains txt:/path/to/file/map.txt
RewriteCond %{HTTP_HOST} ^([^\.]*)\.
RewriteRule (.*) /${subdomains:%1}/$1 [L]

/path/to/file/map.txt

#
# subdomain mappings
# comments start with a "#"
# key value pairs one per line
#
alabama   -1   # state
auburn     1   # city
something  2
else       3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM