htaccess RewriteRule doesn't work properly - php

I have this code in my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?link=$1 [L]
</IfModule>
And I am getting 404 although I can acces it with non-pretty url like this index.php?link=somepage
My full url is http://www.website.com/subfolder/index.php?link=somepage
I have Apache 2.4.18 on Ubuntu 16 and mod_rewrite module is loaded.
Update
Also when I try with this:
RewriteRule ^ index.php [L]
Then I can access it with http://www.website.com/subfolder/?link=somepage
So this way it works when just removing index.php
Is it possible that some additional configuration has to be set on server?

This was a server issue including mime types and multiviews.
It's not really issue for stackoverflow but if someone runs into same problems here are sources that solved this problem:
http://www.bennadel.com/blog/2218-negotiation-discovered-file-s-matching-request-none-could-be-negotiated.htm - remove MultiViews from Virtual Host configuration
https://serverfault.com/questions/372733/apache-file-negotiation-failed - add missing mime types

Try :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/index.php?link=$1 [L]

Try with this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ ./index.php?link=$1

First of all you should check your apache config
sudo vi /etc/apache2/apache2.conf
Search for
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Change AllowOverride None to AllowOverride All
enable htaccess module by running cmd
sudo a2enmod rewrite
then restart your server
sudo service apache2 restart
and use this .htaccess file in your project folder
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
Hope it works for u .. :)

Related

How to remove index.php from URL in CodeIgniter using .htaccess

I know this question is asked many times before and I've gone through most of them already but couldn't find a solution. I've tried most of the answers given here, here and in CodeIgniter official documentation. And restarted apache each time. It didn't work for me. mod_rewrite is already enabled in my apache server.
Weird thing is, I've a working example for WAMP (in my laptop):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /project/index.php
</IfModule>
It works perfectly in WAMP, but it doesn't work in LAMP. Any kind of help is appreciated.
Please use below code:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
I have used this and it is working.
Try Below code in your .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ROOT DIRECTORY NAME/
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<Directory "/path/to/document/root/">
AllowOverride All
Allow from All
</Directory>
Just like this......
.htaccess inyour root directory with following code..
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Set configuration in application/config.php as follows..
$config['base_url'] = 'http://domain_name';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
I made it working with the help of following .htaccess code:
RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
And I updated <Directory /var/www/> section of /etc/apache2/apache2.conf file with:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
</Directory>
It solved all problems and now it works fine.
here it is my approach:
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
And also in config.php replaces
$config['index_page'] = "index.php"
to
$config['index_page'] = ""
and also in the same file replace
$config['uri_protocol'] ="AUTO"
by
$config['uri_protocol'] = "REQUEST_URI"

Remove index.php from url in codeigniter-3 in ubantu14.04

I have Ubantu 14.04 machine also I checked Link
My phpinfo() says rewrite_module is Enable also I have done changes into config.php
$config['index_page'] = '';
and below is my .htaccess file on root
RewriteEngine On
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Try this one
Options +FollowSymLinks
IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
# Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
I got solution just added
<Directory /var/www/html >
Order allow,deny
AllowOverride All
Allow from all
Require all granted
</Directory>
into file /etc/apache2/sites-available/000-default.conf and it's working fine

Installing codeigniter on subdomain but it is giving 500 internal server error

