Codeigniter - removing the index.php - .htaccess

I've been trying for hours and could not make it work. Searched for answers everywhere, and nothing seems to solve my problem.
I even tried the solutions described here: Remove index.php in codeigniter 2.1.0. No luck at all.
I have set the config['index_page'] as ''.
My .htaccess is on the root folder (where the sys and app folders are) and is written like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Still, the only way I can access my controller methods is with the goddamn index.php in the url.
And yes, my apache conf has mod rewrite enabled.
ANY HELP would be incredibly nice.
EDIT
I´ll add some more info, maybe it's relevant.
I´m using ZendServer.
I´m working at localhost by now.

RewriteEngine On
RewriteBase /yourdirectoryname/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/ [L]
I have no experience on ZendServer, but tonight I also got this problem on localhost using xampp (without ZendServer), and finally got it worked :
check your httpd.conf, and make sure "LoadModule rewrite_module modules/mod_rewrite.so" line is not commented.
open config.php, set $config['base_url'] = 'http://localhost/yourdirectoryname/'
still in config.php, set $config['index_page'] = ''
create .htaccess file on the same directory with your index.php (in my case, htdocs/yourdirectoryname)
the content of that .htaccess file is as above

I have no experience with code igniter, but if I understand correctly you are trying to serve something like /index.php/my-page to the user, without the need for him to prepend index.php to it?
If I'm correct, you could try something like this:
RewriteBase / # make sure we always rewrite from within the root directory
RewriteCond %{REQUEST_FILENAME} !-f # make sure the request is not to an existing file
RewriteCond %{REQUEST_FILENAME} !-d # nor an existing directory
RewriteCond %{REQUEST_FILENAME} !^index\.php # make sure we do only interpret requests that DO NOT start with index.php
RewriteRule ^(.*)$ index.php/$1 [L,QSA] # interpret request as if it were to index.php/...
# make sure we do only interpret requests that DO start with index.php
RewriteRule ^index\.php/(.*)$ $1 [L,R,QSA] # redirect user to new URL, which will be handled by the first rewriterule
Keep me posted whether this works or not

try this one, i've used this on many hosts anywhere , check:
RewriteEngine On
RewriteBase /yoursitedirectory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
if is not enought then go to config.php file and search for this line:
|--------------------------------------------------------------------------
| URI PROTOCOL
|--------------------------------------------------------------------------
|
| This item determines which server global should be used to retrieve the
| URI string. The default setting of 'AUTO' works for most servers.
| If your links do not seem to work, try one of the other delicious flavors:
|
| 'AUTO' Default - auto detects
| 'PATH_INFO' Uses the PATH_INFO
| 'QUERY_STRING' Uses the QUERY_STRING
| 'REQUEST_URI' Uses the REQUEST_URI
| 'ORIG_PATH_INFO' Uses the ORIG_PATH_INFO
|
*/
$config['uri_protocol'] = 'AUTO';
i use AUTO but you maybe need to switch that to REQUEST_URI or QUERY_STRING, try

I finally managed to work this out. Thanks for all the answers, special thanks to #ddanurwenda. His rewrite rules were the only ones that solved my problem.
There was an Apache config that I had overlooked: I changed this line (from httpd.conf):
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>
To the following:
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
Satisfy all
</Directory>
That configuration were keeping my htaccess file from being read.

This is what I use and works.
Use this code in your .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* website/index.php/$0 [PT,L]
Just make sure the .htaccess is in your root folder. (ie: xampp/htdocs)
Then in your config.php(application\config) replace
config['index_page']='index.php'
to
config['index_page']=''.

Related

Exclude php from url in get request

Is it possible to exclude the .php from URL?
Currently i got this in my .htaccess to remove php from urls:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
But whenever i try to do
dashboard?test=lol
It doesn't work, but if i do
dashboard.php?test=lol
then it does work.
I've seen other sites being able to do it without the .php in url... so how?
Thanks.
Have you restarted the webserver?
You also need to enable AllowOverride in Apache configuration. Something similar to this:
AllowOverride All
With your shown samples, try following. Please make sure to clear your browser cache before testing of URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?(\S+)\s [NC]
RewriteRule ^ /%1?%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ $1.php [QSA,L]
Just add this line in your site root .htaccess to enable content negotiation:
Options +MultiViews
Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will serve /file.html.

Codeigniter URL rewriting .htaccess is not working on CentOS

