Welcome to WebmasterWorld Guest from 34.204.173.36

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

rewrite rules htacess multilingue

redirection switch the language code

     
7:31 am on Sep 25, 2018 (gmt 0)

New User

joined:Sept 25, 2018
posts: 3
votes: 0


Hello,
i need to write htaccess rewrite URL rules,
my urls can be on 2 formats:
1/ monsite.com/page.html ==> is the page page.php?lang= so it's page with default language
2/ monsite.com/en/page.html ==> is the page page.php?lang=en so it's page with language english

in page.php i do $_GET['lang'] to can know with which language i will display my data

How i can write this rule in my .htacess ? i haven't good knowledge on htaccess language

thanks
8:05 am on Sept 25, 2018 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:12913
votes: 893


Hello pws2018 and welcome to WebmasterWorld [webmasterworld.com]
5:08 pm on Sept 25, 2018 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15943
votes: 890


It isn't clear from your post what you're trying to do. Do you mean that you want to rewrite requests for page.html to page.php, with different "lang" values depending on the original URL? Are there any other parameters or will this be the only one? Do you want to retain the /en/ (or /fr/ or whatever) in the rewrite target, or is it all getting rewritten to the same place?

Use “example.com” for the domain name to prevent auto-linking.
7:54 pm on Sept 25, 2018 (gmt 0)

Senior Member

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

joined:Dec 15, 2003
posts:2645
votes: 7


Typically when you are creating this type of redirect you would do it in a piece of middleware. It is possible to force a lang using htaccess but you can't code in default langs nor can you check session variables containing a set lang.

Are you using a framework of some kind or is this a all custom handcoded?
8:27 am on Sept 26, 2018 (gmt 0)

New User

joined:Sept 25, 2018
posts: 3
votes: 0


thanks for your reply,

i need to set a rule for my page PHP page.php?lang='en' to write in navigator example.com/en/page.html or example.com/fr/page.html but is possible also to be example.com/page.html
all this format is for page.php?lang=

I use native PHP and in my script page.php i have $_GET['lang'] to get the language from URL
4:45 pm on Sept 26, 2018 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15943
votes: 890


:: looking vaguely around for nearest available French speaker ::

Does “page.html” mean some specific page, or are there many possible pages, each of which exists in parallel /en/ and /fr/ forms?

The one-size-fits-all version would be
RewriteRule ^(\w\w)/(\w+)\.html /$2.php?lang=$1 [L]
or, if there are never more than two languages,
RewriteRule ^(en|fr)/(\w+)\.html /$2.php?lang=$1 [L]
and, for the language-less version,
RewriteRule ^(\w+)\.html /$1.php [L]
You can cross-check in the php subforum, but I don't think you even need to say "lang=" (with no value) since you can so easily write the php* to do the same thing, whether the parameter is absent or zero or undefined. In fact you probably should do the php that way.


* As we all know, I speak about three words of php, and empty("blahblah") is one of them. Someone hereabouts taught it to me.
6:22 pm on Sept 26, 2018 (gmt 0)

Senior Member from FR 

WebmasterWorld Senior Member leosghost is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Feb 15, 2004
posts:7139
votes: 413


:: looking vaguely around for nearest available French speaker ::


pm sent ( en Français ) to pws2018..( whose English is good as far as I can tell, but .htaccess is complex to understand / explain even for most English speakers ) will get back to the thread with clarification if I get a reply.. :)

Personally, if they are trying to do what I think they are, I wouldn't use .htaccess , I'd just use "mirror image" separate language pages, but..without a clear explanation, I could be misunderstanding their objectives..
6:46 pm on Sept 26, 2018 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15943
votes: 890


Come to think of it ... php can read the whole URL perfectly well, so all you have to do is check whether the URLpath contains an element like /fr/ and take it from there. But it you're already rewriting from .html to .php* you might as well do it all in one fell swoop.


* I love doing this, because then it looks as if it's just an ordinary hard-coded html page. If you rewrite from extensionless, the user pretty well knows something has been rewritten.
4:15 pm on Sept 27, 2018 (gmt 0)

Senior Member

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

joined:Dec 15, 2003
posts:2645
votes: 7


When you have your solution set up one thing to make sure is that you do a "302" not a "301" redirect, and add to this response header

'Vary' => 'Accept-Language'

This is best practice for Search Engines to understand the redirect and index accordingly.

I would also make sure you add the lang tag to the <head> tag of your pages
<head lang="en">

As well for your sitemap you will want to use the xmlns:xhtml schema
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">

Then form your sitemap entries like this:
<url>
<loc>https://example.com/en/page.html</loc>
<lastmod>2018-06-14</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
<xhtml:link rel="alternate" hreflang="fr-ca" href="https://example.com/fr/page.html" />
</url>

Doing these three things will make sure Search Engines understand your structure, and that these aren't 2 seperate pages, but 1 page in 2 langs.
4:32 pm on Sept 27, 2018 (gmt 0)

New User

joined:Sept 25, 2018
posts: 3
votes: 0


ok thanks for your help :)