Rewrite rule to remove .php extension using .htaccess - .htaccess

I have found many solutions on StackOverflow to handle this common problem but none seem to work for my setup. When I link to a php page I still see the .php extension and would like to have
index.php updated to 'website'
about-us.php to be website/about
I am working on a site using wamp / localhost inside a folder 'website'. I have my .htacces file located inside the root of the 'website' folder.
The rewrite condition / rule I'm trying to use to remove the .php extension from index.php and other php pages is this.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
My links have URL's like this
<a href="index.php">
<a href="about-us.php">
Here's the full .htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType application/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##
<IfModule mod_deflate.c>
# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>
# Or, compress certain file types by extension:
<files *.html>
SetOutputFilter DEFLATE
</files>

It's not actually "removing" .php from the URL, it works as redirecting.
In other words, if you don't want others see that .php in your URL, then you'll have to fix all your link.
i.e <a href='www.example.com'>
<a href='www.example.com/about-us'>
<a href='www.example.com/blog'>
You'll have to fix all your links inside your script.

Related

Images on CSS are not being served as HTTPS

I have a css file where I call few images as background. Although my website has a valid certificate and the images are in the same address, it doesn't work.
I've tried using my urls as //talesebner.com/images/logo.svg, https://talesebner.com/images/logo.svg, and even https://www.talesebner.com/images/logo.svg. But Chrome, and Lighthouse too, keep saying that they are not being loaded via HTTPS.
As a side effect (I believe) the images are not even being loaded on Safari on iPhone.
What can I do?
Edit: As asked, here is my .htaccess code:
ErrorDocument 500 /errors/500.html
ErrorDocument 404 /errors/404.html
ErrorDocument 403 "Sorry, I can't allow your access today."
<IfModule mod_mime.c>
AddDefaultCharset utf-8
DefaultLanguage en-GB
</IfModule>
<IfModule mod_autoindex.c>
Options -Indexes
</IfModule>
#Compress text files
<ifModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/bmp
AddOutputFilterByType DEFLATE image/vnd.microsoft.icon
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/rdf+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom+xml
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/ecmascript
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/ecmascript
AddOutputFilterByType DEFLATE application/json
AddOutputFilterByType DEFLATE application/ld+json
AddOutputFilterByType DEFLATE application/manifest+json
AddOutputFilterByType DEFLATE application/schema+json
AddOutputFilterByType DEFLATE application/geo+json
AddOutputFilterByType DEFLATE application/x-web-app-manifest+json
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE font/truetype
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/eot
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE application/x-pointplus
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/wasm
AddOutputFilterByType DEFLATE text/cache-manifest
AddOutputFilterByType DEFLATE text/calendar
AddOutputFilterByType DEFLATE text/markdown
AddOutputFilterByType DEFLATE text/vcard
AddOutputFilterByType DEFLATE text/vnd.rim.location.xloc
AddOutputFilterByType DEFLATE text/vtt
AddOutputFilterByType DEFLATE text/x-component
AddOutputFilterByType DEFLATE text/x-cross-domain-policy
</IfModule>
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
#Redirects
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteOptions inherit
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ %{ENV:PROTO}://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^talesebner\.com$
RewriteRule ^mail\/?$ "https\:\/\/sxb1plzcpnl453513\.prod\.sxb1\.secureserver\.net\:2096\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^talesebner\.com$
RewriteRule ^cpanel\/?$ "https\:\/\/sxb1plzcpnl453513\.prod\.sxb1\.secureserver\.net\:2083\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^talesebner\.com$
RewriteRule ^cv\/?$ "https\:\/\/talesebner\.com\/tales\-ebner\-cv\.pdf" [R=301,L]
RewriteCond %{HTTP_HOST} ^talesebner\.com$
RewriteRule ^portfolio\/?$ "https\:\/\/www\.figma\.com\/proto\/uAmDiShc6IahLeyhizKPQx\/\[Tales\-Ebner\]\?node\-id\=282\:15\&viewport\=\-1412\,635\,0\.3143424093723297\&scaling\=contain\&hotspot\-hints\=0\&hide\-ui\=1" [R=301,L]
RewriteCond %{HTTP_HOST} ^talesebner\.com$
RewriteRule ^vcard\/?$ "https\:\/\/talesebner\.com\/tales\-ebner\.vcf" [R=301,L]
RewriteCond %{HTTP_HOST} ^talesebner\.com$
RewriteRule ^regex\/?$ "https\:\/\/talesebner\.com\/regex\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^talesebner\.com$
RewriteRule ^old\/2013\/?$ "https\:\/\/web\.archive\.org\/web\/20140714190641\/http\:\/\/tales\.ebner\.com\.br\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^talesebner\.com$
RewriteRule ^old\/2014\/?$ "https\:\/\/web\.archive\.org\/web\/20141218124503\/http\:\/\/ebner\.com\.br\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^talesebner\.com$
RewriteRule ^old\/2016\/?$ "https\:\/\/web\.archive\.org\/web\/20161002001905\/http\:\/\/ebner\.com\.br\/" [R=301,L]
</IfModule>
#Expire headers
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|webp|js|css|swf)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>
<FilesMatch "\.(ttf|otf|woff|woff2)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>
ExpiresActive On
ExpiresDefault "access plus 31536000 seconds"
ExpiresByType image/x-icon "access plus 31536000 seconds"
ExpiresByType image/jpeg "access plus 31536000 seconds"
ExpiresByType image/jpg "access plus 31536000 seconds"
ExpiresByType image/png "access plus 31536000 seconds"
ExpiresByType image/gif "access plus 31536000 seconds"
ExpiresByType image/webp "access plus 31536000 seconds"
ExpiresByType text/css "access plus 31536000 seconds"
ExpiresByType text/javascript "access plus 31536000 seconds"
ExpiresByType application/javascript "access plus 31536000 seconds"
ExpiresByType application/x-javascript "access plus 31536000 seconds"
ExpiresByType text/html "access plus 31536000 seconds"
ExpiresByType application/xhtml+xml "access plus 31536000 seconds"
ExpiresByType font/ttf "access plus 31536000 seconds"
ExpiresByType font/otf "access plus 31536000 seconds"
ExpiresByType font/woff "access plus 31536000 seconds"
ExpiresByType font/woff2 "access plus 31536000 seconds"
#Cache-Control Headers
<IfModule mod_headers.c>
Header set X-Frame-Options SAMEORIGIN
Header set X-Content-Type-Options nosniff
Header set X-XSS-Protection "1"
Header set Cache-Control "private, max-age=31536000, must-revalidate"
Header set Content-Security-Policy "default-src 'self'; style-src 'self' fonts.googleapis.com; font-src 'self' fonts.gstatic.com; script-src 'self' 'unsafe-inline'; object-src 'none'"
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" env=HTTPS
</IfModule>
#Turn ETags Off
Header unset Pragma
FileETag None
Header unset ETag
Your images appear to be loading just fine for me over https://example.com/ (no www) - so maybe you have resolved this already? However, there is a problem with your www to non-www redirect that results in a redirect-loop, so if you are using www.example.com to reference any of your images then they will fail to load. (Incidentally, since this is a 301 (permanent) redirect then it's possible you are seeing an erroneous cached response.)
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ %{ENV:PROTO}://%1%{REQUEST_URI} [R=301,L]
The PROTO environment variable does not appear to be set in the code you have posted and this is resulting in a relative (malformed) redirect to https://www.example.com/<filepath-to-document-root>://example.com/foo which will naturally cause a redirect loop.
You should hard-code the https protocol, as in all the other directives:
:
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
It is possible that this PROTO env var is intended to be set in the .htaccess file (or parent config) you are inheriting from (the purpose of the RewriteOptions inherit directive) - if any - but that does not appear to be the case.
Also, unless you plan to implement HSTS then you should reverse the HTTP to HTTPS and www to non-www rules in order to avoid an unnecessary double redirect when requesting http://www.example.com/ (ie. HTTP and www). For example:
# 1. Redirect www to non-www (and HTTPS)
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
# 2. Redirect HTTP to HTTPS (already non-www)
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

