apache rewrite rule - php

I have problems with apache mod rewrite
I have this code in my .htaccess :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?route=$1 [QSA,L]
to rewrite routes like this
sitename.com/index.php?foo/bar/baz
to this
sitename.com/foo/bar/baz
It works fine but I have problems with css|js|jpg|png and all other files
how can I exclude rewriting rules for specific types of files?

You just need an additional rewrite condition which tests for that kind of filename:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !/\.(css|js|jpg|png)$/i
RewriteRule ^(.*)$ index.php?route=$1 [QSA,L]

Related

How to create rewrite rule that allows to rewrite something right after slash with exceptions

i want to create these rewrite rules:
From:
www.something.com/name-of-some-product
To:
www.something.com/index.php?site=name-of-some-product
But I also need to use images and some other files and also other PHP files.
The .htaccess i have:
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.bmp|\.css)$
RewriteRule ^(.*)$ index.php?site=$1 [QSA]
The problem is when i try to load file like this:
www.something.com/another_php_file.php?parameter=value
It doesn't work, because i don't know how to avoid rewriting PHP files.
Try this Rule,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?site=$1 [QSA]

Rewrite Rules to just add .php not working

So I have a few files like
about.php
contact.php etc
I'm trying to set up rewrite rules so that I can access them through /about and /contact
To do this I've got the following:
RewriteEngine on
RewriteRule ^(.+)$ $1.php
But it doesn't seem to be working. Is there something I'm doing wrong?
This is my simple rewrite rule to rewrite only if the file does not exist.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*) $1.php
If its not wirking you should check if you have set AllowOverride all in your vhost configuration. Otherwise the rewrite rule is not working.

remove index.php with alternative routing in codeigniter

I'm working on a project in Codeigniter that doesn't use the standard routing.php file.
It calls the functions like this: test.vi/index.php/controller/function.
My goal is to remove the index.php from the url with a htaccess rewrite:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
And also this config.php change:
$config['index_page'] = '';
My question is do I need to route all the controllers/functions in my route.php or can it be done without using the route.php file?
first make sure you enabled mod_rewrite in httpd.conf:
to do so add/make sure it exsits this line
LoadModule rewrite_module modules/mod_rewrite.so
then create .htaccess in your codeigniter root folder (next to index.php file) not application folder.
.htaccess
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|img|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
codeigniter take care of all routings for you; so for
controlers/welcome.php method=hello u can access it by
http://localhost/welcome/hello
its not like laravel you need to specifically add route; codeigniter automatically Fitch controller for you. unless you have it stored in a different location or inside subfolder then you dont need to touch route.php except for setting your default route.
Here's my redirect in .htaccess for a live codeigniter site. The second part exempts certain directories.
#Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond $1 !^(index\.php|phpinfo\.php|test\.php|example\.php|images|i|css|js|beta|captcha|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
THIS works perfect for me... give it a try..
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
try this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test.v/index.php?/$1 [L]
I guess everyone forget about add test.vi folder name on .htaccess

htaccess - using codeigniter framework

I search a lot websites and lots of links and i follow the same for enable htacces in wamp but facing the problem and now as default use index.php even when i run the page without index.php its not working even msg comes page not found on server i already enable in apache module also the httpd.conf file remove the # sign from rewrite module and i use the mention below code in htaccess file for codeigniter for suggestions i share htaccess code
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
please suggest so i can solve this issue on wamp server
Try this code in your htaccess :
RewriteEngine On
RewriteBase /my_project_name/
# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
Try this
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

How to correct this rewrite rule?

I have url as
http://www.mydomain.com/levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c. I need to change this url to http://www.mydomain.com/medium.
I am not familiar with rewrite url.
I tried with RewriteRule ^medium/?$ levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c, but not worked correctly.
Full rewrite rule
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^medium/?$ levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c
RewriteRule ^(.*)$ index.php [QSA,L]
Try to change only that line to:
RewriteRule ^medium /levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c
Or if you don't care if anything is after medium is the following:
RewriteRule ^medium(.*) /levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c
Your rules are in the wrong order, try changing them to:
RewriteEngine On
RewriteRule ^medium/?$ levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
The conditions need to be applied to the index.php rule, and not the medium rule. RewriteConds are only applied to the immediately following RewriteRule, not all of them.
This rewrite rule worked fine
RewriteRule ^medium.*$ /levels/home?mode=48bb6e862e54f2a795ffc4e541caed5c [R=301,L]

Categories