从https删除www

时间:2011-06-18 22:57:32

标签: .htaccess ssl rewrite

我正在尝试找到htaccess重写以删除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>

6 个答案:

答案 0 :(得分: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 :(得分: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/

答案 2 :(得分:1)

我们可以通过apache virtualhost

实现这一目标
 <VirtualHost *:80>
 ServerName www.example.com
 Redirect / https://example.com/
 </VirtualHost>

<virtualHost *:443>
 DocumentRoot /var/www/html/example.com
 ServerName example.com 
</VirtualHost>

答案 3 :(得分:0)

再添加一个条件(到现有的重定向规则):

RewriteCond %{HTTPS} ^on$

答案 4 :(得分:0)

这将从www.https中删除http,而无需添加您的域名。

# 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]

答案 5 :(得分:0)

Mu arquivo最终.htaccess广告,网址为httpstambém。

我的整理文件.htaccess内部使用路径url(例如:example.com/intranet):

import 'package:flutter/material.dart';

class UserSearch extends SearchDelegate {
  @override
  String get searchFieldLabel => 'Find other users';

  @override
  ThemeData appBarTheme(BuildContext context) {
    return Theme.of(context);
  }

  @override
  List<Widget> buildActions(BuildContext context) {
    // TODO: implement buildActions
    return [
      IconButton(
        color: Colors.orange,
        iconSize: 30,
        icon: Icon(Icons.backspace),
        onPressed: () => query = '',
      )
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    // TODO: implement buildLeading
    return IconButton(
      color: Colors.orange,
      iconSize: 30,
      icon: Icon(Icons.close),
      onPressed: () => close(context, null),
    );
  }

  @override
  Widget buildResults(BuildContext context) {
    // TODO: implement buildResults
    return ListView();
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    // TODO: implement buildSuggestions
    return Column();
  }
}