links in codeigniter - codeigniter

I'm experimenting with codeigniter but don't really understand how links work.
for example, I have a link like this:
localhost/ci/welcome/cat/7
My base url is localhost/ci, so by clicking on this link I would expect the method "cat" of controller "welcome" to be called.
This method is very simple:
function cat()
{
echo "just a test.";
}
Pretty basic - I would expect to see the text on screen, but I just see a 404 -page not found error.
What could be the problem?

http://localhost/ci/index.php/welcome/cat/7

Have you configured mod_rewrite (or URL rewriting in general) properly for CodeIgniter? It's not supported out of the box.
They have some instructions in their wiki.

I had problems with the rewrite rule suggested in the user guide. I was also getting 404s. I had to remove the slash before index.php in the rule.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
As Veeti says, also remember to enable mod_rewrite for Apache.

Related

Codeigniter HMVC without 'index.php'

I implemented the wiredesignz HVMC functionality in CI 2.1.7.
The only problem, is that, unlike the tutorial I followed(http://somethingstatic.com/setting-hmvc-codeigniter-2-1/), I have to add 'index.php' in my url:
how it should be according to the tutorial:
http://localhost/site/hmvc
What I actually have to call:
http://localhost/site/index.php/hmvc
How can I configure CI so the 'index.php' is not needed?
You need not to do anything extra. this link will help you to get this thing done. Just normal procedure. I done multiple times with the same one.
Hope this helps you.
//user following code in your .htaccess file to remove index.php from url.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ ./index.php/$1 [L]

Upload CodeIgniter project to cpanel

I have worked on that the whole day, but I couldn't find why I always get the CI 404 Page not found. The project works very well in localhost. What could be the problem?
This is my .htaccess
RewriteEngine on
RewriteCond $1 !^(index.php|css|js|jq|uploads|robots.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
I suspect the default controller can't be found.
What more do you think I should show you?
I will be grateful for your answers.
PD: I use CI 2.1.2
If you've set up everything right, then try changing your last line to: RewriteRule ^(.*)$ /index.php/?$1 [L]
EDIT
Since it's not working when you disable .htaccess and use index.php, you need to check your config.php for things like base_url, index_page, uri_protocol, etc... and routes.php and make sure the default controller is not capitalized (read more about the naming conventions, but I wrote you the general idea in the comments).

ModRewrite .htaccess / SEO URLs syntax

I am trying to re-use some .htaccess code on a new site/server and it doesn't work correctly. I'm not an expert with URL rewriting so would appreciate it if anyone can see if my syntax is incorrect or if there is something else I need to check server side.
I am using the following code:
Options +FollowSymLinks
RewriteEngine on
# News Pages
RewriteRule ^news/$ /news.php
RewriteRule ^news/(.*?)/$ /news.php?article=$1
It works for the 1st level, /news/ but /news/article-1/ just loads the /news/ (news.php) overview page. /news.php?article=article-1 works correctly.
Server is running Apache 2.2.9 and PHP is in CGI mode.
Try it such way:
RewriteRule ^news/(.*?)/?$ /news.php?article=$1 [L]
RewriteRule ^news/?$ /news.php [L]
[L] in the end of instruction stops server cheacking of other instructions, if current one match.
So you write more specific ahead of more general.
If your rules work correctly being the only one in the .htaccess, then such organisation of them can help.
/? - this means that / at the end can absent or present

Code Igniter URL routing confusion

I have a slight issue. I have configured my default controller as :
c$route['default_controller'] = 'news';
so when I point my browser to http://localhost/mysite all the news get loaded as instructed by the news controller.
Now on this page, I have given links to "read article" to read the full article. For this I have to point my browser to http://localhost/index.php/news/view/news-slug. I want to remove index.php from the url. I had already tried re-routing using wildcard without any success. Can you tell me how to do that?
There is a documentation on removing the index.php from the URL if you are using Apache with it's mod_rewrite:
Removing the index.php file
Using the following rewrite code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Also, there are many similar questions to yours, have a look at these questions from the search:
Search query on removing index.php in Codeigniter
You need to define your .htaccess file for this. Check this wiki: http://codeigniter.com/wiki/mod_rewrite

How to redirect url to another

OK...
I have setup things so that when the following page is requested (browser users and servers)
http://www.visualise.ca/?_escaped_fragment_=corona
the website returns the following the content of this (HTML snapshot)
http://www.visualise.ca/corona
Where 'corona' always change, it varies depending on the page the users or servers are requesting. It could also be
http://www.visualise.ca/?_escaped_fragment_=anne-au-cherry
redirecting to
http://www.visualise.ca/anne-au-cherry
Thanks
UPDATE: OK let me be more clear. I use AJAX to load my Wordpress post and they appear like this http://www.visualise.ca/#!/corona when loaded. But it's not crawlable by Google that request to serv them as http://www.visualise.ca/?_escaped_fragment_=corona so I modified Wordpress to do so. Now Google can crawl my page and index its content and accessing the HTML snapshot available at http://www.visualise.ca/corona.
The problem is that when I paste the http://www.visualise.ca/#!/corona link to facebook it seems to read the http://www.visualise.ca/?_escaped_fragment_=corona and is unable to read the content. But when I paste directly the http://www.visualise.ca/?_escaped_fragment_=corona link it works, it reads http://www.visualise.ca/corona (The HTML).
So I thought maybe if I could redirect http://www.visualise.ca/?_escaped_fragment_=corona to http://www.visualise.ca/corona it would solve my problem.
Here is the existing .htaccess file
#--- DH-PHP handlers ---
AddHandler fastcgi-script fcg fcgi fpl
AddHandler php-fastcgi .php
Action php-fastcgi /cgi-bin/dispatch.fcgi
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The common mistake that a lot of people do is trying to match whole URL including query string. The reality is: when matching URL, the pattern get applied to path part of it and query string has to be matched separately.
Use this rule: it will issue 301 Permanent Redirect from this kind of URL /?_escaped_fragment_=corona to /corona (where corona can be anything).
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=([^&]*)
RewriteRule .* http://%{HTTP_HOST}/%1? [R=301,L]
I guess your question is really "how do I do this kind of redirection?". So the answer is here:
There are three ways that I could think of, each with slight differences.
Doing the redirection server side
This basically means you set redirect headers in your response. In php, you could do this using the header function. It can also do a delayed redirect, in which case you need to worry about the contents of the page.
Doing it using client side using html's "http-equiv" meta tags. This way the page always gets loaded. Example here.
doing it via javascript. Thats you basic document.location.href thing. You need to figure out a way to pass the argument to javascript, or have your JS read it from the address url itself.
Since I've shown you 3 ways of doing this, I really hope that's what you're looking for :P
Update after seeing the comments:
The above methods will cause the URL to change. If you don't want the URL to change, but show the contents of that other page on your original page, you caould either do that using iframes (baaaad), or do the decent thing and set up URL rewriting.
:)
RewriteEngine on
RewriteRule \?_escaped_fragment_=(.*?) /$1 [L,R=301]
is it what are you looking for?

Resources