.htaccess Rewrite Rule doesn't work on my site - apache

how i can rewrite this url:
http://www.example.com/album?album_id=52
to this:
http://www.example.com/album/52
my .htaccess code is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^album/$ album?album_id=$1 [L]

You need to capture album id from your pattern first before you can use it as $1:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^album/([0-9]+)/?$ album?album_id=$1 [L,QSA]

Related

Apache Redirect from a folder to a index.php file

I want to redirect from http://www.domain.com/customer1 to /app/index.php?c=customer1 and http://www.domain.com/customer2 to /app/index.php?c=customer2
I tried the below rule but not works well...
RewriteRule ^customer1(.*)$ /app/index.php?c=customer1
I am assuming you are talking about rewriting,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)$ /app/index.php?c=$1 [QSA,L]
This will internally rewrite your urls if not rewriting then try it like this,
RewriteEngine on
RerwiteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)$ /app/index.php?c=$1 [R=301,QSA,L]

Tricky 301Redirect Rule

I want link to be redirected in a manner such that url with not slash at the end should be redirected to the url with the slash at the end.
example 1:
http://example.com/quiz/funny-riddles-with-answers
should be redirect to
http://example.com/quiz/funny-riddles-with-answers/
example 2
http://example.com/quiz/math-riddles-with-answers
should be redirected to
I have no idea how to do it.
my current screen shot of .htacess is
RewriteEngine On
RewriteBase /quiz/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)?$ mypage.php?param1=$1&param2=$2 [L,QSA]
Try these rules in /quiz/.htaccess:
RewriteEngine On
RewriteBase /quiz/
# add a trailing slash for non-directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?[^/])[?\s]
RewriteRule ^ %1/ [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ mypage.php?param1=$1&param2=$2 [L,QSA]
You can easily rewrite to a trailing slash using:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
Place above your already existing rewrites.
You need to change your current rule's pattern to: ^([^/]+)/([^/]+)?/?$
then add this right under RewriteBase /quiz/:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /$1/$2/ [L,R=301]

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

Apache Htaccess forwarding sub folders

I want to create a custom rule for htaccess so that all the URL's requested in the home folder such as localhost/asfsd will redirect to localhost/index.php?url=asfsd but if the user enters the following url localhost/asfsd- he must then be forwarded to localhost/info.php?url=asfsd . Here is my current .htaccess configuration :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php?url=$1 [L,QSA]
You can try these 2 rules:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)-$ /info.php?url=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?url=$1 [L,QSA]
Try this
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\-$ info.php?url=$1 [L,QSA]
RewriteRule ^(.*) index.php?url=$1 [L,QSA]