Forum Moderators: phranque

Message Too Old, No Replies

Translating URL on the fly, same PHP file

         

bertrand

8:07 pm on Mar 5, 2009 (gmt 0)

10+ Year Member



Hi!

I'm fairly new to all this « mod_rewrite » thing but it seems to be the only way to help me with something.

Here's what I have to do :

Let's say I have this website « www.website.com ». I'd like any URLs to point to the same file for 3 URLs (languages) entered. Example : www.website.com/nouvelles.php should be displayed in the adress bar as www.website.com/nouvelles.php BUT use the file news.php to display the content of the website.

In short :

www.website.com/nouvelles.php
www.website.com/destacados.php
www.website.com/news.php

...should use the file « news.php » to display the content BUT the URL should stay as entered.

Is « mod_rewrite » the answer to this?

Thanks a lot for your precious time!

g1smd

8:28 pm on Mar 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, a rewrite (that's a rewrite not a redirect) will connect a URL to a different internal filepath.

bertrand

8:39 pm on Mar 5, 2009 (gmt 0)

10+ Year Member



Ok thanks!

I seem to have a problem thought;

The language is stored in a cookie. Any idea how I could access that information in the .htaccess file to change the URL displayed to (say, spanish as an example) "destacados.php" but use the file "news.php"?

Also, I'd hate to have to change all the links site-wide...!

Thanks.

jdMorgan

8:55 pm on Mar 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See the Apache mod_rewrite documentation, RewriteCond %{HTTP_COOKIE}

Jim

bertrand

7:26 pm on Mar 10, 2009 (gmt 0)

10+ Year Member



Damn!

I have a hard time figuring this one out. Let's break it down:

I need the htaccess to filter every link requests;

If it sees it's a french or spanish cookie identified user;

Change the displayed URL to a language equivalent URL;

Feed the right file.php (not translated)
...

Let's use news.php as an example.

...

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} langue=(Fr) [NC]
RewriteCond %{REQUEST_URI} !^/(news\.php)$
...
is it next -> RewriteRule ^(news\.php)$ /$1nouvelles\.php [NC,L]

?

bertrand

8:18 pm on Mar 10, 2009 (gmt 0)

10+ Year Member



I've read a lot of topics again and think I found something rather disturbing :

A Rewrite does not *make* a URL. A rewrite changes nothing on the pages of your site. A rewrite changes nothing out there on the web.

A rewrite takes an incoming URL request arriving at your server and silently changes the value for the internal filepath that was hinted at in the URL to be a different internal filepath inside the server, and gets the content from there.

That is, a rewrite takes a URL request for www.example.com/22/44 and instead of the server looking in /var/public_html/www/yoursite/22/44 on the hard disk, the rewrite changes the path part to instead get the content from /var/public_html/www/yoursite/catalog.php?category=22&product=44 without letting anyone back out on the web know that that has happened.

Hmmm... does that mean I'll have to change every link across the website? Please don't say yes...! :(

g1smd

8:26 pm on Mar 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You will need to link to the URL that users will 'see' and 'use' on the web.

jdMorgan

11:30 pm on Mar 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you wish the URLs nouvelles.php, destacados.php, and news.php to all resolve to the file news.php, then no language cookie testing is needed:

RewriteEngine On
RewriteBase /
#
RewriteRule ^(nouvelles¦destacados)\.php$ news\.php [L]

Replace the broken pipe "¦" character with a solid pipe character before use; Posting on this forum modifies the pipe character.

You will have to change all links to point to nouvelles.php for French and destacados.php for (Spanish or Portuguese?). Since you are using PHP, this should be fairly trivial to do.

Jim

g1smd

11:49 pm on Mar 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The PHP script can then detect what the originally requested URL was, and go from there.

bertrand

12:37 pm on Mar 11, 2009 (gmt 0)

10+ Year Member



Yeah that's what I was affraid I'd have to do. I'll have to make a fairly good check after to see if all links resolves right.

Thanks for your time guys, really appreciated.