.htaccess force "www." on everything but subdomains and remove trailing slashes - .htaccess

Here's what I have so far:
#Force www.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)$
RewriteRule ^(.+)/$ http://www.domain.com/$1 [R=301,L]
However, this messes up all subdomains doing the following redirect:
sub.domain.com -> www.sub.domain.com
And also, its dependant on the domain written on the remove trailing slash bit.
So... two questions.
How do I rewrite the rule on the "remove trailing slash" bit to exclude writing the domain on it?
How do I make a rewritecond to exclude subdomains, without explicitly writing them down, on the "force www." bit?
Examples of desired results -
sub.domain.com/something/ -> sub.domain.com/something
domain.com/something/ -> www.domain.com/something
www.domain.com/ -> www.domain.com
sub.domain.com -> sub.domain.com
Thanks!

Change the www to check for the actual domain:
#Force www.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Or, if you're hosting a bunch of domains, you can check for a name before the TLD:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]{2,4})$ [NC]
RewriteRule ^ http://www.%1.%2%{REQUEST_URI} [L,R=301]
As for the trailing slash, you have to be careful that the request isn't made for a directory. Because if it is, and you have DirectorySlash turned on (by default it is on), then you'll cause a redirect loop.
To exclude subdomains, we assume that the first rule redirected the browser to ensure that it started with "www", and since subdomains aren't being redirected to start with "www", we can just check for that:
#Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.+)/$ /$1 [R=301,L]

Related

How to combine 2 redirections in one, https + removing trailing slashes of url?

My htaccess actually looks like :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com(.)*$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^(.*)/$ /$1 [R=301,L]
RewriteRule ^([0-9a-zA-Z/\ -]+)(?:&([0-9a-zA-Z&=_\ -]+))?$ index.php?action=$1&$2 [L]
# $1 : route name and framework parameters
# $2 : classic $_GET parameters (&param=value)
It redirects to https, then redirect to url without trailing slashes, then rewrite a clean url without index etc.
I would like to know, if I enter http://www.example.com/somepage/, how to redirect to https://example.com/somepage in a single redirection instead of multiple?
I would like to know, if I enter http://example.com/somepage/, how to redirect to https://example.com/somepage in a single redirection instead of two?
You can use this single redirect rule for that:
DirectoryIndex index.php
RewriteEngine On
# add https, remove www and remove trailing slash in same rule
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{REQUEST_URI} /$
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*?)/?$ https://%1/$1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .+ index.php?action=$0 [L,QSA]

Struggling with .htaccess (Godaddy Linux Hosting)

I would really appreciate your help with the below. I have been both googling and testing code via trial an error, I believe that I'm slowly figuring things out but I would appreciate the fast track assistance in both the result and my learning development.
I currently have a website hosted on a shared resource on a apache Linux server through GoDaddy. I am trying to achieve the following;-
1) Redirect non www traffic to www.
2) Remove the .html file extension and add a trailing slash on the web address.
I currently have the code below, the 301 www. redirect is working and the trailing slash is also working. However I am getting 404 errors on the subpages ie www.swiftcomm.co.uk/contact/;-
How do I go about resolving this issue with the code?
Options +FollowSymlinks -MultiViews -Indexes
RewriteEngine on
# Redirect to domain with www.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Same for HTTPS:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Ensure all directory URLs have a trailing slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
# Same for HTTPS:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
Please try the following code - it can replace your entire file. The comments explain what each ruleset does.
Options +FollowSymlinks -MultiViews -Indexes
RewriteEngine on
RewriteBase /
# Force www. prefix - http/https
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
# Remove .html extension suffix from URI only if it matches an existing file.
# Add trailing slash simultaneously.
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} \s\/(.+)\.html\s [NC]
RewriteRule ^ %1/ [R=302,L]
# Force trailing slash if it has'nt been forced already.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^\/(.+[^/])$
RewriteRule ^ %{REQUEST_URI}/ [R=302,L]
# Rewrite non-existing files to their .html counterparts.
# If the .html file does not exist, Apache will gracefully degrade to a 404.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1.html [L]

htaccess rewrite nonexistent wildcard subdomains with and without https

I want to rewrite http://, http://www. https://www. and nonexistent subdomain variations to https://domain.com. A nonexistent subdomain variation might be http://abc.domain.com and https://abc.domain.com. To complicate matters further, I want to keep trailing URLs, and prevent redirecting existing subdomains. AND, some existing subdomains use http while others use https.
This is what I have so far:
#don't redirect existing subdomains example1 or example2
RewriteCond %{HTTP_HOST} ^((example1|example2)\.)?domain\.com$
RewriteRule .* - [L]
##redirect http, wildcard subdomains, and www. with appended trailing URLs
RewriteCond %{HTTP_HOST} . [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://domain.com/$1 [R=301,L]
Thanks in advance.
Here is how you can redirect nonexistent subdomains to https://domain.com:
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^(valid1|valid2|valid3)\.domain\.com$
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,L,NE]

