Charlie
Charlie

Reputation: 197

Rewrite Rule for Multiple Languages

At the moment the website uses no rewrites, I'm in the process of rewriting both the website and the .htaccess to encorporate this. It is multilingual, and at the moment the language is changed via a query string (?lang=en/es/de).

I would like to format the URL so it reads http://website.com/en/products/product1

At the moment the pages are stored as separate files, in other words it's not a complete rewrite such as: http://website.com/index.php?page=product1&category=product&lang=en

Becomes:

http://website.com/en/products/product1

The pages are all stored in the root directory, but I have used an individual rule to rewrite the separate files (as it is only a small site).

The problem I'm having is, the regex solutions I have found elsewhere replace the ?lang=en part and then they place it at the end of the URL. How could I make it come directly after the domain in the URL as shown above?

Hope this is clear, thanks in advance.

Upvotes: 0

Views: 336

Answers (2)

anubhava
anubhava

Reputation: 784958

Put this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+index\.php\?page=([^&]*)&category=([^&]*)&lang=([^&\s]*) [NC]
RewriteRule ^ /%3/products/%1? [R=302,L]

# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?page=$3&category=product&lang=$1 [L,QSA]

Upvotes: 1

Akihiko Arihyoshi
Akihiko Arihyoshi

Reputation: 21

I think this hope /* copy and paste below, save the name. htaccess */

RewriteEngine On
RewriteBase /website.com/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
#ex1:http://website.com/en/products/product1
RewriteRule ^([a-zA-Z_-]+)/([a-zA-Z_-]+)/([a-zA-Z_-]+)?$ index.php?lang=$1&category=$2&page=$3 [L]

/* and in your file php */

<!-- #ex1:http://website.com/en/products/product1 -->
<a href="<?php echo "en/products/product1" ?>">Link</a>

Upvotes: 0

Related Questions