Removing index.php from a Codeigniter website - .htaccess

So, I've spend hours on this. I cannot figure it out. I've read several message boards and have not got it working.
Here's what I have done:
1)
Added a file called ".htaccess" to the folder "/www/site_name".
this file contains the following:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
2)
changed
$config['index_page'] = 'index.php';
to
$config['index_page'] = '';
3)
went through several httpd.conf files changing every possible combination of the lines:
AllowOverride None
to
AllowOverride All
4)
enabled the rewrite_module
This is driving me absolutely mad. I've spent literally hours on this.
EDIT:
Maybe i'm not setting the right AllowOverride to all. Which one is the right one?
EDIT:
I got it working. Thank you to the chosen answer for the help

Below is the .htaccess file which I use on my codeigniter installations. Perhaps you could try it out in an attempt to rule out the .htaccess file? If this works then we can add in your other rules if they're really necessary for your situation?
<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?baseurl=$1 [QSA,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
The lines below bulk cater for your requirement to allow directories like /images/ and files such as robots.txt etc. If the file or folder exists it wont be rewritten. The application and system directories are protected by the other rules though.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

Related

Subdomain issue with cakephp application

I have a cakephp application running on shared hosting server. Now I want to add a staging server for testing purpose. I defined a subdomain, and made changes in htaccess files, but these things don't work. Please suggest what should I change more.
I know similar questions have already asked on stackoverflow but nothing works for me.
sudomain name staging.example.com
Here root folder .htaccess code is mentioned here:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteRule ^$ app/webroot/ [L]
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
and .htaccess file from app/webroot from staging:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Is there any issue with such code or have to add more changes in another files.
Library is also installed in subdomain. Version of cake is 2.5.
This condition:
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
will not match your subdomain hence RewriteRule won't fire.
Just change your rule to this:
RewriteEngine on
RewriteRule .* app/webroot/$0 [L]

GoDaddy + CodeIgniter + subdomain

I'm attempting to remove index.php using the CodeIgniter framework (hosted with GoDaddy - Linux), which is currently installed to: example.com/ci
I've already declared the base_url in application/config/config.php as http://example.com/ci/.
The following is my latest test input for .htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>
Keep in mind that the domain CI is installed under is not my hosting root. The actual path for the CI folder would be: root/mydomain/ci
After spending the better part of the day today trying a plethora of "solutions", I'm beginning to wonder if this is possible to do with GoDaddy at all, or perhaps my situation is somehow unique.
Any advice would be greatly appreciated!
EDIT: The closest I've come to fixing this issue is using the following .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /clone/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^index.php/(.*)$ [L]
</IfModule>
.. while setting my base_url to mydomain.com/clone/ (using http:// of course) and removing index.php from index_page. Navigating to mydomain.com/clone works fine but navigating to the two pages that are part of the software results in a 404 page.
An answer to your comment, you need to add a rewrite condition to access the sites/ directory. CodeIgniter is looking for sites in the controllers folder.
RewriteCond $1 !^(index\.php|sites)
You can also add the names of your resource folders like images/video etc.
Change this
In your config.php
$config['base_url'] = '';
$config['index_page'] = '';
then in .htaccess
RewriteEngine on
Options -Indexes
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
or else you can use your .htaccess too
EDIT 01
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

CodeIgniter rewrite URL: catch 404's but ignore specific folder and file type

In my CodeIgniter application, I use a specific procedure to catch and handle 404's. That's the way I want it to be. I've come up with this .htaccess file to match my needs:
<IfModule mod_rewrite.c>
RewriteEngine On
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>
ErrorDocument 404 index.php
</IfModule>
Wich works great, except for one major problem. It redirects kind of.. everything. Say, for example, that my final output references to a .css or .js file that doesn't exists. This will cause my application to re run everything once again, when it actually doesn't need to.
My solution to this would be to ignore the static folder (root of my project -- same folder as index.php), and every .css/.js file. However, I don't really know how to implement this, so any directions or suggestions that'd point me in the right direction would be highly appreciated.
Add another condition:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(static|(.*)\.css|(.*)\.js)
RewriteRule ^(.*)$ index.php?/$1 [L]
Adding this line tells .htaccess to not process static directory contents.

mod_rewrite sometimes turns off?

I am using mod_rewrite in my .htaccess files to make cleaner looking URLs. I have two directories (subdir1, subdir2) that are folders created inside the root directory. They are not sub-domains. The .htaccess files reside in the three directories: root, subdir1 and subdir2.
The following .htaccess file resides in the root directory:
Options All -Indexes
ErrorDocument 404 /404.php
FileETag MTime Size
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(subdir1|subdir2)(/.*)?$ [NC]
RewriteCond %{REQUEST_URI} ^(.*)?$ [NC]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ /subdir1/ [R=301,QSA,L]
RewriteRule ^(.*)(\.html|\.htm)$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php [L]
</IfModule>
and this one is in the subdir1 directory:
Options All -Indexes
ErrorDocument 404 /404.php
FileETag MTime Size
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subdir1/
RewriteRule ^(.*)(\.html|\.htm)$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php [L]
</IfModule>
Everything is working fine. I can browse the www.mydomain.com/subdir1/1234-myaritcle.html page and read its content, but sometimes the rewrite module turns off without creating the clean URLs and the article opens with the following url: www.mydomain.com/index.php?article=1234
Can you please help? What is the cause of the problem?
Is there something wrong with the .htaccess files?
Nothing in your rules is causing the url to appear as www.mydomain.com/index.php?article=1234. Whatever link you are opening the article with is probably not generating the clean URLs.

when removing index.php from url ( codeigniter ) .htaccess Prevent the files from the display

I'm using codeigniter, and I tried to remove index.php from url by creating .htaccess file
and it contents:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
this way is working will at xampp server.
but when I uploaded the site, unfortunately there was a problem appeared:
I have a folder called files and it content sub folders: images, css, js, swf, upload.
and every file at those folders can't view, and the browser said there the file not found.
any help ploese.
Just make sure you exclude those too:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|css|swf|upload)
RewriteRule ^(.*)$ /index.php/$1 [L]
This is my .htaccess file. My understanding is that for any file or directory which has access attempted and doesn't exist, it will forward onto Codeigniter.
Anything which does exist will work fine and will not require individual exclusion. This will save time and hassel each time you add a new directory or folder as it wont require you to edit this file.
RewriteEngine on
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Works for us, but my understanding may well be incorrect. Hope it helps
try this one which i think will solve your problem ..
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|files\/images|files\/css|files\/js|files\/swf|files\/upload)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
you have to tell your server not to handle those directories over to index.php and the line which work for this is :
RewriteCond $1 !^(index\.php|robots\.txt|files\/images|files\/css|files\/js|files\/swf|files\/upload)
Here is my .htaccess file - which should work for you:
Options -Indexes
Options +FollowSymLinks
# 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 !^(files|css|js|swfimages|assets|uploads|captcha)
# do not rewrite for php files in the document root, robots.txt or the maintenance page
RewriteCond $1 !^([^\..]+\.php|robots\.txt|crossdomain\.xml|maintenance\.html)
# 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>
thank you. this worked fine:
RewriteEngine on
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
I have another website and next index.php I have a folder called files, and this one content alot of folders should I have to put all names of them and names of sub-directories in them?

Resources