1

I have index.php where it set the language with index.php?lang=de. English is the default language which I don't need to use index.php?lang=en

When the language is set it use this condition in htaccess

RewriteRule ^([a-zA-Z0-9\-_]+)/$ /index.php?lang=$1 [L]

When the language is not set

RewriteRule ^([a-zA-Z0-9\-_]+)$ /index.php?pag=$1 [L]

When language and page is set

RewriteRule ^([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_]+)$ /index.php?lang=$1&pag=$2 [L]

When language, page, and sub-page is set

RewriteRule ^([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_]+)$ /index.php?lang=$1&pag=$2&spag=$3 [L]

But I can't access sub-page when the language is not set, when is english it must to be

 index.php?pag=services&spag=web_design
 RewriteRule ^([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_]+)$ /index.php?pag=$2&spag=$3 [L]

Any idea how to make it work? Also if you have an idea to merge all this line in a single one will be wonderful.

Thanks!

zmeutz
  • 37
  • 2
  • 10

2 Answers2

2

As far as I understand it, someone would access your page in the following format:

http://www.mysite.com/LANGUAGE/PAGE/SUBPAGE/

Something like:

http://www.mysite.com/ENGLISH/events/upcoming

would translate to:

http://www.mysite.com/index.php?lang=ENGLISH&pag=events&spag=upcoming

Which would take them to the English version of Events->Upcoming?

Solution

Assuming the above to be correct. The following will work:

ReWriteRule ^([\w-]*)/*([\w-]*)/*([\w-]*)/*$ index.php?lang=$1&pag=$2&spag=$3

Running var_dump($_GET) in index.php would then output the following:

// http://www.mysite.com/ENGLISH/home/upcoming
array(3) {
  ["lang"]=>
  string(7) "ENGLISH"
  ["pag"]=>
  string(4) "home"
  ["spag"]=>
  string(8) "upcoming"
}

If one isn't set then you place it as a zero (or leave it blank if at the end) in the url, like so:

http://www.mysite.com/0/home

Which would give:

array(3) {
  ["lang"]=>
  string(1) "0"
  ["pag"]=>
  string(4) "home"
  ["spag"]=>
  string(0) ""
}

Example URLs

http://www.mysite.com/ENGLISH/news/recent/
http://www.mysite.com/ENGLISH/about/
http://www.mysite.com/ENGLISH/
http://www.mysite.com/0/news/recent/
http://www.mysite.com/0/about/
http://www.mysite.com/0/

Order

You could also change the order... So that language comes last:

ReWriteRule ^([\w-]*)/*([\w-]*)/*([\w-]*)/*$ index.php?lang=$3&pag=$1&spag=$2

and use URLs like:

http://www.mysite.com/news/recent/ENGLISH
http://www.mysite.com/about/history/
http://www.mysite.com/about
Steven
  • 5,830
  • 2
  • 11
  • 26
  • I use switch($lang) ... default: $lang_file = 'lang.en.php'; and first solution works perfect, because en is default language and I don't need to set $_GET['$lang']... Thanks for solution! – zmeutz Sep 22 '13 at 06:31
  • Okay, erm, I must be missing something haha why is that a problem? – Steven Sep 22 '13 at 06:36
  • Just to follow up, you did get this to work for you in the end? – Steven Sep 22 '13 at 13:26
0

You can use these rules:

# just language
RewriteRule ^([a-z]{2})/?$ /index.php?lang=$1 [L,NC,QSA]

# just page
RewriteRule ^([\w-]{3,})/?$ /index.php?pag=$1 [L,QSA]

# lang & page
RewriteRule ^([a-z]{2})/([\w-]+)/?$ /index.php?lang=$1&pag=$2 [L,QSA,NC]

# page & spage
RewriteRule ^([\w-]{3,})/([\w-]+)/?$ /index.php?pag=$1&spag=$2 [L,QSA]

# lang, page & spage
RewriteRule ^([a-z]{2})/([\w-]+)/([\w-]+)/?$ /index.php?lang=$1pag=$2&spag=$3 [L,QSA,NC]
anubhava
  • 664,788
  • 59
  • 469
  • 547