CodeIgniter routes problem with controller's index function - .htaccess

My current development is using CodeIgniter, and when I try to pass some parameters to index function of my main controller it returns me 404 error.
http://localhost/gis/1 -> 404 ERROR
http://localhost/gis/home/1 -> 404 ERROR
Actually my route.php file has a route like this:
$route['default_controller'] = "home";
$route['home/([:num])'] = 'home/index/$1';
My .htaccess file is this (from this post to take away index.php from CI urls):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
And my config.php file:
$config['base_url'] = "http://localhost/gis";
$config['index_page'] = "";
What am I doing wrong? Thanks!

Update:
Quoting OP:
.htaccess wasn't guilty. The problem
was that my route needs to be
$route['home/(:num)'] = ... I added
both parenthesis and all works.
Rather than:
$route['home/([:num])'] = 'home/index/$1';
Try specifying it like this:
$route['home/:num'] = 'home/index/$1';
Also make sure that from application/config/routes.php, your default controller is set to home:
$route['default_controller'] = 'home';

Related

Redirect Old IDX home listing pages to Search Page

We are getting a lot of 404 errors on a Real Estate website after houses are sold and the listing goes offline. I am trying, via htaccess, to redirect the missing pages that Google Search Console shows as 404 to a home listing search page. I have tried the code below but it is redirecting all listing pages not just the ones that no longer exist. Not sure if it's my code or because the pages are dynamically created.
All the home listings are under www.example.com/homes-for-sale-details/[address]. If the listing no longer exists I want the page to redirect to www.example.com/homes-for-sale-details.
My htaccess code
# Redirect old home listing to a search page
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^homes-for-sale-details/(.*)$ https://www.example.com/homes-for-sale-details [L,NC,R=301]
</IfModule>
Where are I going wrong?
Many Thanks!
Edit (added more of htaccess code):
####################################
# START Redirect pages from old site
#
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^Properties https://www.example.com/home-listings [L,NC,NE,R=301]
RewriteRule ^Access https://www.example.com/search-homes [L,NC,NE,R=301]
RewriteRule ^Neighboorhoods https://www.example.com/neighborhoods [L,NC,NE,R=301]
RewriteRule ^Buyer-Resources https://www.example.com/buy-home-in-colorado-springs [L,NC,NE,R=301]
RewriteRule ^Relocation-Guide https://www.example.com/buy-home-in-colorado-springs/relocation-guide [L,NC,NE,R=301]
RewriteRule ^Buyer-Resources/Buyer-Finance/Finance-Information https://www.example.com/buy-home-in-colorado-springs/home-finance [L,NC,NE,R=301]
RewriteRule ^Seller-Resources https://www.example.com/sell-colorado-springs-home [L,NC,NE,R=301]
RewriteRule ^Area-Schools https://www.example.com/local-lifestyle/area-schools [L,NC,NE,R=301]
RewriteRule ^Colorado-Springs-Attractions https://www.example.com/local-lifestyle/colorado-springs-attractions [L,NC,NE,R=301]
RewriteRule ^Military-Bases https://www.example.com/local-lifestyle/military-bases [L,NC,NE,R=301]
RewriteRule ^About$ https://www.example.com/about-us [L,NC,NE,R=301]
RewriteRule ^contact$ https://www.example.com/contact-us [L,NC,NE,R=301]
RewriteRule ^Terms-Of-Service https://www.example.com/terms-of-service [L,NE,R=301]
RewriteRule ^Privacy-Policy https://www.example.com/privacy-policy [L,NE,R=301]
RewriteRule ^Site-Map https://www.example.com/sitemap [L,NC,NE,R=301]
RewriteRule ^neighborhoods/fountain$ https://www.example.com/neighborhoods/fountain-security-widefield [L,NC,NE,R=301]
RewriteRule ^neighborhoods/securitywidefield https://www.example.com/neighborhoods/fountain-security-widefield [L,NC,NE,R=301]
RewriteRule ^park-avenue-properties-blog https://www.example.com/blog [L,NC,NE,R=301]
RewriteRule ^Primary-Factors-the-Affect-the-Real-Estate-Market https://www.example.com/primary-factors-affect-real-estate-market [L,NC,NE,R=301]
</IfModule>
# END Redirect pages from old site
# Force HTTPS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
</IfModule>
# Remove "Blog" from blog post URLs and preserve blog paging
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !page
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^Blog/(.*)$ /$1 [L,NC,R=301]
</IfModule>
# Redirect old home listing to a search page
#<IfModule mod_rewrite.c>
# RewriteEngine On
# RewriteBase /
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteRule ^homes-for-sale-details/(.*)$ https://www.example.com/homes-for-sale-details [L,NC,R=301]
#</IfModule>
####################################
# Browser caching code removed :)
####################################
# 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
I've tried to get this to work with the .htaccess file and could not. The issue with your script above is that you're not checking first for the 404 error, so all requests from that "folder" are being redirected. I've tried using the ErrorDocument directive in the .htaccess file with no luck, so the solution I worked out was to just use what wordpress already provides. In your themes subfolder (/wp-content/themes/THEMENAME) there should be a 404.php file. If it's not there you can create it. You can use some code at the top of this file to parse the request url and redirect the page. Something like this should work:
<?php
if (strpos($_SERVER['REQUEST_URI'], 'homes-for-sale-details') !== false)
header('Location: /homes-for-sale-details', true, 301);
else {
?>
// ... paste the current 404.php content here
<?php } ?>
Just remember to back up your current 404.php page in case you overwrite something you didn't mean to. If you want to try to get this working with the ErrorDocument stuff in htaccess be my guest, but for whatever reason, it wasn't working for me. Here's a post that shows how to do that.
Good luck!
After unsuccessfully trying to find a htaccess answer slim's answer got me thinging and I found this post WordPress Template Redirect (thank you so much for your help slim!!!)
Here is the modification I made to that code.
// Redirect missing home listing to a search page.
function __404_template_redirect()
{
if( is_404() )
{
$req = $_SERVER['REQUEST_URI'];
if ( is_file( $req )) {
return; // don't reduce perf by redirecting files
}
// check if "homes-for-sale-details" is in the URL
if ( strpos($req, 'homes-for-sale-details') == false ) {
return; // only redirect missing homes
}
// pull the parent directory and convert to site url
$parent_url = get_permalink( 1232 );
// redirect to parent directory
wp_redirect( $parent_url, 301 );
exit();
}
}
add_action( 'template_redirect', '__404_template_redirect' );

