L
L
lightalex2015-11-14 01:25:03
htaccess
lightalex, 2015-11-14 01:25:03

How to add an extra part to the url using .htaccess?

Hello!
There is a site domain.com
When you enter the site, the script for determining the country is triggered and adds two letters of the country to the url.
Let's say we came from Russia and then we get to domain.com/ru/
How to make the server perceive this address as domain.com/?lg=ru?
Of course, any files/directories should be processed normally
. For example:

domain.com/ru/account/ -> domain.com/account/?lg=ru
domain.com/ru/php/1/order.php -> domain.com/php/1/order.php?lg=ru

And another point
The site should not crash when adding or not adding a slash at the end
. For example:
domain.com/ru/account/ -> domain.com/account/?lg=ru
domain.com/ru/account -> domain.com/account/?lg=ru

How to implement this? I try it myself, it doesn't work :(
Who fumbles in htaccess, please help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaliy Orlov, 2015-11-14
@orlov0562

Here's a starting example for you:

RewriteEngine On
RewriteRule ^([^/]+)(/.+)$ $2?lg=$1 [QSA,D]
RewriteRule ^(.+)$ index.php?p=$1 [QSA]

in index.php
<?php
print_r($_REQUEST);

and run with this request:
localhost/ru/test/fed?f=f
you get the following response:
further modify to taste

S
ShamblerR, 2015-11-21
@ShamblerR

RewriteCond %{REQUEST_URI} ^(.*)/dir1/(.*)$ 
# если строка содержит /dir1/ 
RewriteRule ^(.*)$ %1/dir2/%1 [R=301,L]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question