使用.htaccess为通配符域重写URL

时间:2016-12-23 09:58:23

标签: php .htaccess codeigniter mod-rewrite url-rewriting

我想用以下的通配符重写url:

  

https://any.example.com

致:

  

https://example.com/fixed/any

'any'是任何字符串,'fixed'是固定字符串。

感谢。

1 个答案:

答案 0 :(得分:0)

尝试在.htaccess文件中添加一些这样的重写规则:

RewriteEngine on
RewriteBase /

# Make sure it's not an actual file or 
# directory, being accessed. If you wish.
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d

# Match the subdomain
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$

# Rewrite the request to the main domain:
# %1: The subdomain matched against HTTP_HOST in the previous RewriteCond.
# $1: The request URI.
RewriteRule ^(.*)$ https://example.com/fixed/%1/$1 [L,R,QSA]

如果它不是最后一次重写规则,请随意删除L标志。有关详细信息,请参阅RewriteRule Flags

你可以test it online