简体   繁体   中英

Permanent redirect 301 with /?lang=de

I need to redirect a multilingual website to a new domain and will use an .htaccess file for it. I redirect all old pages to their respective new ones on the new domain.

OLD URLS

Englisch
    Main page: www.olddomain.com
    Sub pages: www.olddomain.com/bike-rental

Spanish
    Main: www.olddomain.com/?lang=es
    Sub pages: www.olddomain.com/bike-rental?lang=es

German
    Main: www.olddomain.com/?lang=de
    Sub pages: www.olddomain.com/bike-rental?lang=de

NEW URL's

Englisch
    Main page: www.new-domain.com
    Sub pages: www.new-domain.com/bike-rental

Spanish
    Main: www.new-domain.com/es
    Sub pages: www.new-domain.com/es/alquiler-de-bicis
German
    Main: www.new-domain.com/de
    Sub pages: www.new-domain.com/de/fahrradvermietung

I realized it does not work with a normal redirect in the .htaacces file such as:

    Redirect 301 /?lang=de http://www.new-domain.com/de/
    Redirect 301 /bike-rental?lang=de http://www.new-domain.com/de/fahrradvermietung

Any ideas how to redirect those old URL's to the new ones?

Thanks

Since php 5.3.0 This variable was added:

$locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
echo $locale; // returns "en_US"
$localeshort = substr($locale, -2);
echo $localeshort; // returns "US"

If you use this, you can make a redirect with something like:

Header("Location: index.php?lang=".$localeshort);

OR use: this tool to create a redirect, repeat this until you have all languages. IMAGE

See my previous message: Since php this function was added:

$locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
echo $locale; // returns "en_US"
$localeshort = substr($locale, -2);
echo $localeshort; // returns "US"

Use in your .htacces

RewriteEngine on

RewriteRule ^(.*)$ /index.php [R=permanent,L]

Now it will be redirected to the page on the begin. This is the only solution i know.

You can do something like this in htaccess:

RewriteCond %{REQUEST_URI} !^/bike-rental$
RewriteCond %{QUERY_STRING} ^lang=(.*)$ 
RewriteRule ^(.*)/ /bike-rental?lang=%1 [L,R=301]

(Tested and added condition to avoid circular rewrite)

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