htaccess remove trailing slash causes redirect loop

I am amending my htaccess file to achieve non-www to www (this is working) plus removing the trailing slash at the end of the URL, e.g.:
www.domain.bc.ca/club/ ---> www.domain.bc.ca/club
www.domain.bc.ca/club/index.html/ ---> www.domain.bc.ca/club/index.html
The portion of the htaccess file is below - the Force www bit is working; the Remove trailing slash bit is not. Help! Many thanks, Amanda.
# Force www.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^domain\.bc\.ca$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.+)/$ /$1 [R=301,L]
I'm going to go out on a limb and guess that you're trying to access a directory when this happens. In your example, the "club" seems to be a directory and when you redirect /club/ to /club, a module called mod_dir will redirect it back to having the trailing slash again. There's a really good reason for this, because if the trailing slash is missing for a directory, the directory's contents will be displayed instead of the index file. That means if you were able to go to www.domain.com/club (without the trailing slash), you'd see all the contents of the club directory instead of the club/index.html file.
If that's ok with you, then you can turn off mod_dir by adding this to your htaccess file:
DirectorySlash Off
But then you'd need to internally add the slash back in:
DirectorySlash Off
# Force www.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^domain\.bc\.ca$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.+)/$ /$1 [R=301,L]
# Add the slash back
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])$ /$1/ [L]

Apache rewrite all URL's to https with www + a few exceptions

I've tried all the answers to similar stack questions and nothing has worked. I need to redirect all to https://www except for example.com/blogs/* and example.com/page-name.
I currently have this:
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{http_host} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
which redirects everything except for https://example.com, it will NOT add the www.
You can see for yourself at https://moblized.com
RewriteEngine On
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://www.moblized.com/$1 [R=301,L]
RewriteCond %{http_host} ^moblized.com [NC]
RewriteRule ^(.*)$ https://www.moblized.com/$1 [R=301,L]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} blogs
RewriteRule ^(.*)$ http://moblized.com/blogs/$1 [R,L]
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
# $Id: .htaccess,v 1.90.2.4 2009/12/07 12:00:40 goba Exp $
AddHandler php5-script .php
Thank you!
I hope I understood you correctly. You want:
redirect from example.com to www.example.com (except /blogs/ and /page-name)
redirect all pages to HTTPS (except /blogs/ and /page-name)
based on your current .htaccess under /page-name you mean /favicon.ico
Here are the rules for the above requirements -- put them into your .htaccess:
# activate rewrite engine
RewriteEngine On
# don't touch favicon.ico (always accept as is regardless of the domain or protocol)
RewriteRule ^favicon.ico$ - [L]
# don't touch /index.php (usually means already overwritten URL)
# otherwise we may enter into a loop
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^index\.php$ - [L]
# ensure trailing slash is present for /blogs -> /blogs/
RewriteRule ^blogs$ http://mobilized.com/blogs/ [R=301,QSA,L]
# /blogs/ should only be accessible via http://example.com/blogs/
RewriteCond %{HTTP_HOST} !^moblized\.com$ [NC]
RewriteRule ^blogs/(.*)$ http://mobilized.com/blogs/$1 [R=301,QSA,L]
RewriteCond %{HTTP_HOST} ^moblized\.com$ [NC]
RewriteCond %{HTTPS} =on
RewriteRule ^blogs/(.*)$ http://mobilized.com/blogs/$1 [R=301,QSA,L]
RewriteRule ^blogs/.* - [L]
# redirect to www.example.com if necessary
RewriteCond %{HTTP_HOST} ^moblized\.com$ [NC]
RewriteCond %{REQUEST_URI} !=/client-ipad-contest
RewriteRule ^(.*)$ https://www.moblized.com/$1 [R=301,QSA,L]
# redirect to HTTPS if not there already
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !=/client-ipad-contest
RewriteRule ^(.*)$ https://www.moblized.com/$1 [R=301,QSA,L]
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA]
BTW, browser most likely will show "Untrusted Certificate" warning if your customer go to https://example.com. This is because HTTPS session has to be fully established first before the request starts processing by Apache's rewrite module.
If that is problem -- then consider buying another SSL certificate (or from another vendor) which will cover both example.com and www.example.com (GoDaddy does this for sure) or get wildcard certificate which will cover all subdomains -- *.example.com (but this most likely will be much more expensive).
UPDATE: After simulating your requirements locally (sorry, I have no SSL with working Apache, so I have replaced it (in my testing) with different kind of rule/domain name) I have revised and updated the rules.
I've tested these rules locally (all pages are very simple, just include 1 image & css and a bit of text) -- everything looking good. Let me know if something does not work.

Resources