Those are the lines of concern currently in my .htaccess file,
note that i have two domains pointing to the same site:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule .* http://www.example.com/ [L,R]
RewriteCond %{HTTP_HOST} ^example2\.com$ [NC]
RewriteRule .* http://www.example.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.example2\.com$ [NC]
RewriteRule .* http://www.example.com/ [L,R=301]
Now I need to convert my site to SSL, I was instructed to use the following
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
I think that this code misses my other domain and the www rewrite part,
how to enable SSL while accounting for my other domain and also for the WWW part?
You can use:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(www.)?example2\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R=301]
Related
I'm trying to accomplish the following:
non-http force to https - works
www force to non-www - works
website loaded from subfolder (/web) - works
test.example.com load different subfolder (/test) - does not work
4, Does not work, the condition is met to go to /web. Can't understand how to change this into /test
the .htaccess code I use:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^test.example.com$
RewriteRule ^(.*)$ /test/$1 [L,NC,QSA]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteRule ^(.*)$ /example/web/$1 [L,NC,QSA]
With your shown samples please try following htaccess rules file here. Please make sure to clear your browser cache before testing your URLs. In case you have further more rules(apart from shown ones) then make sure these Rules are before those rules.
RewriteEngine On
##Apply https to uris here..
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
##Apply non-www to uris here..
RewriteCond %{HTTP_HOST} ^(?:www\.)(example\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
##Apply test to all uris here..
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test [NC]
RewriteRule ^(.*)/?$ example/web/$1 [L,NC,QSA]
##Apply test to all uris here..
RewriteCond %{HTTP_HOST} ^test\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test [NC]
RewriteRule ^(.*)/?$ www.example.com/test/$1 [L,NC,QSA]
We are busy with a name change for our webshop and i am working on our HTACCESS to redirect 1700 links. from those 1700 links there are 177 links that are changing in our new webshop. So they have to be in the HTACCESS. The other links keep the same and i redirect them now with a general rewriterule.
The only problem now is that he does not look well what the exact link is. For example see below my HTACCESS.
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]
RewriteCond %{HTTP_HOST} ^www\.old\.nl$ [NC]
RewriteRule ^vloerkleden/categorie/(.*)$ https://www.new.nl/vloerkleden/ [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.old\.nl$ [NC]
RewriteRule ^vloerkleden/categorie/vintage-vloerkleed/(.*)$ https://www.new.nl/vloerkleden/vintage-vloerkleed/ [R=301,L]
RewriteCond %{HTTP_HOST} ^old.nl [NC,OR]
RewriteCond %{HTTP_HOST} ^www.old.nl [NC]
RewriteRule ^(.*)$ https://www.new.nl/$1 [L,R=301,NC]
When i type now the following url in the browser www.old.nl/vloerkleden/catagorie/vintage-vloerkleed/ he links me to www.new.nl/vloerkleden in stead of www.new.nl/vloerkleden/vintage-vloerkleed.
Your problem in this line :
RewriteRule ^vloerkleden/categorie/(.*)$ https://www.new.nl/vloerkleden/ [R=301,L]
Nothig to present (.*)$ in substitution https://www.new.nl/vloerkleden/ so , it should look like this https://www.new.nl/vloerkleden/$1 because $1 will represent (.*) in pattern.
Also , you could do this with another rules and you could also sumerize your rules like this :
RewriteEngine on
# the folwoing rules will force every request for both old & new into https://wwww:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^(?!.*www\.)(.*)$
RewriteRule .* https://www.%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?old\.nl$ [NC]
RewriteRule ^vloerkleden/categorie/(.*)$ https://www.new.nl/vloerkleden/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?old\.nl$ [NC]
RewriteRule ^vloerkleden/categorie/vintage-vloerkleed/(.*)$ https://www.new.nl/vloerkleden/vintage-vloerkleed/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?old.nl [NC]
RewriteRule ^(.*)$ https://www.new.nl/$1 [L,R=301,NC]
Note: clear browser cache then test.
I have to redirect my domain from http to https. In my htaccess I have already.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
This snippet redirect everything without "www" to "www".
When I change this to
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
The result is:
http://www.example.com/folder/page.php
becomes
Location => https://www.example.com/folder/page.php
Fine!
https://example.com/folder/page.php
becomes
https://www.example.com/folder/page.php
Fine!
but:
http://example.com/folder/page.php
becomes
Location => https://example.com/folder/page.php
but it has to be
Location => https://www.example.com/folder/page.php
How is it possible to fix this?
I know all of this redirects:
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
but I need only one redirection instead of two 301.
You can use the following rule
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://www.%1%{RERUEST_URI} [NE,L,R=301]
Clear your browser cache before testing this.
I have found a solution here:
https://webmasters.stackexchange.com/questions/84757/htaccess-redirect-non-www-to-www-with-ssl-https
The second answer by #w3dk is working.
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
Your site needs to be accessible by both www and non-www over SSL for the .htaccess redirect to trigger.
With letsencrypt its no problem to have this 2 certs.
Ok, so i want to redirect all www to non www link,
I want to redirect www.example.com to example.com, and i can do that too using this code.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
But when someone try to access www.example.com/example, that redirects the site to example.com,
I want a code that can redirect www.example.com/example or any subfolder to example.com/example or any subfolder.
Kinda like a wildcard entry, how to do that?
Also if possible force ssl on it too.
Thank You.
If I've understood your question correctly:
www.example.com/dir/anypage.html to example.com/dir/anypage.html (same page)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com [nocase]
RewriteRule ^(.*) http://example.com/$1 [last,redirect=301]
OR Use:
RewriteCond %{HTTP_HOST} ^(www\.example\.com)?$
RewriteRule ^(.*)$ http://example.com/subfoldername/$1 [R=301,L]
-------- Alternative---------
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(example\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1 [L]
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(/)?$ /subfolder/index.php [L]
To force SSL:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
hope some could help me.
I have a wordpress blog with 3 domains, .com, .es and .net. The .com is the target.
And i want to redirect from non-www to www as well.
The redirect from .es to .com works fine, and .net to .com too. But when i add the redirect from non-www to www is not working and the site doesnt load.
This is my .htaccess file...
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^myblog.com [NC]
RewriteRule ^(.*)$ http://www.myblog.com/$1 [L,R=301,NC]
#RewriteCond %{HTTP_HOST} ^myblog.es$ [OR]
#RewriteCond %{HTTP_HOST} ^www.myblog.es$
#RewriteRule ^(.*)$ "http\:\/\/www\.myblog\.com/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^myblog\.es$ [OR]
RewriteCond %{HTTP_HOST} ^www\.myblog\.es$
RewriteRule ^(.*)$ "http\:\/\/myblog\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^myblog.net [NC,OR]
RewriteCond %{HTTP_HOST} ^www.myblog.net [NC]
RewriteRule ^(.*)$ http://www.myblog.com/$1 [L,R=301,NC]
These rules should suffice for your needs:
RewriteEngine On
# redirect myblog.es and myblog.net (with or without www.) to www.myblog.com
RewriteCond %{HTTP_HOST} ^(www\.)?myblog\.(es|net)$
RewriteRule ^(.*)$ http://www.myblog.com/$1 [R=301,L]
# Force www on myblog.com
RewriteCond %{HTTP_HOST} ^myblog.com [NC]
RewriteRule ^(.*)$ http://www.myblog.com/$1 [L,R=301,NC]