.htaccess non www to no www SSL and www to www SSL

I have searched every where and tried various .htaccess configurations, but haven't found an answer.
With access to only the .htaccess file I want to:
Direct NON www to NON www SSL
Example: http://example.com -> https://example.com
AND
Direct www to www SSL
Example: http://www.example.com -> https://www.example.com
Most examples show forcing all requests to SSL as only NON www or www. I want both.
htaccess
<IfModule mod_headers.c>
Header append Vary: User-Agent
Header append Vary: Accept-Encoding
</IfModule>
AddDefaultCharset utf-8
AddType image/svg+xml svg
RewriteEngine On
RewriteRule ^sitemap\.xml$ /sitemap.php [L]
# Exception - Reached the document root then stop
# (Prevents rewrite loop if index.php is missing - 404 instead)
RewriteRule ^index\.php$ - [L]
# Exception - Any request for a valid file stop here
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# If already requesting "index.php" then step up a directory
RewriteRule ^(.*/)?[^/]+/index\.php$ /$1index.php [L]
# Otherwise try "index.php" in the current path segment
RewriteRule ^(.*/)?[^/]*$ /$1index.php [L]
<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilter DEFLATE .shtml
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
AddType image/x-icon .ico
ExpiresByType image/x-icon "access plus 1 year"
ExpiresByType image/ico "access plus 1 year"
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/svg "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
You can use:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

Htaccess with Angular2 to force SSL and fix routing

