.htaccess RewriteRule country codes as a get param - apache

What I want to achieve:
http://example.com/en -> http://example.com/?lang=en
http://example.com/en/something -> http://example.com/something/?lang=en
http://example.com/ru -> http://example.com/?lang=ru
http://example.com/ru/something -> http://example.com/something/?lang=ru
I try with:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^en/(.+)$ /?p=$1&lang=en [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ru/(.+)$ /?p=$1&lang=ru [L,QSA]

You can do it like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(ru|en)/(.+)$ /$2/?lang=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(ru|en)/?$ /?lang=$1 [L,QSA]
(ru|en) will match either ru or en. If you wanted to make it a generic 2 letter match, you could use [a-z]{2}

Related

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

how to escape "/" from mod_rewrite

this is my .htaccess file thnx to #Gyrocode-com
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/(.*)$ fun.php?p=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^u/(.*)$ user.php?u=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^u/([^/]+)/([1-9]+)$ user.php?c=$1&p=$2 [L] #my problem is here
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([1-9]+)$ index.php?p=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?c=$1&p=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ index.php?c=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /jokes/$1/ [R]
everything works great except whene i try to go to this link
localhost/u/user1/2
which suppose to take me to page number 2 of user1
but instead it catches user1/2 as the value of variable (u)
and what I want is u=user1 ; p= 2;
Your rules can be refactored to this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^p/([^/]+)/?$ fun.php?p=$1 [L]
RewriteRule ^u/([^/]+)/?$ user.php?u=$1 [L]
RewriteRule ^u/([^/]+)/([1-9]+)$ user.php?c=$1&p=$2 [L]
RewriteRule ^([1-9]+)$ index.php?p=$1 [L]
RewriteRule ^([^/]+)/([^/]+)$ index.php?c=$1&p=$2 [L]
RewriteRule ^([^/]+)/$ index.php?c=$1 [L]
RewriteRule ^([^/]+)$ /jokes/$1/ [R,L]
Using ([^/]+) instead of (.*) to restrict matching to anything before /
Instead of using RewriteCond %{REQUEST_FILENAME} -f and RewriteCond %{REQUEST_FILENAME} -d before each rule it is better to use that in a separate rule as shown.

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]

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]

.htaccess RewriteRule country codes and urls

I am using the following .htaccess code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ca/(.+)$ /index.php?p=$1&c=ca [L,QSA]
RewriteRule ^fr/(.+)$ /index.php?p=$1&c=fr [L,QSA]
RewriteRule ^(.+)$ /index.php?p=$1 [L,QSA]
In order to achieve the following effect:
http://xyz.com/ca/test -> http://xyz.com/index.php?p=test&c=ca
http://xyz.com/fr/test -> http://xyz.com/index.php?p=test&c=fr
http://xyz.com/test -> http://xyz.com/index.php?p=test
But it is failing with a server error. Any ideas on how to fix it?
Thanks
RewriteCond conditions only apply to the RewriteRule that immediately follows the condition(s). Your last 2 rules don't have any conditions on them and the rules are looping. Just add the 2 conditions in front of the last 2 rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ca/(.+)$ /index.php?p=$1&c=ca [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^fr/(.+)$ /index.php?p=$1&c=fr [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?p=$1 [L,QSA]