URL rewrite for Subdomain

一个人想着一个人 提交于 2020-01-04 05:18:28

问题


The situation is simple. I create a wildcard subdomain in my cpanel that goes something like this: *.mysite.com.

I can load the page but I want to take the value of the wildcard as a parameter of GET so the page can actually display the relevant contents based on that GET value.

So what i want to know is if its possible to have a rewrite rule that allows me to do the following.

Get: $_GET['store']=='andystore' from the url: http://andystore.mysite.com


回答1:


You can use this code in your DOCUMENT_ROOT/.htaccess file:

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]

Reference: Apache mod_rewrite Introduction

Apache mod_rewrite Technical Details




回答2:


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


来源:https://stackoverflow.com/questions/25938781/url-rewrite-for-subdomain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!