My website url looks like www.[site-name].com
I want to redirect to https://www.[site-name].com on .htaccess with following code.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301, L]
I copy the file to ftp at this path: httpdocs/.htaccess
But it does not redirect to [site-name].com when I try to load www.[site-name].com. How can I solve this?
Try this:
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,QSA]
If you want to redirect non-www url to www url , for both http and https use this ;
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
If you want to redirect www to non-www url use this ;
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
or without using domainname ;
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,L]
RewriteCond %{HTTPS_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,L]
Related
I have the following domains:
example1.com
example2.com
example3.com
The domains point to /public_html/. There are three things I want to do in /public_html/.htaccess:
Redirect (with all parameters and paths) the domains example2.com and example3.com to the domain example1.com.
example1.com itself should get shown (if not, then redirect) always with https and www, means: https://www.example1.com
The custom root path for the domains is /public_html/. I want to change this to /public_html/example_path/.
I have the following in /public_html/.htaccess:
RewriteCond %{HTTP_HOST} ^example2.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example2.com [NC]
RewriteRule ^(.*)$ https://www.example1.com/$1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^example3.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example3.com [NC]
RewriteRule ^(.*)$ https://www.example1.com/$1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^example1.com [NC]
RewriteRule ^(.*)$ https://www.example1.com/$1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^example1.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example1.com$
RewriteCond %{REQUEST_URI} !example_path/
RewriteRule (.*) /example_path/$1 [L]
This is nearly working as expected. But when opening http://www.example1.com there is no redirection to https://www.example1.com. This is only working when removing the last four code lines, that should change the root path.
For all other domains it's working.
Why isn't it woking for http://www.example1.com? And why is it only working when not changing the root path?
Add this rule to yours at the top of the .htaccess file
# http <> https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
Or check these rewrites instead of your rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.example1\.com [NC]
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !example_path/
RewriteRule (.*) /example_path/$1 [L]
</IfModule>
For specific domains (www/non-www example-other.co, www/non-www example3.com, www/non-www example2.com, example1.com)
<IfModule mod_rewrite.c>
RewriteEngine On
# http www/non-www example-other.com/any to https://www.example-other.com/any
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?example-other\.com [NC]
RewriteRule (.*) https://www.example-other.com/$1 [R=301,L]
# https non-www example-other.com/any to https://www.example-other.com/any
RewriteCond %{HTTP_HOST} ^example-other\.com [NC]
RewriteRule (.*) https://www.example-other.com/$1 [R=301,L]
# http www/non-www example1.com/any to https://www.example1.com/any
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?example1\.com [NC]
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
# http/https www/non-www example2.com/any to https://www.example1.com/any
RewriteCond %{HTTP_HOST} ^(www\.)?example2\.com [NC]
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
# http/https www/non-www example3.com/any to https://www.example1.com/any
RewriteCond %{HTTP_HOST} ^(www\.)?example3\.com [NC]
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
# https non-www example1.com/any to https://www.example1.com/any
RewriteCond %{HTTP_HOST} ^example1\.com [NC]
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
# Rewrite /any to /example_path/any
RewriteCond %{REQUEST_URI} !example_path/
RewriteRule (.*) /example_path/$1 [L]
</IfModule>
I am trying to enforce https://www. on all of my pages except hotels.php.
My current code below is adding www. twice, i.e. https://www.www.example.com
.htaccess
RewriteEngine On
RewriteCond %{HTTPS} =off
RewriteCond %{REQUEST_URI} !^\/hotels.php\/
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} \/hotels.php\/
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [L,R=301]
Let me know what I am missing here which causing issue.
Thanks
Try these rewrite conditions:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force https:// for all except your desired URLs
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/hotels.php/ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force http:// for your desired URLs
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /hotels.php/ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You have to check if www is in the request URI as you check HTTPS. Try this:
//If has not www and it's not a subdomain adds www
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
//If has not https and it's not hotels.php add https
RewriteCond %{REQUEST_SCHEME} !https
RewriteCond %{REQUEST_URI} !^\/hotels.php\/
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Not a master with Htaccess regex, bit this works for me:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]
</IfModule>
Hope it helps! There's lot of answers to this on StackOverflow... pretty sure that probably where I got the above!
this is my htaccess:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteRule ^ /index.html [L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^shadyab.com [NC]
RewriteRule ^(.*)$ http://www.shadyab.com/$1 [L,R=301,NC]
but my site loaded with non www.
Set condition to any address not starting with www and then redirect.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Force www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
Force non-www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
I have an issue with mod_rewrite in apache2, I had enabled the mod_rewrite but doesent load I get this message: ERR_TOO_MANY_REDIRECTS
The .htaccess:
RewriteEngine on
RewriteRule ^(\w+)$ index.php?page=$1 [L,NC,QSA]
RewriteRule ^(\w+)+\/$ index.php?page=$1 [L,NC,QSA]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
IDK what is wrong.
Make the .htacess easier and clean.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^/(.*) http1://www.%{HTTP_HOST}%/$1 [R=301]
RewriteCond "%{REQUEST_URI}" "!=/index.php" RewriteRule "^(.*)" "/index.php?page=$1" [L,PT]
It solution works without SSL, as main step.
i have this code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com:8080
RewriteRule ^ http://www.domain.com:8080%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com:8080$ [NC]
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+)\.domain\.com:8080$ [NC]
RewriteRule ^([A-Za-z0-9-]+)/([0-9-]+)/?(.*)?\.html$ view.php?prefix=%1&cat=$1&id=$2&title=$3 [L,QSA]
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com:8080$ [NC]
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+)\.domain\.com:8080$ [NC]
RewriteRule ^([A-Za-z0-9-]+)/?((index|news|photos|videos|articles)\.html)?$ categories.php?prefix=%1&cat=$1 [L,QSA]
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com:8080$ [NC]
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+)\.domain\.com:8080$ [NC]
RewriteRule ^(index\.html)/?$ category_index.php?prefix=%1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com:8080 [NC]
RewriteRule .? - [S=3]
RewriteRule ^(tube|login|register|facebook|logout)\.html$ $1.php [L,QSA]
RewriteRule ^page/([A-Za-z0-9-]+).html$ page.php?prefix=$1
RewriteRule ^sitemap\.xml sitemap.php [QSA,L]
</IfModule>
i want login.html and all sisters open only on www.domain.com/login.html and give 404 error if opened on games.domain.com/login.html
and also RewriteRule .? - [S=3] not work for me, i'm tried to do
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com:8080$ [NC]
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9-]+)\.domain\.com:8080$ [NC]
RewriteRule .? - [S=3]
RewriteRule ^([A-Za-z0-9-]+)/([0-9-]+)/?(.*)?\.html$ view.php?prefix=%1&cat=$1&id=$2&title=$3 [L,QSA]
RewriteRule ^([A-Za-z0-9-]+)/?((index|news|photos|videos|articles)\.html)?$ categories.php?prefix=%1&cat=$1 [L,QSA]
RewriteRule ^(index\.html)/?$ category_index.php?prefix=%1 [L,QSA]
its not working here and here
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com:8080 [NC]
RewriteRule .? - [S=3]
RewriteRule ^(tube|login|register|facebook|logout)\.html$ $1.php [L,QSA]
This condition is wrong:
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com:8080 [NC]
As you can only match host name using HTTP_HOST variable without port. To make it work use this compound condition:
RewriteCond %{HTTP_HOST}:%{SERVER_PORT} !^(www\.)?domain\.com:8080$ [NC]