2016-10-05 41 views
1

我就面臨一個很奇怪的問題重定向example.comwww.example.com的.htaccess - 如何非www重定向到www

我有衝浪互聯網,並試圖很多東西像如下

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

但如果我的網址是它僅適用,example.com/something然後將其重定向到www.example.com/something但它不工作正常簡單的域名。

它並不作品example.com只有

我已經嘗試了其他選項,如下面

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^(.*)$ [NC] 
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC] 
RewriteCond %{HTTPS}s ^on(s)| 
RewriteRule^HTTP%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

另外一個,

RewriteEngine On 
RewriteCond %{HTTP_HOST} !^$ 
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteCond %{HTTPS}s ^on(s)| 
RewriteRule^http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

我的全.htaccess文件如下

RewriteEngine On 
RewriteBase/

# If an existing asset or directory is requested go to it as it is 
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR] 
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d 
RewriteRule^- [L] 

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

#RewriteCond %{HTTP_HOST} !^$ 
#RewriteCond %{HTTP_HOST} !^www\. [NC] 
#RewriteCond %{HTTPS}s ^on(s)| 
#RewriteRule^http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

# If the requested resource doesn't exist, use index.html 
RewriteRule ^(.*) #/$1 

<IfModule mod_deflate.c> 
    # Compress HTML, CSS, JavaScript, Text, XML and fonts 
    AddOutputFilterByType DEFLATE application/javascript 
    AddOutputFilterByType DEFLATE application/rss+xml 
    AddOutputFilterByType DEFLATE application/vnd.ms-fontobject 
    AddOutputFilterByType DEFLATE application/x-font 
    AddOutputFilterByType DEFLATE application/x-font-opentype 
    AddOutputFilterByType DEFLATE application/x-font-otf 
    AddOutputFilterByType DEFLATE application/x-font-truetype 
    AddOutputFilterByType DEFLATE application/x-font-ttf 
    AddOutputFilterByType DEFLATE application/x-javascript 
    AddOutputFilterByType DEFLATE application/xhtml+xml 
    AddOutputFilterByType DEFLATE application/xml 
    AddOutputFilterByType DEFLATE font/opentype 
    AddOutputFilterByType DEFLATE font/otf 
    AddOutputFilterByType DEFLATE font/ttf 
    AddOutputFilterByType DEFLATE image/svg+xml 
    AddOutputFilterByType DEFLATE image/x-icon 
    AddOutputFilterByType DEFLATE text/css 
    AddOutputFilterByType DEFLATE text/html 
    AddOutputFilterByType DEFLATE text/javascript 
    AddOutputFilterByType DEFLATE text/plain 
    AddOutputFilterByType DEFLATE text/xml 

    # Remove browser bugs (only needed for really old browsers) 
    BrowserMatch ^Mozilla/4 gzip-only-text/html 
    BrowserMatch ^Mozilla/4\.0[678] no-gzip 
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html 
    Header append Vary User-Agent 

</IfModule> 

Options -Indexes 

有人能幫我解決這個問題嗎?

+0

仍然無法正常工作。我不明白爲什麼它不適用於** example.com **。它只適用於** example.com/something ** –

+0

如果您在.htaccess中還有其他規則,請發佈完整的.htaccess – anubhava

回答

2

讓你的規則是這樣的:

RewriteEngine On 
RewriteBase/

RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteCond %{HTTPS}s ^on(s)| 
RewriteRule^http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE] 

# If an existing asset or directory is requested go to it as it is 
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR] 
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d 
RewriteRule^- [L] 

重要的是保持www規則作爲你的第一個規則,否則你跳過的文件/目錄規則將跳過登陸頁面。

+1

它現在正在工作..我浪費了很多時間。感謝Anubhava .. :) –