How do I edit the index page of your domain in CodeIgniter? - codeigniter

So right now I have this in my .htaccess page:
RewriteEngine On
RewriteCond $1 !^(index\.php|images|js|fonts|css|robots\.txt)
RewriteRule ^(.+)$ index.php?$1 [L]
which makes it so that I can access http://www.yourdomain.com/index.php/page by going to http://www.yourdomain.com/page. However what I can't figure out is how to edit the page that shows up at http://www.yourdomain.com.
I've tried creating a controller called index, but this doesn't do anything unless you go to http://www.yourdomain.com/index
Can anyone help with this? Thank you!!!

The default controller that loads when there are no url segments is defined in application/config/routes.php:
From: http://codeigniter.com/user_guide/general/routing.html
Reserved Routes
$route['default_controller'] = 'welcome';
This route indicates which controller class should be loaded if the
URI contains no data, which will be the case when people load your
root URL. In the above example, the "welcome" class would be loaded.
You are encouraged to always have a default route otherwise a 404 page
will appear by default.
If there are no url segments besides the controller itself, by default the index() method of that controller is called (this is always the case, not just in regards to routing). So with this example you would look at the welcome controller and index method and see which view files etc. are being loaded.

Related

.htaccess redirect from image to article

im trying to redirect traffic i get from reddit to an article, but the image in the article is being hot-linked on reddit.com
this is fine but im wondering if theres a way that when users click on the image on reddit.com with a path like:
http://mysite.com/i/12345.jpg to redirect to http://mysite.com/r/12345
the only site I've noticed to do this successfully is livememe.com which has an image on reddit redirect to the article when clicked on. for example:
http://www.livememe.com/36opcf5.jpg redirects to http://www.livememe.com/36opcf5
and im trying to do something similar. Ive noticed that this redirect occurs whenever you go directly to that url.
Try this code :
RewriteEngine On
RewriteRule ^i/([a-zA-Z0-9]+)\.(jpe?g|JPE?G)$ /r/$1 [R=301]
I know it's been a few months since you asked, but if you still need a solution, I found an answer. Tried to find how it is done for a few hours, but unsuccessfully, so I had to come up with my own solution. Livememe doesn't do that redirect with htaccess, they do it with PHP. Since you can't see the difference in referrer, it would be the same whether the picture is embedded, or user clicks a link to it, you have to check something else. And the only difference I see is HTTP_ACCEPT header. If a resource is used as an image, it won't send HTTP_ACCEPT with a value of "text/html", it will be "image/jpeg" or something like that. So what you do is something like this: first, create .htaccess file with this:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /redirect.php?q=$1 [L]
So if a file is not found, redrect.php is loaded (wile your URL stays the same). And inside redirect.php put something like this:
$accept = explode(',', $_SERVER['HTTP_ACCEPT']);
$file = $_GET['q'];
$file_info = pathinfo( $file );
$url = $file_info['filename'];
$ext = $file_info['extension'];
$check_html = array_search('text/html', $accept);
if ( file_exists("$url.html") ) {
if ($check_html !== FALSE) header("location: http://yourdmain.com/$url"); // location of URL you want to redirect to
else header("location: http://i.yourdomain.com/$url.$ext"); // loction of image file
}
else {
header('HTTP/1.0 404 Not Found');
print 'File not found';
}
Adjust according to your server (the file_exists() part). Or you can check the database, to see that such post exists. Well, I think you get the idea, how that is done. Hope that helps.

How to load an index page without a controller

I have a question and apologies if it is too simple but I really couldn't figure it out. If I have my domain for example: www.shop.com. I changed .htaccess to omit the need for index.php. Now I am wondering how can I load for example a page (main home page) without having any controller in url address. E.g.:
www.shop.com =========> should land me on the home page of my site.
currently the only way I can do it is by defining a controller and doing this:
www.shop.com/controller/
Thanks and your help is much appreciated :)
Go to /application/config/routes.php
Add the default_controller rule like so
$route['default_controller'] = 'home';
So now in /application/controllers/home.php method index() will run on the index page.

codeigniter:everything created through MVC pattern?

I'm studying codeigniter and I would realize a simple application. I'm asking if every page, even if doesn't not contain directly dynamic element must be create through MVC pattern? I explain myself: my home page will not contain anything of dinamic. only an header, menu and footer. it needs to create model,controller and view to handle this situation or I create simple the home page?
You always have to create a controller because that is what is called from the url.
As far as the view and model. You don't always have to create either.
I've got plenty of pages with static info so I don't need any model interaction at all.
Without a view you are kind of defeating the purpose of the MVC. It is possible for the controller to just echo all your html for the page but I wouldn't do it.
The way I do it is that I have a default view that contains the header and footer. A content view that all my content for the page goes into. I then pump my view for the page into the content view then that into the default view to create my page.
$arrData["vwsContent"] = $this->load->view("your view for the page", $arrData, TRUE);
$arrData["vwsPageContent"] = $this->load->view("content template view", $arrData, TRUE);
$this->load->view("default template view", $arrData, FALSE);
In this way I can have different content views but the same default view for all the pages. For instance my homepage looks different than my regular pages so I would have a HOME template to use instead of a CONTENT template.
You can define the home page function in any controller.
In routes.php the default controller and action can be defined
$route['default_controller'] = "welcome"; (welcome can be replaced by any your prefer controller) .
Create function with name index
function index(){
$this->load->view('index');
}
Then create the file index.php in "views" folder.
In index.php you can put all your HTML static content. You can use URL helper [ function base_url()] for images/css/js path.

loading view in codeigniter

HI I have a codeigniter controller called CIcontroller and I have a method say redirectmethod
in the redirectmethod i have some code and then i do this
$data['redirect_page'] = 'page_name';
$this->load->view('template_view',$data);
the template view basically loads header footer and the corresponding view as specified by the data parameter
Now everything works fine but my url has value http:\\blabla\CIcontroller\redirectmethod instead of http:\\blabla\page_name
could anyone help me fix this thing
You need to emit a Location header to tell the browser to load a different page. See redirect in the url helper.

CodeIgniter Url Rewrite (language dependent)

So, i have my .htaccess, my controllers, everything is going fine. I added localization, so now i have Portuguese(Default), English and Italian.
I am using the _lang files in the appplication/languages directory, i am using session->userdata('lang') and everything works fine.
My controllers are named with portuguese words, after the top menu. What i'm looking for is:
to rewrite my url, changing the name of the controller, depending on the session->userdata('lang').
Is this even possible? how?
Thank you
So i am trying, as InFog suggested, in the routes file:
if ($this->session->userdata('lang') == 'english') {
$route['novidades/([a-z]+)'] = 'news/$1';
}
but i just get a blank screen when i open the application.
And i've tried it without the if clause, and nothing happens, when i go to
http://localhost/myapp/novidades
the url stays the same
You can solve this using CodeIgniter Routes. You can do it editing the file 'system/application/config/routes.php:
$route['news/([a-z]+)'] = 'noticias/$1';
This way an URL like '/news/run-fools' will be remaped to 'noticias/run-fools'. Now you can have just one controller =)
Good Luck
Override CI_Router to translate the name in the fetch_class() method to change controllers. Override fetch_method() to change methods.

Resources