Link to home
Start Free TrialLog in
Avatar of Cesar Aracena
Cesar AracenaFlag for Argentina

asked on

How to translate path in URL with PHP?

Hello everybody,

I'm making a site with only a handful of folders/pages in it so instead or rewriting the URLs I'm making folders with only an index.php page in them (which includes everything it needs to) and then linking directly to those folders. In a nutshell my site's structure is something like the following:

mysite.com
mysite.com/category/
mysite.com/category/sub-category1/
mysite.com/category/sub-category2/

The problem I have is that the site is multilingual and I hate the idea of not having the URL displayed in the visitor's language. Is it possible (maybe using the DB?) to have something like:

mysite.com/category/sub-category1/ (if visitor chooses English)
mysite.com/categoria/sub-categoria1/ (if visitor chooses Spanish)

Thanks in advance!
SOLUTION
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Cesar Aracena

ASKER

Thanks for the inputs.

Actually Ray they were two separate questions (one on how to handle variables and the other on how to structure correctly my site) but yes, it seems I can work on one while making the other.

Cheers!
I'm not that much of an opponent of regular expressions as used in URL Rewriting. You can have the language as part of the URL, eg www.yoursite.com/en/ and www.yoursite./es/ and turn it to an input parameter via a rewrite rule using regexpression. You can limit this to a) avoid search engines seeing duplicate context in www and the domain without any subdomain prefix, mainly by doing a 301 redirect and b) rewrite all URLs to call one main controller (ie index.php at root level) and do further routing there based on either URL (partly) rewritten as parameters or via parsing $_SERVER['REQUEST_URI'] and $_SERVER['REQUEST_METHOD'].

Yes, these are not necessarily coming in from your own links only, a user can type anything in a browser (or program winsock or use curl), so one (small) safety already can be naming your controller different than index.php, so manually (re)written URLs don't guess that. Even then the parts of the URL rewritten as get parameters can be manually entered, so you never should blindly accept input. You typically will have a whitelist (perhaps data driven) and a fallback for wrong urls, which mostly will be a 404 (not found) redirect.

Obviously if you only have the domain name in the URL you don't go to the 404 page, but display a homepage with default language.

Bye, Olaf.
You imply you have a small set of products, so a non dynamic approach also is possible. You can have that real directory structure and prepare all product pages offline to have a static website in that aspect. Or (as Pavel already suggests in his article) have three controlers for the boilerplate template in each language. JKust avoid to combine such a language specific template with the same content, only have english content for the english part of the site and spanish content for the spanish part. They don't have to always be available in both languages.

Bye, Olaf.
Ok, here's what I've done so far after reading that wonderful guidelines from google and your last comment:

- English site: mysite.com/en/something/somethingelse
- Spanish site: mysite.com/es/something/somethingelse
- Default site if user hasn't visited before or in the past year: /es/ or /en/ depending on browser's locale but changeable with a language selector

What I did is make the folders I need with a simple index.php inside each one and each index.php calls the same template files every time and a specific file for the content. That file is not stored inside the folder. Something like:

/index.php (to redirect users and set cookies)
/home.file
/aboutus.file
/en/index.php
/en/aboutus/index.php
/es/index.php
/es/aboutus/index.php

What do you think?
If all the index.php do the same, think of the DRY principle. If you ever want to change what is done here you have to edit and copy that edit to 5 places. You don't do that.

This is where URL rewriting is in play and turns a url like mysite.com/en/something/somethingelse into mysite.com/index.php?lang=en&category=something&product=somethingelse
At least you could do a rewrite rule splitting the url in one place:

index.php?partialurl=/en/something/somethingelse

And then use explode on that, filter input and load the data in the language.

Bye, Olaf.
Thanks for all the input. I'd already setup everything using most of the comments and suggestions. I do however still need to look into the URL rewriting. Haven't done it in a long time (don't know why but I always thought it wasn't fool-proof) but I'll do it to make later corrections easier.
You can find so many examples of apache url rewrite rules, that you don't need to deep dive into this. And once you have it, you can reuse it.

Bye, Olaf.