Mod Rewrite: How to rewrite URL passing everything to index.php - php

I enabled mod_rewrite and checked it with phpinfo().
I placed a .htaccess file with
RewriteEngine on
RewriteRule ^(.*)$ index.php?/$1 [L]
and a index.php file at apache root directory (/var/www/html/).
Working URL:
http://192.168.56.101/index.php
http://192.168.56.101/index.php/edrgderg
Failing (404) URL:
http://192.168.56.101/sgghsshgfhfgj
Mod_rewrite should form this to http://192.168.56.101/index.php/sgghsshgfhfgj , but it doesn't.
I want to redirect each request to index.php.
SOLUTION:
I had to edit my apache configuration:
sudo nano /etc/apache2/sites-enabled/000-default.conf
I added:
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
As sugested here: https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite-for-apache-on-ubuntu-14-04

Exclude existing files/directories from your rewrite rule to avoid looping the rules:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?/$0 [L,QSA]

Related

Codigniter htaccess error 404 at production

i am very begainer to server technologies.i know this question asked too many times but i really don't get it run.
it runs with domain.com/2.0/index.php/testbut when you remove index.php it throws
The requested URL /2.0/test was not found on this server.
Apache/2.2.15 (CentOS) Server at domain.com port 443
my project stucture
-
Public_html
--2.0(this is my project folder where ci can be located.it has sys,app,.htaccess)
--.htaccess
My domain
https://domain.com/2.0/
about the problem
i have two htaccess one at root mention above one is inside 2.0 folder
and the strange thing is what ever i write in the htaccess file it never effect anything
public_html/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
public_html/2.0/.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
my httpd.conf
<VirtualHost *:443>
ServerAdmin info#domain.com
DocumentRoot /home/natural/public_html
ServerName api01.domain.net
ErrorLog /var/log/httpd/error_log
CustomLog /var/log/access_log common
SSLEngine on
SSLCertificateFile /home/natural/api01.domain.net.crt
SSLCertificateKeyFile /home/natural/api01.domoin.key
<Directory />
Options Indexes FollowSymLinks
Allow from all
AllowOverride None
</Directory>
</VirtualHost>
i search alot and follow all the answers by Stack overflow but no luck.
The directive "AllowOverride None" in httpd.conf will disable parsing of the .htaccess file within the directories specified (in this case /) -- Can you confirm that either of your .htaccess files are in fact being parsed?
If not, does modifying the Directory subsection within your httpd.conf file to:
<Directory />
Options Indexes FollowSymLinks
Allow from all
AllowOverride All
</Directory>
Solve the issue?
For more information: http://httpd.apache.org/docs/2.4/mod/core.html#allowoverride
I assume 2.0 is the new version of the site. It looks like /2.0/.htaccess is written as though it expects to be in the root folder. Try adding RewriteBase /2.0/ after engine on. You can remove it later when you replace the contents of the root folder with that of 2.0's.
The main problem is your # Handle Front Controller... rule. It'll capture any virtual URLs before they ever get to the 2.0 folder. Try changing it to this:
RewriteRule ^(?!2\.0(?:/|$)) index.php [L]
That will exclude the 2.0 folder from the current site's virtual URLs.
You do still need AllowOverride all as recommended by the other answer, and the directory should be the same as your DocumentRoot, not /.
Add this code inside root->.htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^(?!2\.0(?:/|$)) index.php [L]

Codeiginter : Error in URL

My front url is-
http://localhost/myProject/
admin url is -
http://localhost/myProject/admin
It works in windows but not works on Ubuntu.
It gives error "Not found".
What works in ubuntu -
The front page is working - http://localhost/myProject/
The admin login page is working if I add index.php in url like this -
http://localhost/myProject/index.php/admin
Not other pages are working
My .htaccess file contents-
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|themes)
RewriteRule ^(.*)$ /myProject/index.php/$1 [L]
My Apache's mod-rewrite module is on.
I was also following the similar issue in Centos, and I followed the below tutorial :
How to permit changes in the .htaccess file:
open httpd using ------------------> vi /etc/httpd/conf/httpd.conf
Once inside that file, find the following section, and change the line that says AllowOverride from None to All. The section should now look like this:
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
After you save and exit that file, restart apache. .htacess files will now be available for all of your sites.
service httpd restart
Content of .htaccess file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -Indexes
RewriteEngine on
# NOTICE: If you get a 404 play with combinations of the following commented out lines
#AllowOverride All
#RewriteBase /
# Restrict your site to only one domain
#RewriteCond %{HTTP_HOST} !^www\.
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|fonts|js|images|robots\.txt|css)
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_php5.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
</IfModule>
#prevent access to htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
#disable directory browsing
Options All -Indexes
IndexIgnore *

The url requested was not found laravel 4

