htacces rewrite url - remove folder name from url - apache

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]

Related

url rewriting synthax with .htaccess

I am on apache 2 and I would know if my htacces is correct
my url are for example :
localhost/test/boutique/index.php
localhost/test/boutique/index.php/language,en
localhost/test/boutique/index.php/Products/Description/products_id,1
localhost/test/boutique/index.php/Products/Description/products_id,2/language,fr
What is the best approach for a good url like above
In suppose index.php must deseapear to hav someting like that
localhost/test/boutique/Products/Description/products_id,1
I try this but it does'nt work
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://localhost/test/boutique/index.php/$1 [PT,L]
Assuming /test/boutique/ is a real directory, you can use these rules inside /test/boutique/.htaccess:
RewriteEngine On
RewriteBase /test/boutique/
# remove index.php if entered directly by clients
RewriteCond %{THE_REQUEST} /index\.php/(\S*)\s [NC]
RewriteRule ^ %1 [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!index\.php$).* index.php/$0 [L,NC]

.htaccess rewrite to add country code (if missing)

I am using a .htaccess file in the subdirectory /cms and using this subdirectory as RewriteBase. The redirections go to 'backend.php' and send the variables I want. It is a dual language site (nl|en), dutch and english. Everything works fine, as long as the %{REQUEST_URI} starts with (nl|en).
But I need a fallback to the default dutch language when nl|en is omitted, but can I add this to the following .htacces file. I have been trying and searching but cannot find the right syntax to make this happen:
RewriteEngine On
RewriteBase /cms/
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_URI} !(^(nl|en)/)
#RewriteRule (.*) This is where the solution should be used ?
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.[a-zA-Z]{2,4}$
RewriteRule ^(nl|en)/([^/]+)/$ backend.php?page=$2&language=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.[a-zA-Z]{2,4}$
RewriteRule (nl|en)(.*)/(\d+)/$ backend.php?page=$2&id=$3&language=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.[a-zA-Z]{2,4}$
RewriteRule (nl|en)(.*)/(\d+)/(.+)/$ backend.php?page=$2&id=$3&task=$4&language=$1 [QSA,NC,L]
One way to handle this is a rewrite with the dutch language prepended. This will silently change direction to the proper page.
RewriteCond %{REQUEST_URI} !^/cms/(?:nl|en)/
RewriteRule ^(.*)$ nl/$1 [L]
If you want the client to notice and change the URL, you must do a R|redirect instead
RewriteCond %{REQUEST_URI} !^/cms/(?:nl|en)/
RewriteRule ^(.*)$ nl/$1 [R,L]
Your default rule can be this one:
# This is where the solution should be used ?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(nl|en)/ [NC]
RewriteRule ^([^/]+)/?$ backend.php?page=$1 [QSA,L]

redirect to original URL without query string using .htaccess

I want to redirect a url with any query string to its original url.
Something like:
mysite.com/?any=query -> mysite.com/
mysite.com/page/?any=query&more=query -> mysite.com/page/
I'm already using this rules to redirect any file to index.php on root directory.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?pagina=$1
You can use:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?pagina=$1 [L]
RewriteCond %{QUERY_STRING} ^.+$
RewriteRule !^index\.php %{REQUEST_URI}? [R=301,NE,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]

Rewrite rule to handle url exception

I have an app that serves many sites and I am using Apache mod-rewrite to map the
url's like this
http://site1.net/controller
http://site2.net/controller2/another_view
http://site3.net/
http://special_case.net/
maps to:
index.php?url=http://site1.net/controller
index.php?url=http://site2.net/controller2/another_view
index.php?url=http://site3.net/
index.php?url=http://special_case.net/hub
My rewrite rules are:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Which handles all the cases except the special last case, where I need to force
the use of a controller called "hub" when handling a specific domain. I collaborate with others on this project which means I can't do anything about the routing once the index file is called.
Can someone fix my rules so that all the above cases resolve?
You current rules don't seem to add the hostname to the url get-parameter. So I added that to to the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^(special_case\.net)$
RewriteRule ^(.*)$ index.php?url=http://%1/hub/$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^(.*)$
RewriteRule ^(.*)$ index.php?url=http://%1/$1 [QSA,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^http://special_case.net index.php?url=http://special_case.net/hub [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]