htaccess throwing error on second RewriteRule - apache

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z]+)/([0-9a-zA-Z-_]+)/? /pages/index.php?page_url=$2&lang=$1 [QSA,L]
RewriteRule ^([a-zA-Z]+)/? /pages/index.php?page_url=home&lang=$1 [QSA,L]
Both rules work fine on their own, but if I have them both I get a 500 Internal Server Error.
What I'm trying to do is on site.com/en/ got to site.com/pages/index.php?page_url=home&lang=$1 (en in this case)
but on site.com/en/home/ go to site.com/pages/index.php?page_url=$2&lang=$1 (home and en respectively in this case)
Any ideas?

You are getting 500 error because your rules are looping due to wrong regex.
Keep your rules like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z]+)/([\w-]+)/?$ /pages/index.php?page_url=$2&lang=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z]+)/?$ /pages/index.php?page_url=home&lang=$1 [QSA,L]

Related

htaccess Rerwite rules colliding

I have to rewrite rules that collide with each other. Actually, it's the second rule that causes the first not to work, even when I switch positions.
artistmusic.php
From: http://www.example.com/artistmusic?slug=Ben
To: http://www.example.com/Ben/music
artistvideos.php
From: http://www.example.com/artistvideos?slug=Ben
To: http://www.example.com/Ben/videos
Rewrite rule for the above urls.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)$ /artist$2?address=$1 [L]
This works like it should, until I add the rewrite rule for the set of URls below.
allsongs.php
From: http://www.example.com/allsongs?standard=popular&request=monthly
To: http://www.example.com/songs/popular/monthly
allartists.php
From: http://www.example.com/allartists?standard=popular&request=monthly
To: http://www.example.com/artists/popular/monthly
Rewrite rule for the second set above.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /all$1?standard=$2&request=$3
The rule above works but kills the first one.
What is the cause of this? Thanks.
With your shown samples/attempts, please have your htaccess rules file in following manner. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^songs/([^/]*)/([^/]*)/?$ allsongs.php?standard=$1&request=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^artists/([^/]*)/([^/]*)/?$ allartists.php?standard=$1&request=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(music|videos)/?$ artist$2.php?slug=$1 [QSA,NC,L]
Generic solution: In case you don't want to hard code values in your new rules then try following. Make sure use either above OR following only 1 at a time.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ all$1.php?standard=$2&request=$3 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ artist$2.php?slug=$1 [QSA,NC,L]

.htaccess multiple parameters is not working

I have a problem with htaccess rule because is not working. I already try to find any solution on google but still have not found yet.
The code is:
RewriteEngine ON
RewriteBase /deshop/
RewriteCond %{REQUEST_URI} ^/deshop/admin(.+)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ admin/index.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ index.php?p=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^\s]+)$ index.php?p=$1&b=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([0-9]+)$ index.php?p=$1&id=$2 [L]
The urls are working normally:
http://localhost/deshop/index.php to http://localhost/deshop/ -> home page from folder
http://localhost/deshop/admin/index.php to http://localhost/deshop/admin/ -> admin page from subfolder
http://localhost/deshop/index.php?p=tshirts to http://localhost/deshop/tshirts/
http://localhost/deshop/index.php?p=brand&b=John%20Player to http://localhost/deshop/brand/John-Player/
So that happen, last rule does not work.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([0-9]+)$ index.php?p=$1&id=$2 [L]
But last rule has worked yet. I really difficult. I want get url with parameters so look work :
http://localhost/deshop/index.php?p=single&id=0005 to http://localhost/deshop/single/0005
Or, what am I wrong? Any solution? Can you help me? Thanks a lot. By the way, my english is not good. I'm sorry!
Finally I have solved it.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/miistore/miiadmin$
RewriteRule ^(.+)$ miiadmin/index.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]
RewriteRule ^/?single/([^/d]+)/?$ index.php?p=single&id=$1 [L,QSA]
RewriteRule ^/?brand/([^\s]+)/?$ index.php?p=brand&b=$1 [L,QSA]

RewriteRule to redirect whole domain to get parameter

I want to redirect a whole url to a query parameter with a RewriteRule in .htaccess
for example: http://server.com/http://google.com should be redirected to
http://server.com/index.php?url=http://google.com
so far i'm just able to make this work: http://server.com/google.com but when a : or / is contained, it doesn't work..
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-z0-9_.-]+)$ index.php?url=$1 [L,NC,QSA]
thanks for help!
RewriteRule patter strips multiple / into one, better use RewriteCond here:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(.+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . index.php?url=%1 [L,NC,QSA]
Why not just do the TLD and then add the http:// in the rule. This is how I would do it.
This is the way I would use it so it doesn't "look" invalid. http://server.com/google.com
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-z0-9_.-]+)$ index.php?url=http://$1 [L,NC,QSA]

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

Multiple .htaccess rules?

I'm getting the grip of .htaccess, but now i'm stuck in a situation where there's multiple redirects needed.
website/werknemers/5
website/lijst/5
Those links should work, redirecting from
index.php?page=werknemers&klant=5
index.php?page=lijst&lijst=5
I'm using the following .htaccess, which is working! but only the one's i put at the top of the document
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?page=$1&klant=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?page=$1&lijst=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L]
Since your patterns are exactly the same, there is no difference between the first and second rules, so the first always matches and you will always get the klant query string no matter what. You can hardcode the page names if that's the only thing that's different:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^werknemers/(.*)$ index.php?page=werknemers&klant=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^lijst/(.*)$ index.php?page=lijst&lijst=$1 [L]