子域的URL重写

时间:2014-09-19 16:48:24

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

情况很简单。我在我的cpanel中创建了一个通配符子域,如下所示:* .mysite.com。

我可以加载页面,但我想将通配符的值作为GET的参数,这样页面实际上可以根据GET值显示相关内容。

所以我想知道的是,是否可以使用重写规则来允许我执行以下操作。

获取:$ _GET [' store'] ==' andystore'来自网址:http://andystore.mysite.com

2 个答案:

答案 0 :(得分:2)

您可以在DOCUMENT_ROOT/.htaccess文件中使用此代码:

RewriteEngine On
RewriteBase /

# capture first part of host name
RewriteCond %{HTTP_HOST} ^([^.]+)\.mysite\.com$ [NC]
# make sure store= query parameter isn't there
RewriteCond %{QUERY_STRING} !(^|&)store= [NC]
# rewrite to current URI?store=backererence #1 from host name
RewriteRule ^ %{REQUEST_URI}?store=%1 [L,QSA]

参考:Apache mod_rewrite Introduction

Apache mod_rewrite Technical Details

答案 1 :(得分:1)

RewriteCond %{HTTP_HOST} ^mysite.com$ [NC]
RewriteRule ^(.+)$ http://$1.mysite.com [L,R=301]