url rewriting synthax with .htaccess - apache

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]

Related

Rewrite URLs in .htaccess for replacing Query parameters with forward slash (id?=value)

I have made sure that rewrite engine is enabled and removing .php extensions is working so I know that isn't the issue.
what I'm trying to do is simply remove the ?id=value aspect of the URL, so basically making the URL look like such:
folder/medias/value
Instead of
folder/medias?id=value
My current .htaccess looks like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^404/?$ /404.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ 404.php [L,R]
With your shown samples/attempts, please try following htaccess Rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules for external rewrite.
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?id=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Rule for internal rewrite.
RewriteRule ^([^/]*)/([^/]*)/?$ $1?id=$3 [L]
You may try this code inside the /folder/.htaccess (create this file if it doesn't exist):
RewriteEngine On
# External redirect from /folder/media?id=val to /folder/media/val
RewriteCond %{THE_REQUEST} /(\S+?)\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ /folder/%1/%2? [R=301,L,NE]
# Internal rewrite from /folder/media/val to /folder/media?id=val
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?id=$2 [L,QSA]
Trailing ? in first rule is to remove query string from original URL.
%{REQUEST_FILENAME} !-f and %{REQUEST_FILENAME} !-d is to skip existing files and directories from rewrite in 2nd rule.

.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]

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]

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]

Add directory in the middle of a URL with hataccess

My current .htacces file looks like this:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
What I am trying to accomplish (as well as the above) is to change a url such as http://domain.com/pages/pagename to something like http://domain.com/index.php/pages/view/pagename.
Keeping in mind that I still require urls without the /page/ part such as http://domain.com/search to go to http://domain.com/index.php/search. I am working with CodeIgniter.
What I have come up with so far is:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
RewriteCond %{REQUEST_URI} ^/pages/(.*)
RewriteRule ^(.*)$ ./index.php/pages/index/$1 [L,QSA]
But it isn't working.
Try this one:
RewriteEngine on
# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# /pages/ specific rule
RewriteRule ^pages/(.*)$ index.php/pages/index/$1 [L]
# everything else
RewriteRule ^(.*)$ index.php/$1 [L]
The above assumes that http://domain.com/pages/ is not a real folder.
RewriteRule ^pages/(.*) /index.php/pages/index/$1