Redirect to my domain page - php

let me know how to redirect to main domain page in codeigniter. Like I want to Redirect my page to www.example.com from www.example.com/folder/controller/
Does anyone know?

First, get the url that the user is on
$url = $_SERVER['REQUEST_URI']; #www.example.com/folder/controller/
Second, you need to parse the url.
$parsedurl = parse_url($url);
Then, get the host.
$host = $parsedurl['host']; #www.example.com
Finally, redirect with redirect:
redirect($host);
I didn't test this, and it's pure php, which I assume will work, since I don't know anything about codeignighter
Edit 1: I changed header to redirect.
Edit 2: Forgot some semicolons ;)

Simply use redirect.
redirect('', 'refresh');
Also change
$config['index_page'] = 'index.php';
to
$config['index_page'] = '';
in application/config/config.php
Add .htaccess to remove index.php .
Refer official doc

Related

Redirect only if url has certain value

So I've moved my website from site.com to sub.site.com. I have also moved the booking system.
Is it possible for me to have PHP code on index.php on site.com to check if the user wanted site.com/unbook.php to automatically redirect them to sub.site.com/unbook.php
The optimal thing would be some kind of "redirect everything if there is anything in the url other than site.com"
Something like
$urlafter = explode("site.com", $url);
if (strlen ($urlafter[1]) > 0 ) {
header("Location: sub.site.com".$urlafter[1]);
exit();
}
Or would it be better using something like $_SERVER[REQUEST_URI] or even .htaccess
-- EDIT --
Non duplicate because they only show .htaccess answers, I'd prefer PHP
Using Htacces is better for the scenerio.
In the .htaccess file at the site.com you can set the following rule to redirect:
Redirect 301 / http://sub.site.com/
301 is for permanent redirect.
If the redirect is temporary, you can use 302 instead.
In Php you can use the following code to replace the domain:
$uri = $_SERVER['REQUEST_URI'];
$old_domain = 'yoursite.com';
$new_domain = 'sub.yoursite.com';
$old_domain_pos = strpos($url, $old_domain);
$redirect_url = substr($url, 0, $old_domain_pos).$new_domain.substr($url, ($old_domain_pos+strlen($old_domain)));
The above code replaces the domain only once, leaving everything else as it is.
If your url is like the following, where the domain may repeat in the querystring, which themselves need to be replaced.
http://www.yoursite.com/get/?assset=www.yoursite.com/imgs/pic01.png
then you can consider string_replace method.
$redirect_url = str_replace($old_domain, $new_domain, $url);

not getting proper base_url in codeigniter app

i am getting this type of url when trying to access pages other than index page
http://localhost:8012/health-care/8012//localhost/health-care/index.php/index.php/home
but url to access page must be
http://localhost:8012/health-care/index.php/home
my problem in url is: "it is duplicating url after "i hhttp://localhost:8012/healthcare/ that should not be duplicated.
i have autoloaded url helper in autoload.php as well as manually in constructor of controller. my base_url in conf
$config['base_url'] = 'http//localhost:8012/health-care/index.php/';
Guide me about this problem.
in addition to tell you that i have two app fron one codeigniter i.e. frontend and backend.
Url for backend works properly but for frontend its giving me mistakes what to do ?
$config['base_url'] = 'http//localhost:8012/health-care/';
used this base url you getting this url
http://localhost:8012/health-care/index.php/home
Y0u have to change this line
http://localhost:8012/health-care/index.php/home
to this
$config['base_url'] = 'http//localhost:8012/health-care/';
Please have a try.
Try this code:
$config['base_url'] = 'http://localhost:8012/health-care/';
and also make index_page as empty like this:
$config['index_page'] = '';
.htaccess file was not working. just replaced file and my problem got solved.

using $_GET without using '?'

I want to create a link which is catched by $_GET in PHP. Let me explain:
Currently i do this in HTML:
Contact Us
And this is what i do in php:
<?php
if (isset($_GET['contactus'])){
include "contactus.php";
}
?>
In this case the address becomes like this www.domain.com/?contactus.
But I want the address like this: www.domain.com/contactus
If i change html to Contact Us
but it doesn't work. Anyone please tell me. Thanks
you need a .htaccess file to make it work
like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^contactus\/?/?$ index.php?contactus
</IfModule>
and index.php like this
<?php
if (isset($_GET['contactus'])){
include "contactus.php";
}
?>
You can use domain.com/contactus and parse the URL
$url = "http://domain.com/contactus";
$url = parse_url($url);
$path = $url['path']; // contactus
echo $path; // contactus
You can then use it
if ($path == "contactus"){
include "contactus.php";
}
You will still need URL rewrite, try adding or editing your .htaccess file
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^contactus/?$ contactus-page.php [NC,L] # Handle requests for "contactus"
hi you need to do like this
at page where u set anchor tag do following
$url="contact.php?page"='contactus';
give href="$url" .
And other side you can use $urlget = $_GET['page'];
plz note the attribute u used is page so when u try to get in nother page u need to use $_GET['page'] and you can see url when you click on contact us page in your address bar there is also contact.php?page='contactus' showing

Change onarcade script url

I have a problem with Change url Script onarcade .
the code is
$categoryurl = $siteurl."/category/".$categoryId."/".$categoryId;
and in the .htaccess file
RewriteRule ^category/([0-9]+)/([_A-Za-z0-9-]+)/?$ browse.php?c=$1 [L]
the url like this
http://games.myarabvideos.com/category/1/1
I want the url become
http://games.myarabvideos.com/categoryNAME/1/1
$categoryNAME Variable on php code
example:
$categoryNAME=games;
i try this
$categoryurl = $siteurl."/category/".$categoryNAME."/".$categoryId;
but my problem in .htaccess file What Changes ??
The rewriterule you wrote down does not seem to be related to you desired redirect.
For what you need (and what I can understand), just replace:
$categoryurl = $siteurl."/category/".$categoryId."/".$categoryId;
with:
$categoryurl = $siteurl."/category/".$categoryNAME."/".$categoryId;
and that's all, in my opinion. Now you have to redirect all old ID urls to the new ones, one by one with single 301 redirect (no regex) i.e:
redirect 301 /category/1/1 http://yoursite.com/category/nameof1
redirect 301 /category/2/2 http://yoursite.com/category/nameof2
redirect 301 /category/3/3 http://yoursite.com/category/nameof3
...
and so on. Hope this helps ;-)

Codeigniter redirect doubles URL

I'm using codeigniter in localhost and I'm trying to redirect my controller to another controller, so what I basically do is:
redirect('/account/index');
However, it does not go to the URL, instead it goes here:
http://localhost/ticketsystem/index.php/logincheck/localhost/ticketsystem/index.php/account/index
It doubles my address. Do I need to set something in my config.php? My setup in my config.php is
$config['base_url'] = 'localhost/ticketsystem';
$config['index_page'] = 'index.php';
$config['uri_protocol'] = 'AUTO';
Do I need to change something there, or am I doing something else that makes my redirect cause problems?
The base_url behaving like relative path, change into complete url, and also add '/' in last
$config['base_url'] = 'localhost/ticketsystem';
To
$config['base_url'] = 'http://localhost/ticketsystem/';
your base url should be looks like
$config['base_url'] = 'http://localhost/ticketsystem/';
use redirect like :-
redirect('account/index', 'refresh');
refresh will use meta refresh and should quick redirect
try removing the '/' before account!
redirect('account/index');

Categories