CodeIgniter Remove index.php with .htaccess - apache

I've been working on this for an hour already so I figured I might as well ask.
I am trying to remove the index.php from my CodeIgniter app's URL and can not do it.
The app is running on a dedicated server in my office and I access the app through the url http://smr_local
This is my base virtual host block
<VirtualHost 192.168.1.90:80>
ServerAdmin admin#server.com
DocumentRoot "/var/www/smr.dev/app"
ServerName smr_local
ErrorLog "/etc/httpd/logs/error_log"
CustomLog "/etc/httpd/logs/access_log" common
<Directory /var/www/smr.dev/app>
Order allow,deny
Allow from all
AllowOverride All
</Directory>
<IfModule mod_rewrite.c>
RewriteEngine on
</IfModule>
</VirtualHost>
Here is my .htaccess file
RewriteEngine on
RewriteBase /
# Hide the application and system directories by redirecting the request to index.php
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]
And my config file
$config['base_url'] = "http://smr_local/";
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
Right now when I'm trying to access my base url http://smr_local/user/courses I get an error in my apache error log I get this.
File does not exist: /var/www/smr.dev/app/user
I really don't know what to try next. Any help would be appreciated.

Have you checked official user guide?
example.com/index.php/news/article/my_article
You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the "negative" method in which everything is redirected except the specified items:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
In the above example, any HTTP request other than those for index.php, images, and robots.txt is treated as a request for your index.php file.
I have been working with Codeigniter previously and that is how I get it to work:
Here is my .htaccess (assuming you have default folder structure):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes access to the system folder by users.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
# Prevents user access to the application folder
RewriteCond %{REQUEST_URI} ^application.*
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>
ErrorDocument 404 /index.php
</IfModule>
That is relevant part of config.php:
$config['base_url'] = '';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
$config['url_suffix'] = '';
If this won't help, your problem likely in Apache configuration.
Then provide your httpd.conf and any other relevant configurations, not only virtual-hosts.

Related

Migrated website from http to https and now it's broken

I'm doing an internship and they asked me to fix a website that, after migrating from http to https, it's broken.
I'm becoming insane, it's been days and can't get it fixed... I don't know what to do, neither do the sys admins that asked me to fix it. They did other migrations with no problems.
I changed the urls in the code from http to https.
This is the .htaccess they did:
Options -Indexes
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
#When your application folder isn't in the system folder
RewriteCond %{REQUEST_URI} ^application.*
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>
ErrorDocument 404 /index.php
</IfModule>
This is what's inside /etc/apache2/sites-available/webpagename.conf
Options -Indexes
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
#When your application folder isn't in the system folder
RewriteCond %{REQUEST_URI} ^application.*
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>
ErrorDocument 404 /index.php
</IfModule>
And this inside /etc/apache2/sites-available/webpagename-le-ssl,conf
They told me they migrated it like this:
Linux command line:
certbot -apache
/etc/apache2/sites-available/webpagename.conf and wrote those lines.
a2ensite webpagename.conf
systemctl reload apache2
journalctl -xe
Any ideas? Anything else you need to know to be able to check the problem? The webpage it's PHP/Codeigniter.
Assuming you're using CodeIgniter3, as seen here How to force ssl in codeigniter? and following the documentation https://codeigniter.com/user_guide/general/urls.html :
You will need to enable hooks by adding to config/config.php file :
$config['enable_hooks'] = TRUE;
Then create a new file named hooks.php inside the config folder (i.e. application/config/hooks.php) and add the following code in it:
$hook['post_controller_constructor'][] = array(
'function' => 'redirect_ssl',
'filename' => 'ssl.php',
'filepath' => 'hooks'
);
Now create a new directory named hooks inside the application folder (i.e. application/hooks) and then create a new file named ssl.php inside the hooks folder (i.e. application/hooks/ssl.php).
Add the following code in the ssl.php file:
function redirect_ssl() {
$CI =& get_instance();
$class = $CI->router->fetch_class();
$exclude = array(); // add more controller name to exclude ssl.
if(!in_array($class,$exclude)) {
// redirecting to ssl.
$CI->config->config['base_url'] = str_replace('http://', 'https://', $CI->config->config['base_url']);
if ($_SERVER['SERVER_PORT'] != 443) redirect($CI->uri->uri_string());
} else {
// redirecting with no ssl.
$CI->config->config['base_url'] = str_replace('https://', 'http://', $CI->config->config['base_url']);
if ($_SERVER['SERVER_PORT'] == 443) redirect($CI->uri->uri_string());
}
}
Additionnaly you can set the base url to https in config/config.php :
$config['base_url'] = 'https://www.my-site.com/';
And finally, set the .htaccess file at the root folder :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
Now you can add each rule in .htaccess one by one to check your migration.

Remove index.php from URL of Laravel

I am using Laravel 5.5.12 in Linux Mint.I am using LAMP stack. I would like to remove index.php from URL. My mod_rewrite apache module enabled.
My .htaccess file located in public folder and it contains following code.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
Options +FollowSymLinks
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
I renamed the server.php in the Laravel root folder to index.php and copy the .htaccess file from /public directory to Laravel root folder. But it is not working.
I placed below code in .htaccess file
<IfModule mod_rewrite.c>
# Turn Off mod_dir Redirect For Existing Directories
DirectorySlash Off
# Rewrite For Public Folder
RewriteEngine on
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
But this not working.
Could anyone help me in this regard ?
Reset anything you have changed back to the default.
In your apache virtual host configuration, ensure you have the DocumentRoot correct (it will be something like /var/www/html/laravelproject/public).
You should not need to make any changes to the .htaccess file in the public folder as this handles rewriting the url to remove index.php. However, in some environments, I have had to add
RewriteBase /laravelproject
to the .htaccess in the public folder.
I found the answer here:
https://ma.ttias.be/remove-index-php-from-the-url-in-laravel/
To summarise, I added the following to the .htaccess file, directly below the existing index.php entry:
RewriteRule ^index.php/(.+) /$1 [R=301,L]
Go to httpd.conf file, search for the line DocumentRoot and change it to where ever your index.php is. For example:
DocumentRoot "C:/xampp/htdocs/myappfolder/public"

Can't get Anchor CMS rewriting to work

I'm currently attempting to install Anchor CMS on my Apache server.
I have successfully deployed it to the server. That is, when I go to www.domain.com, I am greeted with the default-themed front page and a test post :)
However, if I try to click the post, or go to the admin area, I get an error such as:
Not Found: The requested URL /posts/hello-world was not found on this server.
However, if I follow the (in this case) post link using a direct link, such as:
www.domain.com/index.php/posts/hello-world
It works completely fine. So, it appears to be a problem with the rewriting.
I found another problem on Stackoverflow exactly like mine, located here
Unfortunately, I did the same steps and nothing works. Just to recap, this is how my VirtualHost looks:
<VirtualHost *:80>
ServerName www.domain.com
ServerAlias domain.com
DocumentRoot /var/www/domain.com/public_html
<Directory /var/www/domain.com/public_html>
AllowOverride All
Require all granted
</Dictory>
</VirtualHost>
And this is how my Anchor CMS config.app file looks:
return array(
'url' => '/',
'index' => '',
...(rest of the data here)...
As the documentation states to use an empty index in this default case.
My .htaccess currently looks like this:
Options -indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
And it is located in the document root "public_html"
Sadly, even with these things configured, I still get the "Not Found" errors. I don't really know where to go from here :(
EDIT
Options -indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^ http://google.com [R,L]
RewriteBase /
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
I tried to put in a general rewrite to the .htaccess file, and it seems like it's not being executed:
It appears that the mod-rewrite is not being loaded.
See your server configuration file (httpd.conf). There should be a statement as follows:
LoadModule rewrite_module modules/mod_rewrite.so
If the line starts with #, remove the # character. You can also use the command:
a2enmod rewrite
Also, try to shift your Rewrites outside of the IfModule block.
Options -indexes
RewriteEngine On
RewriteBase /
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [L]

500 Internal Server Error in codeigniter

I am using CodeIgniter 3.
My .htaccess file contains the following.
I am getting 500 Internal Server Error .
I tried renaming the .htaccess file , i got 404 .
I want to see the Apache error log. but i only have one folder in my root directory, which is public_html where my project files are based at.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase http://www.xyzsolutions.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
in my config.php i have
$config['base_url'] = 'http://www.xyzsolutions.com/';
$config['index_page'] = ' ';
in database.php
'hostname' => 'http://www.xyzsolutions.com/',
I cannot find /var/log/httpd/error_log
how to get this , so that i can view the Apache error log ?
I solved the issue , changed my .htaccess to
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
In config.php
$config['base_url'] = 'http://www.xyzsolutions.com/';
And As i am using Codeigniter version 3
The Controllers and model files names should start with an upper case,
Eg:
models/User_model.php
controllers/Welcome.php
I named my files with lower case( starting letter )so i was getting 404.
Lower case thing worked in localhost , but in server it will not work
In your database.php change your hostname to 'localhost'
$config['hostname'] => 'localhost';
In your .htaccess change the RewriteBase to
RewriteBase /
Try also to leave the config['base_url'] empty
$config['base_url'] = "";

How can I remove "index.php" from my links?

Normally I access a page like this: http://localhost/codeigniter/index.php/auth/login
However I want to be able to access to the page like this : http://localhost/codeigniter/auth/login
I have no information about http.conf and .htaccess configurations.
I have set $config['index_page'] = ''; in codeigniter/application/config/config.php
my http.conf file is at its default except uncommentnig "LoadModule rewrite_module modules/mod_rewrite.so"
I inspected many proposed .htaccess files but I could not understand the logic behind the rewriting rules.
Here is the most successful .htaccess content for me. It is located at /www/CodeIgniter/ .
(by the most successful I mean that this is the only one that gives 404 error created by the CodeIgniter not by the server.)
RewriteOptions Inherit
Options -Indexes
Options +FollowSymLinks
RewriteBase /CodeIgniter/
# Set the default file for indexes
DirectoryIndex index.php
<IfModule mod_rewrite.c>
# activate URL rewriting
RewriteEngine on
# do not rewrite links to the documentation, assets and public files
RewriteCond $1 !^(images|assets|uploads|captcha)
# do not rewrite for php files in the document root, robots.txt or the maintenance page
RewriteCond $1 !^([^\..]+\.php|robots\.txt)
# but rewrite everything else
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.
ErrorDocument 404 index.php
</IfModule>
I hate requesting direct solutions however unfortunately, it is my last resort :/
Can you give me a right solution to my problem?
It's my repeat answer:
Codeigniter issues with paths in localhost (XAMPP)
please create .htaccess file in project folder and write:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
You don't need to define in base_url in config file:
$config['base_url'] = ''; // blank it.
put this on your htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
make sure the rewrite module is enabled on your server
This is the simplest one i found which works for me :-
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]