So I have two issues to resolve with my NG2 app:
When routes are refreshed, I get a page not found error
When a non-SSL route is accessed I want to re-direct to SSL.
This fixes the broken routing:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
This forces SSL. However when included with the above code, it no longer works.
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
How do I get these two conditions to work together?
Here is my final solution.
This includes cashing, gzipping etc to make your app super fast!
#REDIRECT ROUTES TO INDEX (fixes broken routes with angular)
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
#ENABLE GZIP COMPRESSION TO IMPROVE PERFORMANCE
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
# SET EXPIRE HEADERS TO IMPROVE PERFORMANCE
<ifModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 2 days"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType application/javascript "access plus 2 week"
ExpiresByType application/x-javascript "access plus 2 week"
ExpiresByType text/javascript "access plus 2 week"
ExpiresByType text/html "access plus 600 seconds"
ExpiresByType application/xhtml+xml "access plus 600 seconds"
</ifModule>
# END Expire headers
# BEGIN Cache-Control Headers
<ifModule mod_headers.c>
<filesMatch "\.(ico|jpe?g|png|gif|swf)$">
Header set Cache-Control "public"
</filesMatch>
<filesMatch "\.(css)$">
Header set Cache-Control "public"
</filesMatch>
<filesMatch "\.(js)$">
Header set Cache-Control "public"
</filesMatch>
<filesMatch "\.(x?html?|php)$">
Header set Cache-Control "private, must-revalidate"
</filesMatch>
</ifModule>
# END Cache-Control Headers
I had this same problem and I did a work-around so that when the page loads on http it simply replaces http with https. I know its not very elegant and it loads the page twice, but I thought of sharing it. Maybe someone can help with a pure htaccess solution.
This is my app.component.ts
import { Location } from '#angular/common';
import { Component, OnInit } from '#angular/core';
#Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent implements OnInit {
location: Location;
ngOnInit() {
if (location.protocol === 'http:') {
window.location.href = location.href.replace('http', 'https');
}
}
}

.htaccess redirect all request (both ssl and non ssl) to https://domain.com

I'm using Let's Encrypt SSL on my domain, unfortunately it only securing my domain.com and not www.domain.com.
So, I want
http://domain.com
http://www.domain.com
https://www.domain.com
to redirect to
https://domain.com
Right now I managed to get it to work except https://www which shows SSL error.
Can someone help?
My htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
<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>
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"
</IfModule>
## EXPIRES CACHING ##
When you load a site protected with a certificate (ie. something beginning with https:), the client
does a DNS resolution and connects to the IP address
through an SSL handshake it gets the certificate from the server
do a HTTP request over the now secured channel, sending the hostname you set in the browser’s address bar
Now between step 2 and 3, the browser will realize the SSL certificate presented by the server holds a different hostname (domain.com) than what you requested (www.domain.com), and will issue a warning.
The only solution here is to get a wildcard certificate for *.domain.com, which will be valid for both domain.com and www.domain.com (but, as far as I remember, not for my.sub.domain.com).

Force HTTP for non WWW and WWW

I'm trying to force users to HTTP, we don't want people to access our site with HTTPS. The code below redirects to http just fine with someone visits the site with https://www but not with https://website
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
How would I make it do the same thing when people enter the url without the www? The site is being forced with WWW though wordpress.
Thanks!
RewriteEngine on
RewriteCond %{HTTPS} ^on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# GZip Ninja Speed -- Starts here
# Do not write anything between "GZip Ninja Speed -- Starts" and "GZip Ninja Speed -- Ends"
# It will be deleted while uninstalling GZip Ninja Speed plugin
AddOutputFilterByType DEFLATE text/plain #GZip Ninja Speed
AddOutputFilterByType DEFLATE text/html #GZip Ninja Speed
AddOutputFilterByType DEFLATE text/xml #GZip Ninja Speed
AddOutputFilterByType DEFLATE text/css #GZip Ninja Speed
AddOutputFilterByType DEFLATE application/xml #GZip Ninja Speed
AddOutputFilterByType DEFLATE application/xhtml+xml #GZip Ninja Speed
AddOutputFilterByType DEFLATE application/rss+xml #GZip Ninja Speed
AddOutputFilterByType DEFLATE application/javascript #GZip Ninja Speed
AddOutputFilterByType DEFLATE application/x-javascript #GZip Ninja Speed
AddType x-font/otf .otf #GZip Ninja Speed
AddType x-font/ttf .ttf #GZip Ninja Speed
AddType x-font/eot .eot #GZip Ninja Speed
AddType x-font/woff .woff #GZip Ninja Speed
AddType image/x-icon .ico #GZip Ninja Speed
AddType image/png .png #GZip Ninja Speed
# GZip Ninja Speed -- Ends here
You can use this in your .htacess
RewriteCond %{HTTPS} ^on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Resources