2014-11-08 78 views
-1

我更改了網站的網址,以接受不同的語言,所以,我從移動爲默認語言htaccess的301重定向到語言子目錄

網站內容:www.site.com到:www.site.com/en

現在,我從PHP做重定向,如果$_GET['lang']不存在或$_GET['path']存在,重定向到site.com/en或site.com/en/(path),但我認爲301從根目錄重定向到文件夾更好。

我該如何寫這條規則?

這是我的htaccess文件:

RewriteEngine on 
RewriteBase/

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] 
RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 

RewriteRule ^download/(.*)$ php/download.php?id=$1 [L] 

# with language 
RewriteRule ^([a-z]{2})/p/(.*)$ single.php?lang=$1&hash=$2 [L] 
RewriteRule ^([a-z]{2})$ index.php?lang=$1 [L,NC,QSA] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([^?]*)$ index.php?path=$1 [NC,L,QSA] 

感謝

+0

需要一些清晰度。什麼是URL的例子以及如何處理這些URL。 – anubhava 2014-11-08 15:31:02

回答

0

如果我的理解是正確的,這將做你想做的。

Updated .htaccess(與更好地理解意見)

RewriteEngine on 
RewriteBase/

# If the hostname is a `www` subdomain, redirect to the parent domain 
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] 
RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 

# Internal rewrite of Download request URL to download PHP script 
RewriteRule ^download/(.*)$ php/download.php?id=$1 [L] 

# If request path is just root `/` (redirect to default language site) 
RewriteRule ^$ /en [R=301,L] 

# If request path is a language code, 
# internally rewrite to index.php with `lang` query paramater 
RewriteRule ^([a-z]{2})$ index.php?lang=$1 [L,NC,QSA] 

# If request path begins with lang code and has a hash segment, 
# internally rewrite to single.php with `lang` and `hash` query parameters 
RewriteRule ^([a-z]{2})/p/(.*)$ single.php?lang=$1&hash=$2 [L] 

# If request path begins with lang code and has additional segements 
RewriteRule ^([a-z]{2})/(.*)$ index.php?lang=$1&path=$2 [NC,L,QSA] 

# For every other request path not having a language code, 
# if path is not an existing file or directory, 
# redirect to a path prefixing default lang code before the requested path 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([^?]*)$ /en/$1 [NC,L,R=301]