12

I would like to redirect all paths like this:

myurl.com/worldwide/en
myurl.com/worldwide/pt
myurl.com/worldwide/de

to:

myurl.com/worldwide/index.php?lang=en
myurl.com/worldwide/index.php?lang=pt
myurl.com/worldwide/index.php?lang=de

Just to be clear a dynamic redirection of the pathname after /worldwide

Actually ideally I would like to keep the original url (e.g. myurl.com/worldwide/de) but load the same php file with the language directory as a param but not sure if this is possible?

Thanks

Jose Gómez
  • 3,110
  • 2
  • 32
  • 54
Dominic
  • 62,658
  • 20
  • 139
  • 163

2 Answers2

14

Use this code

RewriteEngine On
RewriteBase /worldwide/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?lang=$1 [L,QSA]

Please let me know if this helps you

Aman Chhabra
  • 3,824
  • 1
  • 23
  • 39
  • Almost perfect thanks very much! Just needed to remove the `/` from `^(.*)$ index.php?/lang=$1 [L,QSA]`. `$1` Resolves to e.g. `worldwide/en` instead of `en` but not a problem I can just strip it out in php. Glad it doesn't change the url either thanks – Dominic Feb 09 '14 at 18:25
  • Thanks... Please appreciate the same by accepting it as an answer – Aman Chhabra Feb 09 '14 at 18:27
  • I will (have to wait a minimum amount of minutes) – Dominic Feb 09 '14 at 18:27
  • How come when I put a `/` all the way at the end, it doesn't load any external resources (css, images)? – Daniel Node.js Jul 20 '14 at 16:29
  • @Dan the Man: Sorry, I am not able to understand you. Can you please share the piece of code that you want to use – Aman Chhabra Jul 23 '14 at 17:12
  • If I change RewriteBase to / then this works for something like example.com/test - that is, it will load index.php?lang=test . But if I actually have a folder in my www root named 'test', it returns the contents of that folder. Is it possible to force it to return index.php?lang=test instead? – youcantryreachingme Mar 13 '18 at 10:35
0
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteRule ^(.+)$ /index.php?page=$1 [L,QSA]
Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38