1

I've just searched a lot here and googled about this implementation, but I can't figuring out.

I mean: I have a portfolio site (one single page) with 3 languages. Every time I click on a flag (IT, EN, FR) it will load a php file. Inside it there are many translated sentences/words based on choosed languages.

**Question:
The final url is like: localhost/site/index.php?lang=en (if you click on English lang).
I want to rewrite it like: localhost/site/en

How to write the htaccess file? **

I just followed this tip --> Simulate file structure with PHP.

And this one:

RewriteEngine on
RewriteRule ^en/(.*)\.html$ /$1.php?language=english [L,QSA]

I can't figuring out why isn't working.

Other details:
- wamp server
- mod_rewrite activated
- single page site (jquery)
- firefox

Thank you.

Community
  • 1
  • 1
zorta86
  • 49
  • 1
  • 8

2 Answers2

0

Your .htaccess wants to be something along these lines:

IndexIgnore */*

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php

Will direct any requests to files that don't exist to your index.php.

Then in the PHP check the URL and serve up as appropriate:

$uri = $_SERVER["REQUEST_URI"];
$uriParts = explode("/", $uri);
$language = end($uriParts);

if ($language == "en) {
....

Worth throwing a 404 if it doesn't match an expected language.

Dan
  • 1,870
  • 16
  • 28
  • I will try this evening, thank you. Last questions: I'm using WAMP, it may cause problems? I have to pay attention to write my htaccess with WAMP? And where should be created, under root-localhost or my "site" folder? – zorta86 Mar 20 '13 at 14:13
  • any kind of solution doesn't work. Now I suppose is because of WAMP configuration or I miss something in RewriteBase rule. Any help is appreciated, thanks. – zorta86 Mar 20 '13 at 22:32
  • Have you got url rewriting enabled? http://stackoverflow.com/questions/869092/how-to-enable-mod-rewrite-for-apache-2-2 – Dan Mar 21 '13 at 08:58
  • Yep, see "Other details" in my post. Rewriting system works but the problem is that if I use a simple rule to rewrite `index.php?lang=en` into `en/` or `en`, these URL point to nothing (404). If I click on this link --> `localhost/mysite/index.php?lang=en` I see `localhost/mysite/en` but it say page not found. I'm using WAMP, under www folder there is `mysite` folder: this may cause problem with htaccess? – zorta86 Mar 21 '13 at 09:52
0

This will work

RewriteRule ^(.*).html index.php?lang=$1 [L]

Your url will be localhost/site/en.html

msturdy
  • 9,343
  • 8
  • 38
  • 52