I've created a .htaccess file in my document root at /var/www/html/ to rewrite URL of Codeigniter to remove "index.php" from URL of all pages.
e.g. Change URL from
http://myhost/index.php/controller/function
to
http://myhost/controller/function`
Here is the code of my `/var/www/html/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I've got many suggestions from googling to enable mod-rewrite module, but I can see in my httpd.conf its already enabled.
LoadModule rewrite_module modules/mod_rewrite.so
This is perfectly working on my local system running on Debian 7 (Apache/2.4.4).
Any help would be appreciated. :)
You need to also specify the locations that can use it. For example, in /etc/httpd/conf/httpd.conf you should see something like:
<Directory "/var/www/html">
...lots of text...
</Directory>
Make sure is has:
<Directory "/var/www/html">
AllowOverride All
</Directory>
It's possible that your centos server isn't setup to handle PATH INFO. Try replacing the index.php/$1 in your rule's target to index.php?/$1.
Then you need to modify your CI config to enable query strings: http://ellislab.com/codeigniter/user-guide/general/urls.html
In you application/config.php file set $config['enable_query_strings'] to TRUE.
# CentOS Linux release 7.9.2009 (Core) -
# CodeIgniter 3.1.11 - ( .htaccess - This code work for me )
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

url redirection in the default .htaccess file

I would like to map "http://www.example.com/abc" to "http://www.example.com/test/abc" for having the shortest route possible. I am using pyroCMS for my users and content.
the default pyrocms file:
# Multiple Environment config
# Set this to development, staging or production
# SetEnv PYRO_ENV production
<IfModule mod_rewrite.c>
# Make sure directory listing is disabled
Options +FollowSymLinks -Indexes
# disable the Apache MultiViews directive if it is enabled on the server. It plays havoc with URL rewriting
Options -MultiViews
RewriteEngine on
# Keep people out of codeigniter directory and Git/Mercurial data
RedirectMatch 403 ^/.*/(vendor|composer\.json|composer\.lock|system/cms/cache|system/codeigniter|system/cms/config|system/cms/logs|\.git|\.hg).*$
# Send request via index.php (again, not if its a real file or 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>
</IfModule>
I would like to add this rule to the file:
RewriteRule ^([a-z0-9_]+)$ test/$1
To rewrite "BASE_URL/abc" to "BASE_URL/test/abc"
However,
i tried many positions as to where to put this RewriteRule, my website keeps giving a "Page Missing".
Is my RewriteRule ok? And where do i insert it?
PyroCMS has built-in modular routing ability. see here:
http://docs.pyrocms.com/2.2/manual/developers/basics/modular-routing
If your "http://www.example.com/abc" refers to a custom module,then, you can add a file named "routes.php" in a in config folder of your module.
the folder construction should like this :
addones/shared_addons/modules/your-module/config/routes.php
OR even you can edit the core route config file located at system/cms/config/routes.php and add this line or whatever your routing rules are:
$route['abd'] = 'test/abd';
OR more even, at your control pannel there is a redirect module that you can add redirections.
It all depends on what your rule is supposed to do and how it is supposed to interact (or not interact) with the rest of your site. And considering your entire htaccess file is mostly commented out code (which I removed to make it halfway readable), you just want to place it under RewriteEngine On.
However, since it blindly routes everything into test you need to add a few conditions and make it something lilke:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/test%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/test%{REQUEST_URI} -d
RewriteRule ^([a-z0-9_]+)$ test/$1 [L]

Unable to Remove index.php on ExpressionEngine WAMP Installation

I have WAMP v2.2 installed on my Windows 7 machine, with virtual hosts enabled.
I've successfully installed ExpressionEngine for one of my local sites, and everything works great except when I try to remove index.php from the URL using the approved .htaccess method. I still get a 404 error if index.php is not present, but the page displays fine with index.php in the URL.
I made sure the rewrite module was checked in the Apache menu:
. I've successfully used this method dozens of times on commercial hosts, so I'm stumped.
Update for pvledoux:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# If 404s, "No Input File" or every URL returns the same thing
# make it /index.php?/$1 above (add the question mark)
</IfModule>
Note: I tried adding the ? into the rule, but it didn't work.
The site is at the root of the Virtual Host.
Got It!
I needed to add AllowOverride in httpd-vhosts.conf:
<Directory "C:\Site\Root\Path">
AllowOverride All
Order Deny,Allow
Allow from all
</Directory>
A gagillion thanks and a 6-pack of beer goes to #parhamr for helping me out of this pickle.
A few possibilities here you could try out here:
[...]
RewriteEngine On
Options FollowSymLinks
[...]
Or:
[...]
RewriteEngine On
Options +MultiViews
[...]
Or:
[...]
RewriteEngine On
Options FollowSymLinks
Options +MultiViews
[...]
From my experience, Apache when running on Windows often needs one or both of those in order to do these kinds of things correctly. It might also straight up 500 on you, but these have worked for me in the past.
try to add RewriteBase /
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# If 404s, "No Input File" or every URL returns the same thing
# make it /index.php?/$1 above (add the question mark)
</IfModule>
and check for sure that mod_rewrite is active in phpinfo
Have you checked your default document in Apache? If you are removing index.php physically from the machine and it reports a 404 on just a directory, www.domain.com/home/ that means Apache is looking for index.php still.
Is mod_rewrite turned on in the Apache?

.htaccess not working on live site but working on localhost?

I am using ExpressionEngine and want to remove index.php from my URL's. I have this .htaccess file saved in the root folder. It works perfectly on localhost but when I upload it to the server it doesn't work. The correct URL appears in the address bar but the page stays on homepage. Any tips?
<IfModule mod_rewrite.c>
# Enable Rewrite Engine
# ------------------------------
RewriteEngine On
RewriteBase /
# Redirect index.php Requests
# ------------------------------
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteCond %{THE_REQUEST} ^GET
RewriteRule ^index\.php(.+) $1 [R=301,L]
# Standard ExpressionEngine Rewrite
# ------------------------------
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
Make sure your AllowOverride Directive in Apache is configured to allow .htaccess files and your server has mod_rewrite installed and active.
On Mac OS X, you'll find this file at /etc/apache2/httpd.conf. Look for the <Directory> Directive and change it to be:
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
</Directory>
You'll need to restart Apache so it can read the new configuration:
sudo /usr/sbin/apachectl restart
If you'd prefer to use a GUI to restart Apache, go to Apple > System Preferences > Sharing and toggle the checkbox next to the Web Sharing service.
If you're using Windows or any flavor of Linux, the same approach applies, but the Apache configuration may be in a different place, especially if you're using WAMP or MAMP.
Also, for reference, the "officially supported method" by EllisLab for Removing index.php from ExpressionEngine URLs is the following:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# If 404s, "No Input File" or every URL returns the same thing
# make it /index.php?/$1 above (add the question mark)
</IfModule>

Resources