I'm trying to create an app with laravel 4 but i'm facing a url issue. i have wamp installed on my machine. i set up a new virtual host in my httpd-vhost.conf with this code
<VirtualHost mobile.dev>
DocumentRoot "C:\wamp\www\mobile.dev\public"
ServerName mobile.dev
<Directory "C:\wamp\www\mobile.dev">
Options FollowSymLinks Indexes
AllowOverride All
Order deny,allow
Allow from 127.0.0.1
Deny from all
Require all granted
</Directory>
mobile.dev is a folder and also the domain name in my localhost.
here is my Route.php file
Route::get('/','HomeController#showWelcome');
Route::post('login','LoginController#userLogin');
Route::get('login','LoginController#getUsers');
when i ask for mobile.dev/login it gives me the requested url was not found.
can you help me please solve this issue. but when i ask for mobile.dev/ its working.
Here is my .htaccess file :
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Here is more details. when i remove my htaccess content everything works fine with the default url http://mobile.dev/index.php/login
So the problem is in my htaccess and the rewriting in my virtual host
It might be not Laravel problem, but seems your wamp server haven't enabled mod_rewrite in httpd.conf file yet. Locate the line -
#LoadModule rewrite_module modules/mod_rewrite.so
and remove the comment symbol '#'. Then save the file and restart Apache.
Solution:
1- when you have wamp installed in your machine please go to C:\wamp\bin\apache\Apache*.*.*\conf\httpd.conf and make sure that rewrite_mod is enabled by deleting the hash tag. (instead of using the interface provided by wamp)
2-there are two ways to enable rewriting for laravel in .htaccess file :
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
or using this :
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
3- if you're setting a virtual host be sure to add this option in the <virtualhost> tag
<Directory "C:\wamp\www\mobile.dev\public">
Options FollowSymLinks Indexes Multiviews
AllowOverride All
</Directory>
Thanks a lot for your time.
Have you tried
Route::post('login','LoginController#userLogin');
without the () after userLogin?
Your <Directory "C:\wamp\www\mobile.dev"> in config should be <Directory "C:\wamp\www\mobile.dev\public"> to specify the public directory to the root of the htaccess rewrite rules.
I originally thought you were missing the </IfModule> at the end of your .htaccess file but when I posted my .htaccess file it hid the </IfModule> as well so that is a stack overflow issue. Otherwise your htaccess is correct.

Can't remove index.php, custom route not working

I know this question is more appropriate for Server Fault but unfortunately I was banned for poor quality questions (I was down voted on 2-3 questions I asked.) So the next best place to ask these questions are here.
I have two problems related to CodeIgniter routing.
The first problem is that I can't seem to get rid of index.php in the url. I followed the instructions on how to remove it. I have the following mod rewrite code in my .htaccess file (see below) at the root of my WAMP server (CI is located at the root, not in its own folder). I have uncommented this line in httpd.conf file LoadModule rewrite_module modules/mod_rewrite.so. I deleted index.php from $config['index_page'] = "index.php";. And I restarted all WAMP services.
My second problem is that I have a controller called search and a method called index. I would like to change the resultant URL from http://localhost/index.php/search/index to http://localhost/search/whatever_im_searching_for. I tried the following custom route in my routes.php file but it did not work: $route['search/(.*)'] = "search/$1";
RewriteEngine On
# Put your installation directory here:
# If your URL is www.example.com/, use /
# If your URL is www.example.com/site_folder/, use /site_folder/
RewriteBase /
# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# For reuests that are not actual files or directories,
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
I am struggling to understand the code in .htaccess and on how to use CI's custom routing. Any assistance will be greatly appreciated. Thank you in advance.
EDIT 1
Edit your htaccess like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^LoginTut.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond $1 !^(index\.php|images|table-images|js|robots\.txt|css|captcha)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
To have your searchterms in the url you can look at this:
https://stackoverflow.com/a/12070284/1379394
Second problem:
$route['search/(:any)'] = "search/index/$1";
Check Apache's default config file. On WAMP it's probably in
<WAMPSERVER_HOME>\bin\apache\Apache2.2.xx\conf
If it looks like this:
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
then change both:
AllowOverride None
to:
AllowOverride All
I had the same problem and I fixed only by write in my .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
and it's working perfectly.

Remove index.php from URL in code Igniter

i have read posts regarding this but that did not gave answer to my problem
i have set everything i found on forums of code igniter and SO's questions
currently i have following settings :
.htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
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
#This last condition enables access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|(.*)\.swf|images|robots\.txt|css|docs|cache)
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 /application/errors/404.php
</IfModule>
and i also have set $config['index_page'] = '';
but this is not working there is not change in URL
if i type *http://localhost:8088/crud_demo/index.php/login* it works but if i type *http://localhost:8088/crud_demo/login* it shows Not Found Error
My Route config
$route['register'] = "register";
$route['manage'] = "manage";
$route['default_controller'] = "login";
$route['404_override'] = '';
Change AllowOverride None to AllowOverride All in my virtual host file at /etc/apache2/sites-available/000-default.conf in ubuntu
Change AllowOverride None to AllowOverride All in my virtual host file at /etc/apache2/sites-available/default.conf in windows
<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>
....
Then put this code in .htaccess file in root folder in codeigniter
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
It will definately work.
RewriteRule ^crud_demo/(.*)$ crud_demo/index.php/$1
http://martinmelin.se/rewrite-rule-tester/ to test URL rewrites.
I think CodeIgniter has it's own way of routing URLs like other PHP frameworks. Take a look here: http://codeigniter.com/user_guide/general/routing.html
If crud_demo is a subfolder of your root dir then you should change
RewriteBase /
to
RewriteBase /crud_demo

Categories