Codeigniter redirect domain to subdomain - php

I am attempting to redirect example.com to subdomain.example.com using codeigniters (3) redirect($domain). But what I end up with in the browser address bar is base_url/$domain when all I want is $domain.
The controller is set to look at the url and determine if the user has visited without a subdomain. If that is the case they are redirected to a view that asks them where they are from. This data is then passed to the same controller with a $_POST telling it where they are from.
$this->load->helper('url');
$subdomain_arr = explode('.', $_SERVER['HTTP_HOST'], 2);
$subdomain = $subdomain_arr[0];
if ($_POST != NULL)
{
$post = $_POST;
$subnames = $this->sendy->subName($post);//returns subdomain
foreach ($subnames as $subname)
{
$sub = $subname->subdomain;
}
$subdomain = $sub;
$period = ".";
$domain = $subdomain.$period.$subdomain_arr[0].$period.$subdomain_arr[1];
redirect($domain, 'auto');
}
There are thousands of possible subdomains. I just need codeigniter to prepend the subdomain and reload the page so that the url appears in the browser address bar as subdomain.example.com.

Derp! I added http:// to the $domain var and now it works as expected. Sorry for the asshatery.
$subdomain_arr = explode('.', $_SERVER['HTTP_HOST'], 2);
$subdomain = $subdomain_arr[0];
if ($_POST != NULL)
{
$post = $_POST; //this is the library name
$subnames = $this->sendy->subName($post);
foreach ($subnames as $subname)
{
$sub = $subname->subdomain;
}
$subdomain = $sub;
$period = ".";
$http = "http://";
$domain = $http.$subdomain.$period.$subdomain_arr[0].$period.$subdomain_arr[1];
redirect($domain, 'location');
}
With the addition of the $http now the redirect works properly.

Related

Laravel redirect dynamic subdomains to folder

I creating CMS with function auto-creating subdomains
Example: user pressing button Create domain I adding subdomain name john to DB and creating folder domain.com/subdomains/john.domain.com now i need redirect this subdomain john.domain.com to directory domain.com/subdomains/john.domain.com
code web.php:
if (!function_exists('getSubDomain')) {
function getSubDomain($url)
{
$domain = $url = str_replace(Request::getRequestUri(),'', $url);
$url = str_replace('http:','',$url);
$url = str_replace('//','',$url);
$url = str_replace('www.','',$url);
$url = str_replace('domain.com','',$url);
$url = rtrim($url,'.');
$url = ltrim($url,'.');
if($url){
$user = ['john','barry','wells','cisco'];
if(isset(array_flip($user)[$url])){
return true;
}
}
return false;
}
}
Route::group(['domain' => '{sub}.domain.com'], function(){
if(getSubDomain(Request::url())){
//Here need redirect to folder.
echo "Subdomain";
exit;
}
});

Get domain from any php string

I am trying to get domain from inputting a url, or the domain itself (i.e "domain.com")
I tried using
function get_domain($url)
{
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
return $regs['domain'];
}
return false;
}
But that fails when inputting simply "domain.com"
I can't seem to figure this out.
if you ask parse_url with 'domain.com' it return it as file name
$pieces = parse_url($url); // 'path' => domain.com
Update
You can use such regex to take domain
^(https{0,1}:\/\/|)([^\/]+)(\/*.+|)$
it takes example.com from all lines below
http://example.com/dd.dfgjhdfg
https://example.com/dd.dfgjhdfg
http://example.com/
http://example.com
example.com/
example.com
Here is the working code:
$domainparse = parse_url($domainpost);
$domainpath = $domainparse['path'];
if ($domainpost == $domainpath) {
$domain = $domainpost;
}else {
$domain = get_domain($domainpost);
}
$domain = str_replace("www.", "", $domain);
Thank you splash58! I would have used your code if I had seen it in time

Adding a extra path after domain

If trying to add a extra path in URL after my domain in all urls
$extra = "en";
$domain = "http://domain.com";
$current = currentURL();
$rest_path = str_replace($domain,'',$current);
$result = $domain."/".$extra.$rest_path;
// $result is "http://domain.com/en/mysub/mysub"
After this so I redirect my site via using PHP redirect
To get current url is doing like..
function currentURL() {
$pageURL=(#$_SERVER["HTTPS"]=="on")?"https://":"http://";
if($_SERVER["SERVER_PORT"]!="80"){
$pageURL.=$_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}else{
$pageURL.=$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
It's look like many steps , Any easier to do that ? Or review my bad coding pls.
PS : Try to do without using .htaccess
I would use just:
$extra = "en";
$domain = "http://domain.com";
$result = $domain."/".$extra.$_SERVER['REQUEST_URI'];
since you are not using protocol or domain name.
$extra = 'en';
$domain = $_SERVER['SERVER_NAME'].'/'.$_SERVER['REQUEST_URI'];
$new_url=str_replace('.com','.com/'.$extra,$domain);
No?

How can I guess action name and module name from URL in symfony?

For example, I have a referer URL. I want to redirect to a page according to the previous page's module.
There is a code snippet on this page. To get module and action of the previous page, you will use the referer:
$referer = sfContext::getInstance()->getRequest()->getReferer();
// you have to check if the referer is not empty and is a page of your site
....
// then get the module and action:
$url = parse_url($referer);
$path = trim($url['path'], '/');
if (!sfConfig::get('sf_no_script_name') && $pos = strpos($path, '/') )
{
$path = substr($path, $pos+1);
}
$params = sfContext::getInstance()->getRouting()->findRoute('/'.$path);
$module = $params['parameters']['module'];
$action = $params['parameters']['action'];

Grabbing Refferer's URL - PHP

I am trying to grab statistics on my site and I need a way in PHP to assign just the referrer URL to a variable. All I want is just the "www" or subdomain, the domain, and the tld... I.E. www.facebook.com instead of http://www.facebook.com/BLAH-BLAH/?blah=blah
Try something like the following:
$referer = $_SERVER['HTTP_REFERER'];
$parts = parse_url($referer);
echo $parts['host']; //should echo example.com, or whatever
<?php
$ref = $_SERVER["HTTP_REFERER"];
$host = null;
if ($ref != null) {
$parse = parse_url($ref);
$host = $parse["host"];
}
?>
and then you have $host to work with

Categories