I want to rewrite and redirect my http:\\www.domain.tld to https:\\www.domain.tld
And I want to rewrite and redirect my domain.tld to www.domain.tld
I want to have something that redirect and, with seo concern, shows that's a redirection.
For now I have something like this:
1)
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE,QSA]
or
2)
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE,QSA]
What's the best? Is there something wrong?
Thank you
The Apache docs recommend against using a rewrite: redirect to https
To redirect http URLs to https, you should do the following:
<VirtualHost *:80>
ServerName www.example.com
Redirect / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
# ... SSL configuration goes here
This snippet should go into main server configuration file, not into .htaccess as asked in the question.
But if you don't have access to the main server configuration file you could use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
Line 1: if the url starts without www
line 2: if there is no https
line 3: rewrite all urls from this domain to one that starts with https and have www
Related
I had some issues with my apache configuration and I'm trying to isolate the problem.
I came up with the following lines which are not working :
For testing purposes, I'm trying to redirect all https traffic to Yahoo
The redirection is not working and my web site is showing the index.html file stored in public_html
Listen 443
NameVirtualHost *:443
<VirtualHost *:443>
DocumentRoot "${SRVROOT}/htdocs/example.com/public_html"
ServerName example.com
ServerAlias example
Redirect permanent / https://www.yahoo.com/
</VirtualHost>
Can anyone help please ?
Thanks
Use this for the redirect, also enable mod_rewrite for this to work:
Add this to the top of your .htaccess file:
RewriteEngine On #Don't use RewriteEngine On twice in one .htaccess file
RewriteBase /
RewriteRule ^(.*)$ https://www.yahoo.com [R=301,L]
Clear your browser's cache to have a different redirect than yahoo.com, once it works.
Just in case
If you want all traffic of your website to redirect to https:// on the same domain, do the following, don't remove RewriteEngine On and RewriteBase /:
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
If you want to redirect all traffic to one domain with https:
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST] !^example.com
RewriteRule ^(.*)$ https://example.com [R=301,L]
If you want to redirect all traffic to one domain with https and www:
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST] !^www.example.com
RewriteRule ^(.*)$ https://www.example.com [R=301,L]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
I use this in my httpd.config and it works:
<VirtualHost *:80>
ServerName olddomain.com
ServerAlias www.olddomain.com
Redirect permanent /FolderName/Filename_with_underscores.html http://newdomain.com/some-folder-with-dashes/?lang=fr
Redirect permanent /FolderName/Other_filename.html http://newdomain.com/some-other-folder/?lang=fr
Redirect permanent / http://newdomain.com/
</Virtualhost>
Now I'd like to put this in the .htaccess file in the root of the website.
I have tried several things but it keeps failing.
Does anyone know how to translate this httpd redirect to .htaccess?
There are capitals, dashes and underscores in the url's...
The code below works (but it's only half the story: it redirects all (www.)olddomain.com to the desired folder at newdomain.com):
RewriteCond %{HTTP_HOST} ^olddomain\.com\$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^/?$ "http://newdomain.com/some-folder-with-dashes/?lang=fr" [R=301,L]
Thnx
You can use in your root .htaccess:
RewriteEngine on
# All rules only for olddomain.com
RewriteCond %{HTTP_HOST} !^(www\.)?olddomain\.com$ [NC]
RewriteRule ^ - [L]
RewriteRule ^FolderName/Filename_with_underscores\.html$ http://newdomain.com/some-folder-with-dashes/?lang=fr [NC,R=301,L]
RewriteRule ^FolderName/Other_filename\.html$ http://newdomain.com/some-other-folder/?lang=fr [NC,R=301,L]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
I currently have some rules which work for directing http://domain.com to https://www.domain.com, but it doesn't work (I guess it's not matching?) for http://www.domain.com, which should redirect to https://www.domain.com. Could someone modify the below to do this? I've tried quite a few things but haven't been successful, this is my first time with .htaccess rewrite rules.
TL;DR, I need to redirect to both WWW and HTTPS
RewriteEngine On
RewriteRule .? - [E=PROTO:http]
RewriteCond %{HTTPS} =on
RewriteRule .? - [E=PROTO:https]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ %{ENV:PROTO}://www.%{HTTP_HOST}/$1 [R=301,L]
EDIT: I've found the following code, which somewhat works - It redirects both to https://www.domain.com, but it gives a "Too many redirects" error.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} (www\.)?(.+)$ [NC]
RewriteRule ^ https://www\.%2%{REQUEST_URI} [L,R=301]
Have you tried something like this?
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
It would be better to modify this in the VirtualHost for the server.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.domain.com
ServerAlias domain.com
Redirect permanent / https://www.domain.com/
</VirtualHost>
<VirtualHost _default_:443>
ServerName www.domain.com
DocumentRoot /www/
SSLEngine On
# etc...
</VirtualHost>
This is the rule I'm using:
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>
You could include RewriteCond %{HTTPS} !=on as the first condition to force remove https if that's neccesary...
Hope that helps...
Having a difficult time finding the combination to satisfy the following 3 conditions. What Rewrite rules and conditions will accomplish the conditions? (I've already been surprised by the rules not working.)
www stripped from all requests
https for all requests to primary
http for all requests to subdomain (in subfolder of main site) subdomain.com
htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.primary\.mobi [NC,OR]
RewriteCond %{HTTP_HOST} ^primary\.mobi [NC,OR]
RewriteCond %{HTTP_HOST} !^(www\.)?subdomain\.com [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The above do not strip www and send www.subdomain to https.
Explanations welcomed. Trying to understand the apache mod_rewrite manual page and have tried several methods without success.
You can capture the domain and use it in your RewriteRule. HTTP_REQUEST is not available in the substitution part, but only in RewriteCond directives.
I'm not sure, but you can try to split this into two .htaccess files. This one goes into the main directory
RewriteEngine On
# remove www. from HTTPS requests
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(primary\.mobi)$ [NC]
RewriteRule .* https://%1/$0 [R,L]
# redirect HTTP requests to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(primary\.mobi)$ [NC]
RewriteRule .* https://%1/$0 [R,L]
and this is for the .htaccess in the subdomain folder
RewriteEngine On
# remove www. from HTTP requests
RewriteCond %{HTTP_HOST} ^www\.(subdomain\.com)$ [NC]
RewriteRule .* http://%1/$0 [R,L]
# redirect HTTPS requests to HTTP
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(subdomain\.com)$ [NC]
RewriteRule .* http://%1/$0 [R,L]
Test your rules without 301, because the browser caches 301 results and makes testing much harder. Add R=301 not until you're satisfied with the rules.
In Canonical Hostnames are some alternatives described, especially the first one, using virtual hosts, looks promising
<VirtualHost *:80>
ServerName www.primary.mobi
Redirect / https://primary.mobi/
</VirtualHost>
<VirtualHost *:80>
ServerName primary.mobi
</VirtualHost>
<VirtualHost *:80>
ServerName www.subdomain.com
Redirect / http://subdomain.com/
</VirtualHost>
<VirtualHost *:80>
ServerName subdomain.com
</VirtualHost>
I don't know, if this is feasible for you, but you might try.
This works awesome for me in that it redirects www.domain.com and domain.com to https://www.domain.com
My problem is that subdomains like sub.domain.com now are redirected there as well. How do I keep the existing functionality but NOT affect the subdomains?
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
Add a restriction:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
The %{HTTP_HOST} line will make sure only hosts domain.com and www.domain.com get redirected.