Reputation: 3195
I'm trying to find a htaccess rewrite to remove www. from https: pages only. Every article I've found removes www for all pages.
What do I need in .htaccess to do this?
https://www.mydomain.com should be https://mydomain.com
https://mydomain.com should remain https://mydomain.com
http://www.mydomain.com should remain http://www.mydomain.com
http://mydomain.com should remain http://mydomain.com
I've been able to get the following working:
RewriteCond %{HTTPS}s ^on(s)|
RewriteCond http%1://%{HTTP_HOST}%{REQUEST_URI} ^(https?://)www\.(.+) [NC]
RewriteRule ^ %1%2 [L,R,QSA]
But this rewrites both https and http
Here is my current .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>
Upvotes: 3
Views: 10863
Reputation: 11
My finishing file .htaccess inner using path url (ex: example.com/intranet):
Options -Indexes
Options +FollowSymLinks
Options -MultiViews
DefaultLanguage pt-BR
ServerSignature Off
DirectoryIndex index.php
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/intranet/$1 [L,R,QSA]
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
Upvotes: 0
Reputation: 4254
We can achieve this via 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>
Upvotes: 1
Reputation: 6277
This will remove www.
from both https
and http
, and without having to add your domain.
# 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]
Upvotes: 0
Reputation: 33904
This should work:
RewriteEngine On
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://mydomain.com/$1 [R,QSA,L]
If HTTPS
is on
and HTTP_HOST
is www.mydomain.com
, then it redirects to https://mydomain.com/
.
Upvotes: 2
Reputation: 165148
Add one more condition (to your existing redirect rule):
RewriteCond %{HTTPS} ^on$
Upvotes: 0
Reputation: 270617
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]
Upvotes: 4