In Codeigniter some URIs pass through index.php, some don't - codeigniter

Basically one controller (controller/topics.php, http://192.168.1.50/topics) gets passed through CI's index.php like its suppose to, yet another (controller/user.php, http://192.168.1.50/user) for some odd reason simply doesn't pass (I get an 404 error in browser). CodeIgniter and/or Apache2 are doing something funky and I can't figure it out: '.../user' gives me Apache's 404 page, yet '.../User' gives me CI's 404 page which means CI grabs URIs with uppercase controller names and ignores some URIs with lowercase controller names (and then Apache tries to handle the URI).
Any ideas why and how to resolve?
P.S. - Yes, I did post my issue in CI's forum but I'm not having luck with their help. I'm running CodeIgniter 2.0.2 on a Linux distro (Ubuntu 10.10 with LAMP).

The problem was that the rewrite condition syntax is saying don’t route folders that BEGIN with i, c, j or u instead of folders whose name IS i, c, j or u. Coincidently my controller names which weren’t loading started with i (idea) and u (user).
I could just rename my static folders to their full names (“uploads” instead of “u”) and update the rewrite condition and resolve this issue, but I’d like to keep the names the same. Anyone happen to know the correct syntax for the rewrite condition to match exact names (instead of like or begins with)?

Check your controller name, the most common thing people (I find) do is copy their 'main' controller (say topics.php) and rename the file name to whatever.php
They start developing and the controller name stays 'Topics'. 404 is thrown due to the controller not being found (not the actual file)
ex:
class Blog extends CI_Controller {...
if the above code is in a file called stuff.php. You will get a 404 if you are calling /stuff

I ran into a similar problem yesterday. The default controller can have an uppercase file name but any other controller would produce a 404 when uppercase. This was on a MAMP setup.
Make sure your controller reflects the file name as well.
class User extends CI_Controller {

in your htaccess... instead of
RewriteRule ^(.*)$ /index.php/$1 [L]
try
RewriteRule ^(.*)$ index.php/$1 [L]
and in your CI config set base_url as instructed (include trailing /!)
$config['base_url'] = 'http://www.site.com/';
That should do it.

Related

How to remove id from url in laravel

I am trying to remove id from the url. My urls's are something like this,
https://www.example.com/p/29/web-test
But, I want the url like
https://www.example.com/p/web-test
And my route is,
Route::get('/p/{id}/{any}','TestController#dynamic_page')->name('dynamic.page');
I have tried several codes .htacces like
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ p/$1 [L]
But its not working, can anyone please help me out, I am poorly trapped in it,
Try this
Encrypt the $id in blade
{{ route('route_name', encrypt($id)) }}
And then decrypt id in crontroller.
$id = decrypt($id)
It will display encrypted id in URL.
You can't do what you are trying to achieve because if you remove the id from the url, using a .htaccess file or any other redirection method, the final route will still hit Laravel at the end.
If this route does not contain the required data (in your case, the id), it will simply not work.
The only way to "hide" the id from the url while still getting it on the server side (in Laravel) would be to change it after the page has been loaded, using javascript. But to be honnest it is ugly and I strongly discourage you of doing this.
The good news is, putting the id in the url is not a bad practice. Doing what has been proposed in the comments, https://www.example.com/p/web-test-29, is totally fine. Also, using a unique slug could also do the job (and this time, without the id).
It's all up to you, both these methods are technically OK and SEO friendly.
As a side note, since you mentioned it, I would advice against the use of .htaccess files whenever it is possible, since it'll bind your application to Apache and you might run into troubles if you want to switch to another web server later, like Nginx.

Silly 404 page usage?

Alright, so the only way I know of changing links to not show the file extensions;
this yourwebsite.com/customlink.php to yourwebsite.com/customlink
is either creating a folder called customlink and shoving an index file in there. Or creating a 404 page which snoops around the URL and grabs whatever string is behind the last / and does whatever to show the proper content.
My question is if this way of solving the problem is straight out idiotic, or not? I'm doing this because in my mind it saves space, let me explain: Every page that needs a customlink are the same with some bits and content taken from other places, so instead of creating X amount of folders and index files that includes a main file, I'll just have one 404 file that can handle it.
I apologize in advance if this is really stupid
As you are not mentioning what server your pages are running on (Apache, IIS, ...?), I'll just assume Apache.
Put an .htaccess file into the root of your site with the following content:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
It will internally rewrite everything without an extension to the corresponding php file, provided that:
the request is not a valid directory
a file with a php extension is in fact present
A more common approach is to route all HTTP requests to a single index.php using mod_rewrite in a .htaccess.
Then, based on the requested resource, the index.php outputs the appropriate file, whether it is generated from a database or nested in some other folder.
It's probably not a good idea to use a 404 page (an error page) for anything other than "File Not Found" instances.

laravel 5 removing public from url not working on WAMP

Many people post that following htaccess works for removing "public" from url. But i am working on WAMP and this is not working:
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
This shows following Error message from Laravel framework:
Sorry, the page you are looking for could not be found. 1/1 NotFoundHttpException in RouteCollection.php line 161:
There are other methods to remove "public" from url but I want to understand why this method is not working on my side which people followed frequently in their posts. my url is : http://localhost/laravel/ (directory: d:/wamp/www/laravel)
Can any one help me to understand why this popular method is not working on my side? Is their any issue in Laravel internal routing as I think my htaccess is working properly?
Thanks a lot in Advance.
I am sorry this is not exactly what you wanted but... Instead of rewriting the path, go to your Apache config and change your directory so it includes /public as well. That way you will get rid of the now necessary public from the URL. I know it is not an answer to your question, so please do not downvote. Nevertheless, it should solve your problem.
Your router is not picking up the redirected path because your laravel installation is a subdirectory within your webserver and the RewriteRule ^(.*)$ is picking up the whole path which includes /laravel. This is being appended to public/ because it's captured in $1.
The simplest case for you may be to alter the RewriteRule to not capture the laravel directory.
eg:
ReqriteRule ^laravel/(.*)$ public/$1 [L]
You may or may not need a forward slash ^/laravel/(.*)$ ...
Alternatively, (and more correctly) you should setup a virtual host within your apache configuration to serve the laravel application in the root of a virtual host (as it's designed) or you might be better off in the long run using Laravel Homestead as a preconfigured development environment.

CodeIgniter always and only shows default controller or 404 page

I am asked to study about CodeIgniter but I haven't even heard about it before, so I'm trying to go through the textbook and official tutorial. But I can't even successfully display a Hellow World...I've tried to search the solution for a while but none of them helps.
Here's the specific problem. I'm building a CodeIgniter environment with MAMP, PHP version is 5.6.2 and CodeIgniter version is 2.2.0. But no matter what I do, the site can only show me the default controller or 404 page.
First of all here's "hello.php" in controller folder:
<?php
class Blog extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
?>
I haven't set the mod_rewrite yet so I simply goes to this address:
localhost:8888/CodeIgniter/index.php/hello
And I only get a 404 page. So I tried to modify uri_protocol in config.php. I tried all of them, and QUERY_STRING and ORIG_PATH_INFO worked fine at first. I thought I succeeded. But I was wrong. I then setup a mod_rewrite with .htaccess file and I can only get a default controller (which is welcome page). I thought I must did something wrong in my .htaccess file so I tried to edit it but it doesn't work, so I tried to remove the file and go back to index.php/hello again, then I found that it can't work anymore, what I get is only a default welcome page.
I then tried to restart the server, modify index_page in config.php to 'index.php?', move CodeIgniter to root directory (I mean instead of localhost:8888/CodeIgniter/index.php, I removed CodeIgniter folder and just go to localhost:8888/index.php), totally remove the current CodeIgniter folder and download it again from official site, and of course tried other choices of uri_protocol. But nothing helps. What I get is still only default controller or a 404 page.
In summary, in config.php file, if I set
$config['index_page'] = 'index.php';
then
$config['uri_protocol'] = 'QUERY_STRING';
$config['uri_protocol'] = 'ORIG_PATH_INFO';
returns the default controller (even if I try to access a page which actually doesn't exist in controller folder), and other uri_protocols return 404 page.
Else, if I set
$config['index_page'] = 'index.php?';
then
$config['uri_protocol'] = 'PATH_INFO';
$config['uri_protocol'] = 'ORIG_PATH_INFO';
returns the default controller (also even if I try to access a page which doesn't exist in controller folder), and others return 404 page.
Also I thought it may be a problem with MAMP but even if I upload the whole site onto a VPS server (CentOS 6.5, php version 5.3.3) the totally same problem occurs.
Anybody has some idea?
First of all, to remove the index.php from your url, create a .htaccess file in your site root, and add the following code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
and then in your config/config.php file :
$config['base_url'] = '';
$config['index_page'] = '';
set the rest config parameters as usual.
Now, you are using the url localhost:8888/CodeIgniter/index.php/hello to access your application. This is wrong (unless you are routing the url). You have to load your controller which in turn loads the view for you. So, now use:
localhost:8888/CodeIgniter/blog
Again. One more thing. In the constructor of your Controller, you will have to call the parent constructor so that you can use loader class to load librariee, view, models whatever.
Now lastly, your best bet is to go through the documentation well. You have tried but not tried enough! Good Luck.
Umm well thanks for your help! A friend on Twitter told me that the class name was wrong, it should be the same as the php file (in this case, it should be class Hello extends CI_Controller)
After that, all things goes OK!
I was having this issue with CodeIgniter 1.7 when using it through the command line (CLI). I had to modify the _fetch_uri_string() function in the CI_URI class (system/libraries/URI.php) to see if $_SERVER['argv'][1] is set to find the URI being used. To find where your project's URI is present with your global $_SERVER variable, I would just do a var_dump($_SERVER); exit(); at the top of your index.php file.
Changes I made to the _fetch_uri_string() function highlighted:

Codeigniter: Routes only visible when entering /index segments

I'm working on my first project with codeigniter 2.0 and have a bit of a problem.
On my localhost (a MAMP installation) everything works fine with the routes. Only when i add a copy on my domain, change the base url and other necessary settings like my database settings it works fine for like 99%. I can't access my other controllers directly without adding the /index route. For example when i want to visit the http://my_domain.com/work it'll open the 404 error page but when i enter ttp://my_domain.com/work/index it works fine. Does someone know a setting i have to change for the online version? The Htaccess files are identical.
CHeers in advance.
I found my solution. I just checked the routes.php file in my config where i added a route for every controller. I just removed these and only my default_controller route is left. Now it works fine. Just a codeigniter newbie issue.
Thanks anyways for the help #Hibiscus and #BigFatBaby
I sorted my problem by setting the following line as my .htaccess file.
RewriteEngine On
RewriteRule ^.*$ index.php [NC,L]
It'll rewrite anything after the /index.php/blaha to /blaha.

Resources