简体   繁体   中英

Automatically redirect to sub-domain with htaccess

I am building a web application in which user is assigned a sub-domain,
Now somehow I am able write an .htaccess code which allow me to get sub-domain and also retrive the content from the database.

.htaccess code :

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^([w]{3,3}[.]{1,1}){0,1}kanai.in$
RewriteCond %{HTTP_HOST} ^([0-9a-zA-Z-]*)[.]kanai.in$
RewriteRule ^$ website.php?url=%1 [NC,L]

orginal link is kanai.in/website.php?url=test
and when I write test.kanai.in it is successfully giving the content which is stored in database,

What I want is that kanai.in/website.php?url=test should automatically redirect to test.kanai.in

You can try this code :

RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} url=(.*)
RewriteRule ^website.php$ http://%1.kanai.in? [L,R=301]
  1. We search for url value in query string.
  2. We check that we are on website.php page.
  3. We redirect to the value subdomain and remove query parameters by adding a ?

Those links might help you :

Guess what, I got the answer,
My htaccess code is correct,
All I have to do is just give it a href link as " http://test3.kanai.in ",
So it will redirect to test3.kanai.in and will get the corresponding data from the database.....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM