Codeigniter mod-rewrite in apache, removing index.php - apache

So I am quite new to codeigniter and I have been trying to get my head around removing the index.php from my local site, I have managed to make it work for the homepage after looking through other similar questions on SO but none have got it fully working. Whenever I visit another page on my site it displays the wamp page on my screen. Here is my code in codeigniter/.htaccess
<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>
# 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>
I have also put in my apache/conf/httpd.conf
<Directory />
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
But still no luck, is there something I've missed? Thanks in advance

Follow the following step:
1. Change the config file like this:
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
2.Use the following .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Note: .htaccess vary depending on server. I some server (e.g.: Godaddy) need to use the following .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

Use this .htaccess works on most servers
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
# RewriteCond %{HTTPS} off
# RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
You can uncomment the last 2 lines to force HTTPS.
Also this could also work
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /foldername/
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>
Note line 3 of the 2nd .htaccess RewriteBase /foldername/ use this RewriteBase / if your script can be reached without including a folder name.

Related

Unable to remove index.php from Codeigniter 3.0 URL

I am unable to remove index.php from CodeIgnitor 3.0 URL. I have applied so many solutions but nothing worked. I am using WAMP 2.5 . rewrite_module is active in Apache . $config['index_page'] = ''. My htaccess file which is in the root folder is as follows
RewriteEngine on
RewriteBase /Reporting/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|js|img|css|captcha|robots\.txt)
RewriteRule ^(.*)$ /yourfolder/index.php/$1 [L]
Reporting is the folder in www where all the files are there.
I had problems with removing index.php with the default .htaccess code, but I managed and this is how my .htaccess looks like now.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
<Files "index.php">
AcceptPathInfo On
</Files>
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
Try with this code in .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
You have this in Codeigniter documentation - READ

Error in .htaccess file for removing index.php after upgrading to php v5.5

I am using codeigniter framework. I tried to remove index.php, it was success. For some reason I need to upgrade php version to v5.5. Then I copy pasted same code for .htaccess then its not working now. Throws an error like below:
.htaccess file has following code and its on root folder outside application.
<IfModule mysql_auth_module>
AuthType Basic
AuthName "MySQL Member Page"
Auth_MYSQLhost www.kaoscms.eo
## Auth_MYSQLusername grafikkaos_kaoscms
## Auth_MYSQLpassword gJcY3xju}:XP
## Auth_MYSQLgrp_table
Auth_MYSQLdatabase grafikkaos_kaoscms
Auth_MYSQLpwd_table ks_users
Auth_MYSQLuid_field username_usr
Auth_MYSQLpwd_field password_usr
## Auth_MYSQL_EncryptedPasswords off
require valid-user
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin/(.*)$ admin.php/$1 [L]
RewriteRule ^admin/$ admin.php [L]
RewriteRule ^admin$ admin.php [L]
#RewriteRule ^install/(.*)$ install.php/$1 [L]
#RewriteRule ^install/$ install.php [L]
#RewriteRule ^install$ install.php [L]
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^admin(.*)$ admin.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^install(.*)$ install.php [L]
</IfModule>
<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|content|css|js|images)
# No rewriting
RewriteRule ^(.*)$ - [PT,L]
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>
Try this,it will fix your issue.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L,QSA]

redirecting homepage without affecting other rules

My current .htaccess is given below.
# Make sure directory listing is disabled
Options +FollowSymLinks -Indexes
RewriteEngine on
# Send request via index.php (if it's not an existing file/folder)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_php5.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
I want to redirect my homepage to another domain, but maintain all the /sub/directories.
So changed the rules as below.
# Make sure directory listing is disabled
Options +FollowSymLinks -Indexes
RewriteEngine on
# Send request via index.php (if it's not an existing file/folder)
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#redirect
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://example.com/ [L,R=301]
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_php5.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Now all the /sub/directories are getting internel server error. Logs suggesting it's going on loops.
Any idea?
RewriteCond is only for the next RewriteRule.
Use that:
# Make sure directory listing is disabled
Options +FollowSymLinks -Indexes
RewriteEngine on
#redirect
RewriteRule ^$ http://example.com/ [L,R=301]
# Send request via index.php (if it's not an existing file/folder)
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_php5.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

simple Apache RewriteCond and RewriteRule issue

I'm trying to follow https://github.com/crwilliams/diary-user-interface/wiki/cachebusting
If I set my HTML code to:
href="/myDir/myFile.2013103000.html"
and add to .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(html)$ $1.$3 [L]
</IfModule>
I see "Page not found" error as the browser goes to: https://www.mycompany.com/myDir/myFile.2013103000.html
Any idea what could be happening?
What can I do to trouble shoot?
Sitting at the webpage with the link, if I change .htaccess to this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.+) https://www.mycompany.com/myDir/myFile.html [L]
</IfModule>
and then click the link it works (but obviously this just for troubleshooting).
UPDATE
Here's the complete .htaccess file in case something following the above can be identified as the problem:
# attempt to implement version control with cachebusting
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(html)$ $1.$3 [L]
</IfModule>
# Pruning irrelevant parts (Phil)
# force SSL and www
RewriteCond %{HTTP_HOST} ^mycompany\.com$ [OR]
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://www.mycompany.com/$1 [R=301,L]
# Redirect index.html to a specific subfolder;
Redirect permanent /index.html https://www.mycompany.com/home/
# enable pretty url
<IfModule mod_rewrite.c>
RewriteBase /home/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
I'd remove that last block and put it in a separate .htaccess file in your /home directory, eg
# /home/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
# not sure about the next 3 lines though
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>

SocialEngine - Redirects in .htaccess

I've completed a fresh install of SocialEngine PHP in my private server.
I also need now to install Moodle, and access in the following way to it:
http://www.example.com/moodle
SocialEngine creates its own .htaccess file, so every time I try to access the moodle directory, I got a "Page Not Found. The page you have attempted to access could not be found." because the redirection forces it to search moodle inside SocialEngine plugins, and I want it to redirect to a /moodle/ directory inside /httpdocs.
I am very noob with .htaccess options, I need a hint about what can I do.
The .htaccess content is the following:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
# Get rid of index.php
RewriteCond %{REQUEST_URI} /index\.php
RewriteRule (.*) index.php?rewrite=2 [L,QSA]
# Rewrite all directory-looking urls
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*) index.php?rewrite=1 [L,QSA]
# Try to route missing files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} public\/ [OR]
RewriteCond %{REQUEST_FILENAME} \.(jpg|gif|png|ico|flv|htm|html|php|css|js)$
RewriteRule . - [L]
# If the file doesn't exist, rewrite to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rewrite=1 [L,QSA]
</IfModule>
# sends requests /index.php/path/to/module/ to "index.php"
# AcceptPathInfo On
# #todo This may not be effective in some cases
FileETag Size
Any suggestion or tip, please?
Thank you very much in advance!
Regards.
I would exclude the moodle-folder in every rewrite and check if it works. After it is working I would remove it in every rule where you don't really need it.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
# Get rid of index.php
RewriteCond %{REQUEST_URI} !^/moodle
RewriteCond %{REQUEST_URI} /index\.php
RewriteRule (.*) index.php?rewrite=2 [L,QSA]
# Rewrite all directory-looking urls
RewriteCond %{REQUEST_URI} !^/moodle
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*) index.php?rewrite=1 [L,QSA]
# Try to route missing files
RewriteCond %{REQUEST_URI} !^/moodle
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} public\/ [OR]
RewriteCond %{REQUEST_FILENAME} \.(jpg|gif|png|ico|flv|htm|html|php|css|js)$
RewriteRule . - [L]
# If the file doesn't exist, rewrite to index
RewriteCond %{REQUEST_URI} !^/moodle
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rewrite=1 [L,QSA]
</IfModule>
# sends requests /index.php/path/to/module/ to "index.php"
# AcceptPathInfo On
# #todo This may not be effective in some cases
FileETag Size