htaccess for www-only redirection - .htaccess

Is there any way I can redirect my urls to only use http://www.domain.com instead of http://domain.com?
Here's my htaccess but doesnt work:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
# RewriteBase /test/
Options +FollowSymlinks
# Protect application and system files from being viewed
RewriteRule ^(application|modules|system) - [F,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT,L]
#redirec
#detect urls without www
RewriteCond %{http_host} ^thehotelinventory.com [NC]
#redirect to www, does not work though
RewriteRule ^(.*)$ http://www.thehotelinventory.com/$1 [R=301,L]
AddHandler php5-script .php

If you need to redirect from non-www to www, then redirect to HTTPS for certificates like THAWTE that dont support www non-www on SSL 123.
RewriteEngine On
#non-www to www
RewriteCond %{HTTP_HOST} ^yourdomain.com
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]
#Redirect to HTTPS before www redirect
RewriteEngine on
RewriteCond %{HTTPS}
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Thanks to Brasil na Web Dev. Guys.

a simple answer can be found here: http://snippets.dzone.com/posts/show/2264

Related

htaccess force https non-www in single redirect for all pages

I would like to redirect everybody to my https:// site (not https://www) and force HTTPS in single redirect. My relevant parts of .htaccess:
# Check if HTTPS and WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
# Force HTTPS and remove WWW
RewriteRule ^(.*)/$ https://example.com/$1 [R=301,L]
The problem? It doesn't work for sub-pages!
While it does work for main page (eg. http://www.example.com redirects to https://example.com), it doesn't work for sub-pages (eg. http://www.example.com/contact sholud redirect me to https://example.com/contact but only www is removed and I end up on non-https site: http://example.com/contact).
How to change my .htaccess rules, to force HTTPS and remove WWW for my site and all pages of the site in single redirect?
After changes: it works for sub-pages, but in 2 redirects. Can it be done in only one?
Thanks to answer by Nisarg and some digging, my site now properly redirects to HTTPS even on sub-pages. Yay!
Relevant .htaccess code now looks like this:
RewriteEngine On
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^(.*)$ "https://example.com/$1" [R=301,L]
# remaining htaccess mod_rewrite CODE for WordPress
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Unfortunately, it takes 2 steps to process. So, the question is still up: can it be done in 1 redirect?
Try the following:(Guessing you are using index.php)
RewriteEngine On
# Redirect to www
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
#
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule ^ index.php [L]
Make sure you clear your browser cache before testing this change.

htaccess file not redirecting a certain url

I added an SSL to my site and have the following htaccess file in place to redirect:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^(.*)$ "https://example.com/$1" [R=301,L]
</IfModule>
This covers almost everything, so http://www.example.com, http://example.com, and example.com all forward to https://example.com. However, there is one scenario where it does not. If I enter https://www.example.com, it does not redirect to https://example.com and I get a security certificate error. Looking for a solution. Thank you in advance.
cdr6545
Try this
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymlinks
# To redirect from www to non www (Rewrite www.example.com → example.com)
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http%{ENV:protossl}://%1/$1 [R=301,NE,L]
# Redirect HTTP to HTTPS automatically (only if not in localhost)
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

Redirect all subdomains except www to main domain

I want to redirect all subdomains except www to https://www.example.com in .htaccess, how can I do this? All subdomains can be accessed.
I only want to redirect the subdomains, not inner pages on the main domain.
Examples: (Should redirect)
hello.domain.com
test.example.domain.com
www.hello.sub.domain.com
Examples of where not to redirect:
www.domain.com/hello-world
www.domain.com
Current .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.dk$
RewriteRule ^(.*) https://www.domain.dk/$1 [QSA,L,R=301]
# 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
This should get you started. Anything apart from www.mydomain.com will be redirected to www.example.com.
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteRule ^(.*) https://www.example.com/$1 [QSA,L,R=301]
Edit:
Remember that it's the .htaccess in your subdomain folder, or _wildcard_domain.com that you will have to create or edit for this to work.
Try this in your .htaccess:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

I want to redirect all http requests to https, and all www requests to non-www

Here is my current .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
</IfModule>
I want to redirect all http requests to https, and all www requests to non-www, and all file requests to index.php
For example:
http://www.example.com to https://example.com
https://www.example.com to https://example.com
http://example.com/file.php to https://example.com/index.php
Everything seems to be working except the www part.. Any help please?
http://www.example.com to https://example.com
https://www.example.com to https://example.com
http://example.com/file.php to https://example.com/index.php
Maybe this will work:
RewriteEngine On
# Remove www from https requests.
# Next 3 lines must be placed in one .htaccess file at SSL root folder,
# if different from non-ssl.
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,L]
# Redirect all http to https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} (?:www\.)?(.+) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,L]
If it doesn't work, try replacing
RewriteCond %{HTTPS} off or on with
RewriteCond %{HTTP:X-Forwarded-SSL} off or on
You can add an additional rule dealing with the www part
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [L,R]
The RewriteCond captures everything after the www. and uses that in the RewriteRule as %1.
When everything works as you expect, you can change R to R=301.
Never test with 301 enabled, see this answer
Tips for debugging .htaccess rewrite rules
for details.

htaccess add index.htm at end of folder

I need a way to add index.htm at end of url.
For example:
www.example.com/folder1/folder2 AND
www.example.com/folder1/folder2/
must be redirected internally to www.example.com/folder1/folder2/index.htm (this must apply for any number of folders/subfolders)
My htaccess (at root) so far looks like:
Options +FollowSymLinks
RewriteEngine on
RewriteOptions inherit
RewriteBase /
#
# some 301 REDIRECTS here
#
# rewrite non-www into www
# after all redirection rule AND before any rewrite rule
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#
# rewrite rules below here
#
Any way to fix this?
Thank you.
EDIT:
- All urls come from rewrite rules - there are no actual folders in site.
- Typing www.example.com/folder1/folder2 in my browser, only get a 404 page not found. I need it to redirect to www.example.com/folder1/folder2/index.htm (there is a rule creating this url)
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.htm$ [NC]
RewriteRule ^(.*?)/?$ /$1/index.htm [L,R=301]
Do you mean you want to REDIRECT /folder1 and /folder1/ into /folder1/index.htm, and /folder1/folder2 and /folder1/folder2/ to /folder1/folder2/index.htm, and /folder1/folder2/folder3 and /folder1/folder2/folder3/ to /folder1/folder2/folder3/index.htm
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)/?$
RewriteRule ^(.*) /%1/index.htm [R]
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)/([a-z0-9-_]+)/?$
RewriteRule ^(.*) /%1/%2/index.htm [R]
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)/([a-z0-9-_]+)/([a-z0-9-_]+)/?$
RewriteRule ^(.*) /%1/%2/%3/index.htm [R]

Resources