0

This simple rule in .htaccess handles languages at my website:

RewriteRule "^(de|it|es|fr|en-us)(?:|/(.*))$" "$2?lang=$1" [NC,QSA]

It works fine unless I miss slash at the end of URL. Than it changes URL in browser's address bar which is not what I want.

To make myself more clear look at examples bellow.

http://mydomain/en-us/projects/ keeps that URL in address bar and calls http://mydomain/projects?lang=en-us which is OK (+generates REDIRECT_STATUS=200).

http://mydomain/en-us/projects (notice missing slash at the end) calls same URL but makes change in address bar (it doesn't generate REDIRECT_STATUS).

As I have almost zero experience with Apache stuff must ask you how to rewrite any of URLs like

http://mydomain/[lang_id]
http://mydomain/[lang_id]/more/dirs/here/
http://mydomain/[lang_id]/test.php

to

http://mydomain?lang=[lang_id]
http://mydomain/more/dirs/here?lang=[lang_id]
http://mydomain/test.php?lang=[lang_id]
Wh1T3h4Ck5
  • 101
  • 4

1 Answers1

0

Add a question mark after the trailing slash in the rule. End the rewrite rule with /?$. Follow this example:

RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?shop=$1&page=index [NC,L,QSA]

For your scenario I believe it would be

RewriteRule "^(de|it|es|fr|en-us)(?:|/(.*))?$" "$2?lang=$1" [NC,QSA]
El Chapo Gluzman
  • 396
  • 2
  • 16
  • Nope... Doesn't work! Anyway, that `?` is not supposed to change anything because entire group is written as `(nothing|something)` so according to how I understand things that group would be ignored if nothing trails [lang_id]. But my knowledge about regex is related to JS/PHP/Ruby regular expressions and I'm sure that Apache handles them in a bit different way. Thanks for your try, anyway. – Wh1T3h4Ck5 May 12 '16 at 14:48