简体   繁体   中英

How to htaccess url rewrite multi language

Can someone help me with the htaccess rewrite url?

  1. I need to convert ?lang=en to /en
  2. To always display the language used in the url
  3. When someone comes to example.com to automatically 301 redirect them to example.com/en as the main language.
  4. When a user chooses another language to stay on the same page eg example.com/en/contact.php

I have a total of three languages on the site en, de, hr.

The problem with my code is when I go to example.com/contact and select a language, it takes me back to the homepage example.com/en. If I type manually example.com/en/contact it works.

If I am at example.com/en/contact and choose another language eg /de url changes to example.com/en/de and a 404 error.

My current htaccess code looks like this

# Convert ?lang=en in /en
RewriteRule ^([a-z]{2})/?$ ?lang=$1 [QSA,L]
RewriteRule ^([bs|en|de]{2})/(.*)$ $2?lang=$1&%{QUERY_STRING} [L,QSA]

# Remove trailing slash from URL
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://example.com/%1 [R=301,L]

# Remove php extension in URL
RewriteCond %{REQUEST_FILENAME}\.php -f   
RewriteRule ^(.*)$ $1.php

With your shown samples, could you please try following. Please make sure to clear your browser cache before testing your URLs.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !ON
RewriteCond %{HTTP_HOST} ^(?:www\.)reg\.bmi\.id$ [NC]
RewriteRule ^/?$ https://reg.bmi.id [R=301,L]

RewriteRule ^index\.php$ - [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\scontact/contact\.php [NC]
RewriteRule ^ https://reg.bmi.id [R=301]
RewriteRule ^ contact/contact.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM