2017-10-04 169 views
1

我需要一個域來管理語言(en/it/pt/es)以及刪除www。和力HTTPS多種語言+ Htaccess

 
    RewriteEngine On 
    # -> www.example.com to example.com 
    RewriteCond %{HTTP_HOST} www.example.com 
    RewriteRule (.*) http://example.com/$1 [L,R=301] 
    # -> http -> https 
    RewriteCond %{SERVER_PORT} 80 
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

我不知道如何使這個重寫規則: 的index.php LG = ES - >/ES /不/ ES或/es.php甚至拒絕的index.php ?LG的= ES

RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?lg=$1

??

的SEO是壞的,我的網站只有英語是由發動機和其他語言已知的有結果的第4頁..

它是爲什麼,因爲我有兩個方式來顯示URL的原因?

example.com/it/和example.com/it不應該返回同一頁面

感謝您的幫助。

EDIT(我覺得一切都是正確的?):10月07日感謝所有的答案:

 
     RewriteEngine On 
     RewriteCond %{HTTP_HOST} ^www.example.com$ [OR] 
     RewriteCond %{SERVER_PORT} 80 [OR] 
     RewriteCond %{HTTPS} off 
     RewriteRule ^(.*)$ https://example.com/$1 [R=301,L] 
     RewriteRule ^/?([a-z]{2})$ /$1/ [R=301,NC,L] 
     RewriteRule ^/?([a-z]{2})/$ /index.php?lg=$1 [NC] 
     RewriteCond %{ENV:REDIRECT_STATUS} ^$ 
     RewriteCond %{REQUEST_URI} ^/?index.php$ 
     RewriteCond %{QUERY_STRING} ^lg=([^&]+)(&.*)?$ 
     RewriteRule ^/?index.php$ - [F] 
    ErrorDocument 404 /404.php 

只是一個問題:是測試如果HTTPS過的真有用(慢速連接?)?感謝

編輯2018

2月8日之後使用的幾個月也有很多404錯誤。 Htaccess需要更簡化

我只需要:

1) www -> non-www 
2) http -> https 
3) index.php -> example.com 
4) redirect index.php?lg=it and index.php?lg=es and index.php?lg=pt 
    to example.com/it or to example.com/es or to example.com/pt 

,如果不是這門語言的一個 - >去主頁example.com或404?但谷歌網站管理員將增加404錯誤...

我需要一個非常簡化版本的htaccess與3個語言與其中的重寫基地?或不 ?

這裏是我的htaccess:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^www.example.com$ [OR] 
RewriteCond %{SERVER_PORT} 80 [OR] 
RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L] 
RewriteRule ^/?([a-z]{2})$ /$1/ [R=301,NC,L] 
RewriteRule ^/?([a-z]{2})/$ /index.php?lg=$1 [NC] 
RewriteCond %{ENV:REDIRECT_STATUS} ^$ 
RewriteCond %{REQUEST_URI} ^/?index.php$ 
RewriteCond %{QUERY_STRING} ^lg=([^&]+)(&.*)?$ 
RewriteRule ^/?index.php$ - [F] 
ErrorDocument 404 /404.php 

一切都在index.php管理,是它更好地創建3個頁面?

的index.php - >重定向到example.com

es.php - >重定向到example.com/es

it.php - >重定向到example.com/it除去.PHP

有人可以做到嗎?由於

+0

我們能不能順便告訴你你的SEO是壞的。有太多可能的原因,因此這是錯誤的地方來討論它。 – Webdesigner

+0

如果我們有像example.com/it/some-page一個網址......而example.com/it/和example.com/it應該提供相同的頁面恕我直言,如果不是請解釋爲什麼 – Webdesigner

+0

謝謝應該發生什麼回覆,我認爲example.com/it/和example.com/it可能被認爲是重複的內容?在許多網站/重定向到/它/ – Lorenzo

回答

0

這應該爲你工作的認可:

RewriteEngine On 
# redirect to none-www and https 
# if host do not starts with www. 
RewriteCond %{HTTP_HOST} ^www\.(([A-Za-z0-9][A-Za-z0-9-]+\.)+[A-Za-z]{2,})$ [OR] 
# or if Port is 80 
RewriteCond %{SERVER_PORT} 80 [OR] 
# or if https is off 
RewriteCond %{HTTPS} off 
# redirect to example.com with https 
RewriteRule ^(.*)$ https://%1/$1 [R=301,L] 

