2011-06-18 100 views
3

我試圖找到一個htaccess重寫刪除www。來自https:僅限網頁。我發現的每篇文章都會爲所有頁面移除www。從https刪除www從

在.htaccess中需要做什麼?

https://www.mydomain.com應該https://mydomain.com

https://mydomain.com應保持https://mydomain.com

http://www.mydomain.com應保持http://www.mydomain.com

http://mydomain.com應保持http://mydomain.com

我已經能夠得到以下工作:

RewriteCond %{HTTPS}s ^on(s)| 
RewriteCond http%1://%{HTTP_HOST}%{REQUEST_URI} ^(https?://)www\.(.+) [NC] 
RewriteRule^%1%2 [L,R,QSA] 

但這重寫HTTPS和HTTP

這裏是我當前的.htaccess

SetEnv DEFAULT_PHP_VERSION 5 

RewriteEngine on 
RewriteOptions MaxRedirects=1 

# You may need to uncomment the following line on some hosting environments, 
# for example on unitedhosting.co.uk 
# RewriteBase/


# The following line has been added in order to exclude the webim 
# directory from the LemonStand URL processing. 
RewriteCond %{REQUEST_URI} !^/webim 



# 
# Do not allow executing any PHP scripts 
# 

RewriteRule ^(.*).php$ index.php [L] 

# 
# The following section automatically adds a trailing slash to all URLs 
# 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$ 
RewriteCond %{HTTP:X-REQUESTED-WITH} !^(XMLHttpRequest)$ 
RewriteCond %{REQUEST_METHOD} !^HEAD$ 
RewriteCond %{REQUEST_METHOD} !^POST$ 
RewriteRule (.*)([^/])$ %{REQUEST_URI}/ [R=301,L] 

# The following line has been added in order to exclude the webim 
# directory from the LemonStand URL processing. 
RewriteCond %{REQUEST_URI} !^/webim 


# 
# Product files downloading URL 
# 

RewriteRule (^download_product_file/.*) index.php?q=/$1 [L,QSA] 

# The following line has been added in order to exclude the webim 
# directory from the LemonStand URL processing. 
RewriteCond %{REQUEST_URI} !^/webim 


# 
# Administration Area file downloading URL 
# 

RewriteRule ls_backend/files/get/(.*) index.php?q=/backend_file_get/$1 [L] 

# The following line has been added in order to exclude the webim 
# directory from the LemonStand URL processing. 
RewriteCond %{REQUEST_URI} !^/webim 


# 
# All other requests 
# 

RewriteCond %{REQUEST_URI} !(\.(ico|js|jpg|gif|css|png|swf|flv|txt|xml|xls|pdf|eot|woff|ttf|svg|mp4)$) 
RewriteCond %{REQUEST_URI} !(phproad/thirdpart/.*) 
RewriteRule ^(.*)$ index.php?q=/$1 [L,QSA] 

ErrorDocument 404 "File not found" 

# 
# PHP configuration 
# 

<IfModule mod_php5.c> 
php_flag session.auto_start off 
php_value session.cookie_lifetime 31536000 
php_flag session.use_cookies on 
php_flag session.use_only_cookies on 
php_value session.name FWCSESSID 

php_flag short_open_tag on 
php_flag asp_tags on 

php_flag magic_quotes_gpc off 
php_value date.timezone GMT 

php_value post_max_size 100M 
php_value upload_max_filesize 100M 

php_value memory_limit 264M 
</IfModule> 

回答

3
RewriteEngine On 

# Check that you're on port 443 and the hostname starts with www 
RewriteCond %{SERVER_PORT} ^443 
RewriteCond %{HTTP_HOST} ^www\. 

# Redirect to domain without the www 
RewriteRule (.*) https://example.com$1 [L,R,QSA] 
+1

服務器端口可能是非標準的(發生在多個SSL證書需要在同一個IP上服務時)。在這種情況下,%{SERVER_PORT}可以具有與標準443不同的值:/ – LazyOne

+0

這會將所有內容重定向到https:// – dardub

+0

@dardub我有一個錯誤,我可能會在您測試時糾正錯誤。 –

0

添加一個條件(您現有的重定向規則):

2

這應該工作:

RewriteEngine On 
RewriteCond %{HTTPS} =on 
RewriteCond %{HTTP_HOST} ^www\. 
RewriteRule ^(.*)$ https://mydomain.com/$1 [R,QSA,L] 

如果HTTPSonHTTP_HOSTwww.mydomain.com,然後將其重定向到https://mydomain.com/

+0

也許在我的.htaccess中有其他的東西搞砸了。但是這對我也不起作用。如果我輸入https:// www.mydomain.com,我仍然可以訪問https:// www.mydomain.com – dardub

+0

,我確實記得在您的解決方案中更改了mydomain.com部分:D – dardub

+0

好吧,我編輯了一下,但實際上我猜這不會改變這種情況。 – Floern

0

這將從httpshttp刪除www.,而無需添加您的域名。

# remove www. (generic method with HTTPS support) 
RewriteCond %{HTTP_HOST} ^www\. 
RewriteCond %{HTTPS}s ^on(s)|off 
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$ 
RewriteRule^%1%3%{REQUEST_URI} [R,L] 
+0

在https:// www上失敗,將重定向到http:// < – yckart