lundi 1 octobre 2018

Setting the Right .htaccess Settings for Language Handling

I have the following case:

https://example.com/index.php?id=4 fetches a page and its alias from a database and rewrites it correctly. I am using the following htaccess rules for this:

RewriteEngine On
RewriteBase /

# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

The above works correctly.

I now have an addition to this with the language where I am adding a Get Parameter called lang.

So for example, https://example.com/index.php?id=4 results in https://example.com/about and the page shows correctly. To get the French version of it, I type https://example.com/about?lang=fr. This also works correctly and fetches the correct page details.

What I am trying to do is make the page accessible via https://example.com/fr/about in the above example.

To do this I have done some additions to my .htaccess as follows:

RewriteEngine On
RewriteBase /

# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

# If just the language is specified (ie example.com/en)
RewriteCond %{REQUEST_URI} ^/..$ [NC]
RewriteRule ^(.*)$ $1/

# If no language subfolder, default to 'en'
RewriteCond %{REQUEST_URI} !^/../ [NC]
RewriteRule ^(.*)$ /en$1 [R=301]

# If no page specified, default to home.
RewriteCond %{REQUEST_URI} !^/../.+ [NC]
RewriteRule ^/(..) /$1/ [R=301]

# If no ln query, tack it on
RewriteCond %{QUERY_STRING} !lang= [NC]
RewriteRule ^/(..)/(.*) /$1/$2?lang=$1 [R=301]

But I am getting an infinite redirect loop and the URL shows as follows:

https://example.com/enindex.php?q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=enindex.php&q=en

It seems like I'm almost there, can somebody help on this?




Aucun commentaire:

Enregistrer un commentaire