codeigniter is not working after uploading in the server

My site http://www.bishal.prtia.com/prT_base/ throw an 404 error while loading in the server.
It works perfectly in the localhost. I tried changing everything that mentioned in this forum like changing .htaccess to web.config and changing base url routing config etc but it still throws an error.
Check the following things once again:
Make sure your base_url is proper as per your website address, not localhost
Check routes.php file for default controller and make sure it is correct.
Make sure .htaccess looks like
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|images|js|css|upload|favicon.png)
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(.*)$ ./index.php/$1 [L]
Then check following thigs:
Config.php
$config['base_url'] = 'http://www.bishal.prtia.com/prT_base/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
Routes.php
$route['default_controller'] = 'your controller name (like welcome)';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Make sure php file name of controller start with capital letter like Welcome and also inside that file as well when inherit from CI_Controller
Function inside Welcome.php:
public function index()
{
$this->load->view('your view file');
}
Thanks
You need to do few steps.
Update base_url in config.php
Update RewriteBase in .htaccess
Set Permissions
if still not workable, add error_reporting(E_ALL) in index.php and see what it says
I also faced this error. I changed my .htaccess file [WORKED FOR ME] :-
.htaccess for Live Server :-
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
.htaccess for Localhost :-
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php/$1

CodeIgniter 2.1.4 - Routing don't work in server

I know there are many issues like this but I applied everything and I don't find the correct way to do it.
The .htaccess is :
RewriteEngine On
RewriteBase /NewWebsite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /NewWebsite/error.php
</IfModule>
In config/config.php :
$config['base_url'] = 'https://mydomain.com/NewWebsite/';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO'; // I change it with REQUEST_URI and PATH_INFO but it don't work
In server, mod_rewrite is enabled.
In localhost, everything is ok but when i uploaded it to the server, only index works (the first page that you see) but when I want to "move" around the web, i have 404 error, page not found.
I am going to show one controller and how I call it to change to another views.
class Products_controller extends CI_Controller {
public function index()
{
$this->load->helper('url');
$this->load->helper('html');
$this->load->view('products.php');
}
}
And from index, I call it:
echo base_url('Products_controller/index');
OR
echo base_url('Products'); // I have in config/routes.php ---> $route['Products'] = "Products_controller/index";
Anyone can help me? What am I missing ?
Thanks.
Though .htaccess depends on hosting server sometimes but you can try with this and let us know. Replace your .htaccess code with the following code:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php?/$1
Thats it.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /NewWebsite/index.php [L]
</IfModule>
The above should work - make sure your .htaccess is inside the sub-directory you want to run from.
Also make sure that you don't have a conflicting .htaccess rule in the base directory.
And finally, I got a solution about this topic.It was not about a .htaccess problem, was a really dump and simple mistake.
In CodeIgniter, the name of the controllers and the name of the file
have to have exactly the same name. I have been defining my
controllers with the first letter capitalized and not in the file
name. And now it is everything ok !
Thank you guys.

CodeIgniter controllers are not loading and giving a 404 error

I am testing gocart (http://gocartdv.com/) and I have installed it in a subfolder called vine-cart, http://www.icas-v.org/vine-cart.
The default controller loads fine but the rest of the controllers return an error 404 message. I have looked through all the similar problems and the possible solutions, implemented them and they don't seem to work.
Please have a look at my configuration settings:
/---------------------------------|.htaccess file settings|---------------------------------/
RewriteEngine On
RewriteBase /vine-cart
### Canonicalize codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(cart(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
###
# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
/---------------------------------|Config.php settings|---------------------------------/
$config['base_url'] = 'http://www.icas-v.org/vine-cart/';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
/---------------------------------|routes file settings|---------------------------------/
$route['default_controller'] = "cart";
//this for the admininstration console
$route['admin'] = 'admin/dashboard';
$route['admin/media/(:any)'] = 'admin/media/$1';
Ive been trying to debug this for days. Please help.
Can you try this htaccess code? remove rewritebase.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /vine-cart/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /vine-cart/ [L]
</IfModule>
What did you name your controller file?
Also it's a good practice to comment out lines on your .htaccess as you test them 1 by 1 if its causing issues.

CodeIgniter Mod_Rewrite issues

Having issues removing index.php? from the URL:
http://localhost/index.php?/reset_password
With htaccess file:
# Customized error messages.
ErrorDocument 404 /index.php
# Set the default handler.
DirectoryIndex index.php
# Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
CI Settings:
$config['uri_protocol'] = 'AUTO';
I looked at the other threads on here with similar problems but none of their htaccess files work. Am I missing a setting?
I've never seen that .htaccess setup before, although it may work. I've used this for every CI project I've done, give it a try:
<IfModule mod_rewrite.c>
RewriteEngine on
# If the start of the URL doesn't match one of these...
RewriteCond $1 !^(index\.php|robots\.txt|assets|cache|themes|uploads)
# Route through index.php
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Add don't forget this in config.php:
/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = '';
And remember: This will not "remove" index.php from your URL automatically, but rather route the request through it if it isn't present. If you have hardcoded links with index.php in them, they'll have to be changed or you'll need additional .htaccess configuration. If you are using the CI url functions like base_url(), site_url(), and anchor(), they won't use index.php if you have it blank in your config above.
This code works for me:
If name of my site is codeIgniter_ci_intro
I used code in .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /codeIgniter_ci_intro//index.php/$1 [L]
and I changed in config.php
$config['index_page'] = '';

Resources