remove www from https - .htaccess

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>

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]

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/.

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>

Add one more condition (to your existing redirect rule):
RewriteCond %{HTTPS} ^on$

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]

Meu arquivo final .htaccess sendo usado dentro de uma pasta e com redirecionamento de https também.
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]

Related

Redirect HTTP to HTTPS on each and every page of website

I finally installed SSL on my website www.giftdays.jp. it works flawlessly.
It shows green padlock "secured" like I wished for.
But the problem is it only shows on main page. Whenever I access other page such as product page and all, green padlock is gone, and just shows exclamation inside circle. ( It doesn't show NOT SECURED WITH CROSSED HTTPS) .
After opening inspects from chrome, I get MIXED CONTENT error .
This is the exact error.
"Mixed Content: The page at 'https://www.giftdays.jp/products/list.php?mode=search&sps=5' was loaded over HTTPS, but requested an insecure image 'http://www.giftdays.jp/special/list_header/happywedding/images/happywedding.png'. This content should also be served over HTTPS."
Though my site is loaded on HTTPS , why isn't it showing green padlock?
By googling and all, I ended up in stack with someone posting some htaccess post which i added on my htaccess and uploaded on server. Code I added was :
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Yet no changes on other pages of website.
This is what`s inside my htaccess file :
# 基本は SC_Initial.php で設定するが、ini_setで反映されないものはここで設定する
php_value mbstring.language Japanese
php_flag mbstring.encoding_translation off
php_value output_handler none
php_flag magic_quotes_gpc off
php_flag session.auto_start 0
# INI_ALL なのにもかかわらず, ini_set で指定しても反映されない環境がある...
php_value mbstring.internal_encoding UTF-8
# デフォルトテンプレートの状態で 2M 近くになるため
php_value upload_max_filesize 5M
#php_value post_max_size 8M
php_flag register_globals off
RewriteEngine on
Options -Indexes
###################
# メンテナンス中
###################
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/503.php
RewriteCond %{REQUEST_URI} !^/maintenance.html
RewriteCond %{REQUEST_URI} !^/Gds1q2w3eAdmin/
RewriteCond %{REQUEST_URI} !^/user_data/
RewriteCond %{REMOTE_ADDR} !=114.179.83.200
RewriteCond %{REMOTE_ADDR} !=54.250.233.6
RewriteCond %{TIME_YEAR}%{TIME_MON}%{TIME_DAY}%{TIME_HOUR}%{TIME_MIN} >201403311500
RewriteCond %{TIME_YEAR}%{TIME_MON}%{TIME_DAY}%{TIME_HOUR}%{TIME_MIN} <201406011000
RewriteRule ^.*$ /503.php [L]
###################
# 通常使用
###################
RewriteRule ^guide/(.*)$ user_data/guide/$1
RewriteRule ^products/shop_map.php user_data/products/shop_map.php
RewriteRule ^concierge/.* user_data/concierge/$1
RewriteRule ^original/(.*)$ user_data/original_catalog/$1
RewriteRule ^gift/(.*)$ user_data/gift/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#商品ページのURL書き換え
RewriteRule ^gifts/([0-9]+)/$ products/detail.php?product_id=$1 [QSA,L]
RewriteRule ^gifts/([0-9]+)/shop_map.php user_data/products/shop_map.php
#カタログページのURL書き換え
RewriteRule ^catalog/price-([0-9]+)/$ user_data/catalog/list.php?catalog_type_id=1&catalog_id=$1 [QSA,L]
RewriteRule ^catalog/theme-([0-9]+)/$ user_data/catalog/list.php?catalog_type_id=2&catalog_id=$1 [QSA,L]
RewriteRule ^catalog/(.*)$ user_data/catalog/$1
#有効性テスト用
# Redirect /api/ http://www.google.com
#Pマーク指摘対応
RewriteCond %{REQUEST_URI} .*/contact/.*$ [OR]
RewriteCond %{REQUEST_URI} .*/entry/.*$ [OR]
RewriteCond %{REQUEST_URI} .*/gift/.*$ [OR]
RewriteCond %{REQUEST_URI} .*/mypage/.*$
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^(www\.)?giftdays.jp$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# 404ページ
ErrorDocument 404 /user_data/404.php
Upon investigating more, I found some http inside my templates folder inside ftp. But they are like in many 100+ I guess.Should i go inside one by one and change all to https or is there anything I could do to change all at once?
Thank you
When setting up Apache to send an http request to https, you can't use the rewrite engine. You have to use the Redirect directive.
Probably you already have two VirtualHost blocks setup for http and https. Any request arriving on the http VirtualHost should be redirected to the https URL.

How to redirect domain from http to https using .htacces and cakephp

My app is hosted on heroku, and I am using Cakephp API. I want it to be redirect to https://www. and enforcing the www. subdomain.
And, they have two domains point to the same app.
I have the follow code, in the /app/.htaccess
RewriteEngine on
RewriteBase /
#apply if no https
RewriteCond %{HTTPS} off
#Ignore when is local env or any staging env
RewriteCond %{HTTP_HOST} !^local(.*) [NC]
RewriteCond %{HTTP_HOST} !^(.*)heroku(.*) [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
#apply if https
RewriteCond %{HTTPS} on
#Ignore when is local env or any staging env or subdoamin is www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^local(.*) [NC]
RewriteCond %{HTTP_HOST} !^(.*)heroku(.*) [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
#default from cakephp
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
I have the follow code, in the /app/webroot/.htaccess
RewriteEngine On
RewriteBase /
#redirect any request from .poa.br to .com.br
RewriteCond %{HTTP_HOST} ^(.*)example\.poa\.br$ [NC]
RewriteRule ^(.*)$ https://www.example.com.br/$1 [NE,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
But it doesn't work. Just the follows requests are redirect to https://www.example.com.br:
https://www.example.com.br
https://www.example.poa.br
http://www.example.poa.br
This option go to domain, but without https prefix.
http://www.example.com.br
The other options doesn't work (return DNS_PROBE_FINISHED_NXDOMAIN):
https://example.com.br
http://example.com.br
https://example.poa.br
http://example.poa.br
In cakephp3 I struggled with this. My solution was to change the first .htaccess file from
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
TO
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
Notice the 302, the reason I struggled is that the .htaccess got cached and made testing very difficult, I even went as far as delete all the cakephp code in the index.php in the webroot and just echo out 'test' Once I got it working I changed the 302 to 301 so that the .htaccess can hard cache in the browser. ( To test if values like HTTP:X-Forwarded-Proto exist I var_dump($_SERVER) in the index.php file )
IF you are on a server without HTTP:X-Forwarded-Proto
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
Hello make sure rewrite module is on before this works.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourwebsite.com/$1 [R,L]
and in your base url you need to add (s) to http.
I hope this helps you.
You can also add 'www'
just add this
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
We cannot redirect domain to www.domain in Heroku using .htaccess. We need configure it using Heroku command. See the articles:
https://github.com/Helabs/pah/wiki/Configuring-domain-on-Heroku
https://devcenter.heroku.com/articles/custom-domains#add-a-custom-root-domain
I'm looking how to force https.
Change in your cakephp project folder your htaccess file like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^(.*)http(\:|\%3A)(.*)$
ReWriteRule .* - [F]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
And be sur that your server (or your virtual host) listen to the port 443 (for https)
<VirtualHost *:80>
ServerAdmin subdomain#example.br
ServerName subdomain.example.br
ServerAlias subdomain.example.br
DocumentRoot /var/www/subdomain
<Directory /var/www/subdomain/>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
ServerSignature Off
ErrorLog /var/log/apache2/error.log
</VirtualHost>
<VirtualHost *:443>
ServerAdmin subdomain#example.br
ServerName subdomain.example.br
ServerAlias subdomain.example.br
DocumentRoot /var/www/subdomain
<Directory /var/www/subdomain/>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
SSLEngine on
SSLProtocol all -SSLv2
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM
SSLCertificateFile /[path to your CRT file]
SSLCertificateKeyFile /[path to your PEM file]
SSLCertificateChainFile [path to your CRT file]
SSLCACertificatePath /etc/ssl/certs/
SSLCACertificateFile /[path to your CRT file]
ErrorLog /var/log/apache2/error.log
</VirtualHost>
I made all configurations in /app/webroot/.htaccess
There is a trick to identify if request to Heroku is https.
Follow the code:
RewriteEngine On
RewriteBase /
#Identify if it is https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
#ignore local env
RewriteCond %{HTTP_HOST} !^local(.*) [NC]
#apply redirect in all subdomain
RewriteCond %{HTTP_HOST} ^(.*)example\.com\.br$ [NC]
RewriteRule ^(.*)$ https://example.com.br/$1 [NE,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
We decided to redirect all request to naked domain, but if you want to change to www you coult implement the follow command.
RewriteRule ^(.*)$ https://www.example.com.br/$1 [NE,R=301,L]
Thanks

htaccess - modify current file to 301-redirect all traffic to https

this is my current .htaccess file:
Options -Indexes FollowSymLinks Includes ExecCGI MultiViews
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (.*)ApacheBench(.*) [NC]
RewriteRule ^(.*) - [F]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* h++p://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^seconddomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.seconddomain.com [NC]
RewriteRule ^(.*)$ h++p://firstdomain.com/path/to/somewhere [L,R=301,NC]
php_value memory_limit 512M
php_value max_input_vars 10000
php_value upload_max_filesize 5000M
What I want to do is, that all traffic coming to firstdomain.com to be redirected to https://firstdomain.com.
So:
h++p://firstdomain.com
h++p://www.firstdomain.com
h++ps://www.firstdomain.com
all should be redirected (301 redirect) to
h++ps://firstdomain.com
How I need to modify it? I tried it myself already before, but then I always receive a redirect error that seems to be unable to end and therefor the browser stops resolving the domain then.
Options +FollowSymlinks
RewriteEngine on
ReWriteCond %{HTTP_HOST} ^(www.)?firstdomain.com$ [NC]
ReWriteRule ^(.*)$ https://seconddomain.com/$1 [R=301,L]
you can try it.

How to redirect http to https for www and non www for a directory using .htaccess

I'm trying to get all these conditions to redirect to https://www.domain.com/admin/ but only for a certain directory
RewriteCond
http://domain.com/admin/
http://www.domain.com/admin/
https://domain.com/admin/
RewriteRule
https://www.domain.com/admin/
I tried this but agave me a redirect loop:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/admin [R=301,L]
Thank you
UPDATE
I have a feeling that the vhost on the server has something to do with it. Here is what I have in /etc/apache2/sites-enabled/000-default
<VirtualHost *:80>
ServerAdmin webmaster#domain.com
ServerName default.domain.com
# Set application environment.
SetEnvIf Host "^" APPLICATION_ENV=production
SetEnvIfNoCase Host "^(stage\.)(.*)$" APPLICATION_ENV=staging BOGUS_HTTP_HOST=$2
RewriteEngine On
#RewriteLogLevel 3
# Remove parts from the front if host name has too many parts,
# unless a directory with the right name exists in /var/www.
RewriteCond %{HTTP_HOST} "^([^.]+)\.(.*[^.]+\.[^.]+)$"
RewriteCond /var/www/%{HTTP_HOST} !-d
RewriteRule "^(.*)$" "http://%2$1" [R,L]
# Redirect admin and member requests to SSL connection
RewriteCond %{REQUEST_URI} ^/(admin|member|join|order)(/.*)?$ [NC]
RewriteRule ^.*$ https://%{HTTP_HOST}/%1%2 [R,L]
# Set the bogus ssl header
SetEnv BOGUS_HTTPS "off"
UseCanonicalName Off
VirtualDocumentRoot /var/www/%0
</VirtualHost>
Have this rule in /admin/.htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L,NE]

.htaccess redirect index.php?q=* to index.php?q=* on another domain

Why each page redirects to domain? I move dkcherta.ru -> dkchertanovo.ru and want for example http://dkcherta.ru/index.php?id=80 -> http://dkchertanovo.ru/index.php?id=80
My .htaccess:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?dkcherta\.ru$ [NC]
RewriteRule ^ http://dkchertanovo.ru/%{REQUEST_URI} [L,R=301]
RewriteRule ^rss_afisha[/]?$ /index.php?id=230 [L]
RewriteRule ^rss_news[/]?$ /index.php?id=231 [L]
ErrorDocument 404 http://dkchertanovo.ru/
# For full documentation and other suggested options, please see
# http://svn.modxcms.com/docs/display/MODx096/Friendly+URL+Solutions
# including for unexpected logouts in multi-server/cloud environments
RewriteBase /
# Сливка доменного имени
#RewriteCond %{HTTP_HOST} ^dkchertanovo\.ru$ [NC]
#RewriteRule ^(.*)$ http://www.dkchertanovo.ru/$1 [R=301,L]
RemoveHandler .php .htm .html
AddType application/x-httpd-php .php .htm .html
AddHandler application/x-httpd-php .php .htm .html
php_flag register_globals off
# Fix Apache internal dummy connections from breaking [(site_url)] cache
RewriteCond %{HTTP_USER_AGENT} ^.*internal\ dummy\ connection.*$ [NC]
RewriteRule .* - [F,L]
# Exclude /assets and /manager directories from rewrite rules
RewriteRule ^(manager|assets) - [L]
# For Friendly URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
### force www
#RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
#RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#RewriteBase /
# Reduce server overhead by enabling output compression if supported.
#php_flag zlib.output_compression On
#php_value zlib.output_compression_level 5
You don't need multiple lines of these:
Options +FollowSymLinks -MultiViews
RewriteEngine On
Just put only once at the top and it should be effective for rest of the file.
Then to solve your problem move this rule on top of all other rules in your .htaccess file:
RewriteCond %{HTTP_HOST} ^(www\.)?dkcherta\.ru$ [NC]
RewriteRule ^ http://dkchertanovo.ru%{REQUEST_URI} [L,R=301]

Resources