Convert apache rewrite rules to lighttpd rules - apache

How to convert this Apache rewrite rules to lighttpd rules?
RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1

i think this one will work .
url.rewrite-once = (
".*\?(.*)$" => "/index.php?$1",
".*\.(?i)(js|ico|gif|jpg|png|swf|css|html)$" => "$0",
"" => "/index.php")

Related

Rewrite non-dynamic pages using htaccess

I've been banging my head since many hours on this and I am unable to come up with a solution.
What I need to get done.
domain.com/page.php => domain.com/page/
domain.com/category1-name1.php => domain.com/category1/name1/
domain.com/category2-name1.php => domain.com/category2/name1/
Details: I have couple of files with the following names & I want them to be shown in the following way.
category1-name1.php => category1/name1/
category1-name2.php => category1/name2/
category1-name3.php => category1/name3/
category2-name100.php => category2/name100/
category2-something.php => category2/something/
category2-lipsum.php => category2/lipsum/
Also, I would like to achieve the following rewrite too
/category.php => /category/
/abcd.php => /abcd/
I'm trying but I can't achieve any of it.
I tried this code:
RewriteEngine On
RewriteRule ^category1/([^/]+)/?$ category1-$1.php [L]
RewriteRule ^category2/([^/]+)/?$ category2-$1.php [L]
RewriteRule ^([^/]*)/?$ $1.php [L]
Its only working for /abcd.php to /abcd/ and its throwing 404 errors on rest of the pages, even index is 404 due to it.
Try the following rules in your /htaccess file :
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/([^.-]+)\.php [NC]
RewriteRule ^ %1/ [NC,R,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/([^-]+)-([^.]+)\.php [NC]
RewriteRule ^ %1/%2/ [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ $1.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ $1-$2.php [QSA,NC,L]

.htaccess rewrite rules issue

I am trying to rewrite some urls using mod_rewrite.
I want to rewrite
localhost/exotica/pet/somePet/
to
`localhost/exotica/index.php/types/get/somePet/`
I have managed to remove index.php by using
RewriteCond $1 !^(index\.php|resources|uploads|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /exotica/index.php/$1 [L]
so now localhost/exotica/types/get/somePet works
i have tried adding as first rule
RewriteRule ^pet/([A-Za-z0-9-]+)/?$ type/get/$1 [N]
but it simply doesnt work so please help me i have no idea how to enable it
I managed to solve it by adding a route $route['pet/(:any)'] = "type/get/$1";, but I would prefer to do it using .htacess file
Try your rules like this in reverse order:
RewriteEngine On
RewriteBase /exotica/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^pet/([A-Za-z0-9-]+)/?$ types/get/$1 [L]
RewriteCond $1 !^(index\.php|resources|uploads|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Lang parameters in url rewriting

Some times now I'm around my htaccess and no way to get what I want...
I'd like to rewrite as followings :
http://www.mydomain.com/trunk/public/lang/ => http://www.mydomain.com/trunk/public/?l=lang
http://www.mydomain.com/trunk/public/lang/profile => http://www.mydomain.com/trunk/public/profile?l=lang
http://www.mydomain.com/trunk/public/lang/user/45 => http://www.mydomain.com/trunk/public/user/45?l=lang
...
Any idea how to do that?
--------EDIT-------
My htaccess actually looks like :
<IfModule mod_rewrite.c>
RewriteEngine On
#Transform lang into get parameter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(fr|en)/$ ?l=$1 [L,QSA]
#Transform lang into get parameter
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(fr|en)/(.+)$ $1?l=$2 [L,QSA]
#This last condition enables access to the images and css folders, and the robots.txt file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|images|assets|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Results :
http://www.mydomain.com/trunk/public/lang/ gives a bonfire 404 error (it tries to load "lang" module)
http://www.mydomain.com/trunk/public/lang/profile gives a server 404 error
http://www.mydomain.com/trunk/public/profile works
http://www.mydomain.com/trunk/public/profile?l=fr works
I have managed to set the default domain destination in the trunk/public folder via an htaccess placed in a the trunk parent directory
Insert these 2 rules in your /trunc/public/.htaccess file:
RewriteEngine On
RewriteBase /trunc/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ ?l=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.+)$ $1?l=$2 [L,QSA]

Use specific PHP file for specific directories

I am trying to create mod_rewrite rules that rewrite based on the first level directory name plus a failover to rewrite to a standard file in case none of the directory names are matched.
Example:
I have units.php, models.php and other.php. The other.php file should handle all non-assigned requests.
http://www.mydomain.com/units/4435
Should redirect to /units.php?id=4435
http://www.mydomain.com/models/594
Should redirect to /models.php?id=594
http://www.mydomain.com/anything
Should redirect to /other.php?id=anything
http://www.mydomain.com/anything/893
Should redirect to /other.php?id=anything/893
Let me know if this makes sense. I am unsure of how to structure the rules and conditions to achieve what I want.
This is what I have tried to sfar. It works for URLs starting with 'units' or 'models' but I get a 500 Error if I try any other URL:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(units)/(.+)$ /units.php?id=$2 [L,NC]
RewriteRule ^(models)/(.+)$ /models.php?id=$2 [L,NC]
RewriteRule ^(.+)$ /other.php?id=$2 [L,NC]
I would suggest this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(units|models)/([0-9]+)/?$ /$1.php?id=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(units|models)/[0-9]+/?$
RewriteRule ^/[^/]+/([0-9]+)/?$ /other.php?id=$1 [L,QSA]

htacces rewrite url - remove folder name from url

I'm tryng to make an htaccess url rewrite for this cases:
www.website.com/index.php/admin/something => www.website.com/admin/something
www.website.com/index.php/website/something => www.website.com/something
www.website.com/index.php/login/something => www.website.com/login/something
note: "something" can be "something1/something2/something3" or "something1/something2" or "something"
I can cut "index.php" with:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
but I can't found a way to remove only "website/" and not "admin/" and "login/" leaving the rest of url.
If taking this approach you will probably have to specify each possible input reference that should not be mapped to your 'website' controller and remap it to index.php/$1 and treat every other request like it is a method of the website controller, mapping it to index.php/website/$1
Something like the below will map anything starting with admin or login to index.php and everything else to the website controller:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^admin.*
RewriteCond %{REQUEST_URI} ^login.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}
RewriteRule ^(.*)$ /index.php?/website/$1 [L]