I have installed the codeigniter on subdomain like http://abc.domain.com.
I have modified my htaccess file but it is giving 500 internal error. Please
tell me required changes to be done in htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /abc
RewriteCond %{REQUEST_URI} ^system.*
RewriteCond $1 !^(index\.php|images|js|uploads|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Change the RewriteBase to / only and add base_url to your config file like,
# in htaccess change the RewriteBase
RewriteBase /
in application/config/config.php
$config['base_url'] = 'http://abc.domain.com/';
Also make sure that your htaccess file should be in your abc folder, not on the root www
Firstly I hope you have configured your virtual host properly.
For example:
<VirtualHost *:80>
ServerName abc.domain.com
DocumentRoot /var/www/html/abc/or/whatever/
</VirtualHost>
And your Directory directive
<Directory /var/www/html/abc/or/whatever/>
AllowOverride all
</Directory>
Then place this in the /var/www/html/abc/or/whatever/.htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(index.php|images|captcha|css|js|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
This should work in most cases. And don't forget to restart apache!

Not able to access my .css.php using .htaccess

i am trying to access this link on my site
http://mydomain.com/lib/utils/utils.js.php
http://mydomain.com/resources/script/myapp.home.js.php
my .htaccess file is like this
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^resources/(.*).(css\.php|js\.php) ../build/resources/$1.$2 [L]
RewriteRule ^lib/(.*).(css\.php|js\.php) ../build/lib/$1.$2 [L]
RewriteRule ^lib/(.*).(css|js|txt|jpeg|jpg|png|gif|eot|svg|ttf|woff) ../lib/$1.$2 [L]
RewriteRule ^resources/(.*).(css|js|txt|jpeg|jpg|png|gif|eot|svg|ttf|woff) ../resources/$1.$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
my folder structure is
app/handler/x.php
lib/utils/utils.js
build/resources/script/myapp.home.js.php
build/lib/utils/utils.js.php
resources/script/myapp.home.js
web/index.php
web/.htacsess
mydomain.com points to web folder
but i am able to access this link:-http://mydomain.com/index.php
but all other files via .htacess ,all I get is 400 bad Request
If you're on apache, try to edit the file "default" in /etc/apache2/sites-available/ and set
AllowOverride All
in place off
AllowOverride None
than restart apache (/etc/init.d/apache2 restart)

I want to remove index.php name from url [duplicate]

How do I remove the "index.php" sticking out in every path in codeigniter somewhere in the center?
I want clean non index.php-fied URLs?
If you are using Apache place a .htaccess file in your root web directory containing the following:
RewriteEngine on
RewriteCond $1 !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Another good version is located here:
http://snipplr.com/view/5966/codeigniter-htaccess/
I had some big issues with removing the index.php. As a general rule the .htaccess below has been tested on several servers and generally works:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
<Files "index.php">
AcceptPathInfo On
</Files>
If you don't have any luck with that then the next step is to adjust your config file. Try some of the other URI protocols e.g.
| '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'] = 'ORIG_PATH_INFO';
If your still not having any luck try changing the rewrite rule to include your subfolder. This is often a problem if your using a temporary URL on a dev server etc:
RewriteRule ^(.*)$ /subofolder1/subfolder2/index.php/$1 [L]
Just play around with these options, one should work. Also, make sure your index file is set to:
$config['index_page'] = '';
Good luck!
Have the.htaccess file in the application root directory, along with the index.php file. (Check if the htaccess extension is correct , Bz htaccess.txt did not work for me.)
And Add the following rules to .htaccess file,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Then find the following line in your application/config/config.php file
$config['index_page'] = 'index.php';
Set the variable empty as below.
$config['index_page'] = '';
That's it, it worked for me.
If it doesn't work further try to replace following variable with these parameters ('AUTO', 'PATH_INFO', 'QUERY_STRING', 'REQUEST_URI', and 'ORIG_PATH_INFO') one by one
$config['uri_protocol'] = 'AUTO';
Have a look in the system\application\config\config.php file, there is a variable named index_page
It should look like this
$config['index_page'] = "index.php";
change it to
$config['index_page'] = "";
Then as mentioned you also need to add a rewrite rule to the .htaccess file
Note: in CodeIgniter v2 this file was moved out of the system folder to application\config\config.php
All above methods failed for me and then I found that I was not changing AllowOverride None to AllowOverride All in my virtual host file at /etc/apache2/sites-available/default
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride All <---- replace None with All
</Directory>
<Directory /var/www >
Options Indexes FollowSymLinks MultiViews
AllowOverride All <--- replace None with All
Order allow,deny
allow from all
</Directory>
...
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /codeigniter/index.php/$0 [PT,L]
after changing RewriteRule .* index.php/$0 [PT,L] with the project folder name "codeigniter".its working for me. Thanks guys for your support.
Step 1 => Place your .htaccess file in root folder where application and system folders exist.
Step 2 => If your web path using sub-folder like - yourdomain.com/project/ - then use following code in htaccess file
RewriteEngine on
RewriteBase /project/
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /project/index.php/$1 [L]
If your web path using root-folder like - yourdomain.com - then use following code in htaccess file
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Just thought i might add
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
would be the .htaccess and be sure to edit your application/config.php variable in the following manner:
replace
$config['uri_protocol'] = “AUTO”
with
$config['uri_protocol'] = “REQUEST_URI”
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
This must definitely work.. have a try
Ensure you have enabled mod_rewrite (I hadn't).
To enable:
sudo a2enmod rewrite
Also, replace AllowOverride None by AllowOverride All
sudo gedit /etc/apache2/sites-available/default
Finaly...
sudo /etc/init.d/apache2 restart
My .htaccess is
RewriteEngine on
RewriteCond $1 !^(index\.php|[assets/css/js/img]|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Perfect solution [ tested on localhost as well as on real domain ]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
This is an .htaccess for one of my CI projects:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /projectFolder/index.php/$1 [L]
The last line should give you what you need, though you may or may not need '/projectFolder' depending on how your folder structure is set up. Good luck!
Use mod_rewrite as instructed in this tutorial from the CI wiki.
As an .htaccess file, a good option is to use the one provided by the Kohana Framework:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|system)\b.* index.php/$0 [L]
# 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/$0 [PT]
It's a well thought out .htaccess that just works.
There are two way to solve index.php in url path for codeigniter
1:In config/config.php
Change code :
$config['index_page'] = 'index.php';
to
$config['index_page'] = '';
2:Create .htacess in root path if not created,Copy the following code:-
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Save it and then check in your Apache Configuration rewrite_module OR mod_rewrite is Enabled.If not then please enabled.
(Best Approach)!
i tested this on apache2 on many different hosting and it works great.
use this htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
be sure you have enabled mod_rewirte with a phpinfo();
then do this in config/config.php:
$config['index_url'] = '';
|
| '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';
if it doesn't works yet, try to change the $config['uri_protocol']='AUTO' to one of the listed inside application/config/config.php file on line 40/54:
sometimes i used : REQUEST_URI instead of AUTO or "QUERY_STRING" for goDaddy hostings
Look in the \application\config\config.php file, there is a variable named index_page
It should look like this
$config['index_page'] = "index.php";
change it to
$config['index_page'] = "";
Then as mentioned you also need to add a rewrite rule to the .htaccess file like this:
RewriteEngine on
RewriteCond $1 !^(index\\.php|resources|robots\\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
It work for me, hope you too.
If you are on linux and using apache2 server then we may need to override apache2.conf file also beside changes on .htaccess file. Find apache2 configuration file on /etc/apache2/apache2.conf .
Search Directory /var/www/ Change AllowOverride None -> AllowOverride All
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory
place a .htaccess file in your root web directory
Whatsoever tweaking you do - if the above is not met - it will not work. Usually its in the System folder, it should be in the root. Cheers!
I have tried many solutions but the one i came up with is this:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|jquery|js|robots \.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
This iwill remove the index.php from the url as required.
I use these lines in .htaccess file; which I place in root folder
RewriteEngine on
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
Then I can access http://example.com/controller/function/parameter
instead of http://example.com/index.php/controller/function/parameter
Step 1 :
Add this in htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
Step 2 :
Remove index.php in codeigniter config
$config['base_url'] = '';
$config['index_page'] = '';
Step 3 :
Allow overriding htaccess in Apache Configuration (Command)
sudo nano /etc/apache2/apache2.conf
and edit the file & change to
AllowOverride All
for www folder
Step 4 :
Enabled apache mod rewrite (Command)
sudo a2enmod rewrite
Step 5 :
Restart Apache (Command)
sudo /etc/init.d/apache2 restart
This will help you
paste this code in application folder in .htacess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
<Files "index.php">
AcceptPathInfo On
</Files>
<IfModule mod_php5.c>
php_value short_open_tag 1
</IfModule>
This one did me the complete magic:
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]
Hope it helps!
Hi This one worked me
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder/index.php?/$1 [L]
as well as this
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder/index.php?/$1 [L]
where the folder is the name of the subfolder if this codeigniter application is hosted as a subdomain
e.g domainname/folder/index.php.
Hope that works for you. Thanks.
Copy past following code in your .htaccess
RewriteEngine on
Options -Indexes
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Foldername of your ci/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Categories