3

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

Lawrence
  • 717
  • 1
  • 12
  • 25
  • Okay. You're talking about rewrite rules but the only code you've supplied is the `$_GET` method of PHP. Which is it? – Ohgodwhy Sep 19 '14 at 16:53

2 Answers2

2

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

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    Thanks. Worked! Sweet and easy. – Lawrence Sep 19 '14 at 16:56
  • Is it possible to just briefly explain so i and the rest of the noobs, who might have similar questions, viewing can understand the code a little? – Lawrence Sep 19 '14 at 17:25
  • I have same question, but using cakephp, I have tried your code but it redirected to other page. and show error message "Sorry. The website you are looking for is unavailable " http://stackoverflow.com/questions/26212025/url-rewrite-create-subdomain-in-cakephp – Pragnesh Chauhan Oct 13 '14 at 06:05
1
RewriteCond %{HTTP_HOST} ^mysite.com$ [NC]
RewriteRule ^(.+)$ http://$1.mysite.com [L,R=301]
Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38