# Redirect Language Strings without Trailing Slashes 
RewriteRule ^/?([a-z]{2})$ /$1/ [R=301,NC,L] 

# Rewrite Language string 
RewriteRule ^/?([a-z]{2})/$ /index.php?lg=$1 [NC] 

# Prevent direct access to /index.php?lg=$1 [QSA,NC,L] 
RewriteCond %{ENV:REDIRECT_STATUS} ^$ 
RewriteCond %{REQUEST_URI} ^/?index.php$ 
RewriteCond %{QUERY_STRING} ^lg=([^&]+)(&.*)?$ 
RewriteRule ^/?index.php$ - [F] 

我測試了這種積極的一個Ubuntu LTS 03年4月16日與Apache 2.4。27

+0

感謝簡化。只有最後一次重寫條件不起作用。禁止訪問:您無權訪問此服務器上的/index.php。我仍然需要強制/ lg到/ lg /是否正確? :?重寫規則^(EN |它| PT | ESP)\/$的index.php LG = $ 1 [QSA,NC,L] – Lorenzo

+0

和example.com/lg/text不可能只是example.com/lg/ – Lorenzo

+0

好,我做了多個測試,它不工作:一些規則可以www->非www,http - > https,但example.com/es/ - > 404錯誤,example.com/es - >仍然沒有重定向到example.com/es/它不是我的網站,但我想要的東西像dafont.com/es - > www.dafont.com/es/(斜槓總是添加)www.dafont.com/es.php - > 404。我只需要2個字母index.php?lg = es不是index.php?lg = es&p = home,所以只需要$ 1。重定向index.php?lg = es - >/es /即使添加了更多參數。再次感謝 – Lorenzo

1

該解決方案是好的,因爲可以應用到任何結構域(沒有硬代碼名稱)。

一些注意事項:

  1. 刪除WWW。 (無域名硬編碼)
  2. 強制使用HTTPS(在開發環境可以與HTTP工作)
  3. 添加八方通斜線
  4. 定義你的默認主頁,如果未指定頁面名稱
  5. 映射語言和頁面從URL(內部)查詢字符串

請注意,4和5在公共/文件夾中有index.php文件...如果需要,請更改或刪除它。


# ############################################################################### 
# | Rewrite engine (SEO URLs friendly)           | 
# ############################################################################### 
<IfModule mod_rewrite.c> 

    RewriteEngine On 
    Options +FollowSymlinks 

    ## Process pages requests 

    # 1. To redirect from www to non www (Rewrite www.example.com → example.com) 
    RewriteCond %{HTTP_HOST} ^www\.(.+) [NC] 
    RewriteRule ^(.*) https://%1/$1 [R=301,NE,L] 

    # 2. Redirect HTTP to HTTPS automatically (only if not in localhost) 
    RewriteCond %{HTTP_HOST} !=localhost 
    RewriteCond %{HTTPS} off 
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

    # 3. (Add a slash) aa → aa/ 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^([^./]+)$ $1/ 

    # 4. (Default homepage) aa/ → ?lang=aa&page=home 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^([^./]+)/$ public/index.php?lang=$1&page=home 

    # 5. (Language and page) aa/bb → ?lang=aa&page=bb 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^([^./]+)/([^./]+)$ public/index.php?lang=$1&page=$2 

</IfModule> 
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
+0

是的,你是對的,但如果你有http:// www。 example.com這將最終在2個重定向中首先從非www到www,而不是從http到https。 – Webdesigner

+0

@Webdesigner這是一個很好的觀點。我將(#1.)更改爲https:// – SandroMarques

+0

感謝您的代碼:但主頁example.com不起作用:連接不可能,可能是循環...當我更改代碼時,我無法顯示主頁但/ es /不添加/ es /和/ es是一樣的,我真的需要重定向/ es - >/es/like dafont.com/es - > dafont.com/es/ make it(它不是我的網站) 。謝謝。 – Lorenzo