CodeIgniter 2.1.4 - Routing don't work in server - .htaccess

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.

Related

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

.htaccess issue | Codeigniter

I'm relatively new to Codeigniter and MVC. But, have successfully made two apps 'Locally'. While exploring, I found a way to remove 'Index.php' from the URL and also about custom routes. The .htaccess file that i have works like charm locally, but when trying to host it; there is a issue 505 internal server issue
Here is the first .htaccess code that i have (works locally) :-
<IfModule mod_rewrite.c>
# Turn on URL rewriting
RewriteEngine On
# If your website begins from a folder e.g localhost/my_project then
# you have to change it to: RewriteBase /my_project/
# If your site begins from the root e.g. example.local/ then
# let it as it is
RewriteBase /
# Protect application and system files from being viewed when the index.php is missing
RewriteCond $1 ^(application|system|private|logs)
# Rewrite to index.php/access_denied/URL
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]
# Allow these directories and files to be displayed directly:
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|public|assets|css|js|images)
# No rewriting
RewriteRule ^(.*)$ - [PT,L]
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>
When i use the code above there is an error. And, the app works only when .htaccess is removed. (I then have to use the inconvenient long URLs)
After a brief research and using different .htaccess without success, i asked one of my friends who has a Hosted CI app successfully running. He sends me a file which leads me the landing page without any problem; but, cant call any functions with/without using routing .i.e. If i use the custom routed URl (www.mySite.com/contact) then also it leads me to the landing page, the same with actual URL scheme (www.mySite.com/welcome/contact_page)
The new code here:-
<IfModule mod_rewrite.c>
RewriteEngine On
# !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
# slashes.
# If your page resides at
# http://www.example.com/mypage/test1
# then use
# RewriteBase /mypage/test1/
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
Using this code shows me the landing page but i cannot navigate any further from there. When i try to call a function the landing page reloads. This, I think is because of the last error Handling Code (ErrorDocument 404 /index.php)
Does anyone know what the solution the problem might be??
questions
Why doesnt the first .htaccess code work when hosted?
What may be the issue with the second available .htaccess file??
Do you guys have better .htaccess file? If yes, can you post it here??
Try this .htaccess (Codeigniter Recommended )
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|image|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
or
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
In application/config/config.php
$config['base_url'] = 'http://stackoverflow.com/';
$config['index_page'] = ''; # Should be empty
And make sure Controller name, Model names are in proper way. Bcz Linux Host is an Case-Sensitive.

HTACCESS short url and modify my url

How can I use .htaccess to make this URL
http://domain.com/index.php/sub1/sub2/333
look like
http://domain.com/333
?
Thank you very much!
I tried everything and I tried to find everywhere the answer, but nothing works.
This is my htacces, maybe can help you ( I hosted in hostgator,in hostgator is importan write your user, if is local remove ) this configuratio apply to multiple sites
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /<myuser>/%{HTTP_HOST}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /<myuser>/%{HTTP_HOST}/index.php/$1 [L]
</IfModule>
You can replace %{HTTP_HOST} by your folder.
After if you want modify the controller name, you need modify in application/config/routes.php
Following rule should work for you:
RewriteEngine On
RewriteRule ^([0-9]+)/?$ /index.php/sub1/sub2/$1 [L]

.htaccess adding new file

<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /homepage/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /homepage/index.php [L]
</IfModule>
That is my .htaccess code. I have created new file in homepage/test.php and when i visit it from browser it give error of access,
i have tried by replacing index.php with test.php but it still not working.
Can some one explain me how this code work and why not my code is working.
i have deleted .htaccess but its still not working. What else file it can be.
Change
RewriteRule . /homepage/index.php [L]
to:
RewriteRule (.+) /homepage/index.php?page=$1 [L]
P.S. For this to work, your index.php needs to listen for $_GET['page'] and load the appropriate resource.
EDIT: without using the "?page=$1" your code will just display the index page for all requests that are not files or directories. I am not sure if that is what your were going for.

is if else conditions available on htaccess?

I have a .htaccess file in the folder "services" in my website
and have to give two urls following
content of htaccess
#------------------------
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ index.php?n=$1 [L,QSA]
</IfModule>
$-------------------------
1. http://www.mysite.com/services/seo-work
2. http://www.mysite.com/services/online-editor
The 1st URL rewritten using htaccess like http://www.mysite.com/services/index.php?s=seo-work
This is working correct. but when i am uploading other file online-editor.php on the same folder "services", it is not working
so how the first url should show the page using index.php?s=seo-work if the page "online-editor.php" is not found in directory otherwise online-editor.php shoud show. I am programatically representing my problem below
if(IsFoundPageInDirectory(online-editor.php))
{
GoToUrl(http://www.mysite.com/services/online-editor.php)
}
else
{
GoToUrl(http://www.mysite.com/services/index.php?s=seo-work)
}
Please help me to get out of this problem
Advance thanks for your reply
There is not really if/else conditions with ifModule in Apache configuration, but you can use the test in 2 cases : !module & module, like this:
<IfModule !module>
# default directives
</IfModule>
<IfModule module>
# decorator directives
</IfModule>
Check default wordpress .htaccess file. I think you're missing checks for existing files and directories. Something like
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
Last I found answer.. thanks for the fist reply to lead me to get answer
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?n=$1 [L,QSA]
</IfModule>

Resources