I'm at a basic level of understanding when it comes to .htaccess redirects but I'm managing to just about get by with a site I'm redeveloping. I have searched Stack Overflow quite extensively, but can't understand how to put together all the advice in the context of my own situation.
I'm switching my WordPress the site to https and so far have managed to cobble together the following set-up:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [NC,R=301,L]
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I have a few bugs I'd like to iron out though, currently http:// redirects to https:// which then redirects to https://www.
How do I set up an additional rule to move everything from http:// directly to https://www. ?
Also there are some pages I'd like to redirect to new URLs in the https site. http://www.example.com/bunny to https://www.example.com/rabbit. How is the best way (least 301 chains) to redirect these from insecure to a new secure URL?
I advice you to replace your redirect rules to this (adapt your domain):
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
You don't need to use RewriteCond %{ENV:HTTPS} !=on
You need to something like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
and in wp-admin:
wordpress site Address Url : https://Your_site_url
Site Address Url:https://Your_Site_Url
Related
I have web page created in Wordpress v5.4, Theme Twenty Twelve and just installed SSL certificate.
I want to have such redirection structure (redirection codes in rectangles), to optimize for SEO:
but according to Google Chrome Site Inspector I have the follwing one:
So the schema is not fully optimized as for two cases there are dwo cascade redirections whereas should be one.
What is weird, current schema doesn't reflect .htaccess file. My .htaccess file looks as follows:
# 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
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
So, my understanding of the code is that if url is typed in without https://, script rewrites it into version with https:// an nothing else, so there must be another place where other redirections (e.g. with www -> without www) are being made.
I have checked index.php - no redirections.
I also checked additional lines in .htaccess file don't overwrite those "hidden" ones, so if I simply add them I got multiple redirections error in web browser.
Could you please advise me what else should I do to get desired structure of redirections?
First rule redirect all www to https://domainname.com/, second all http to https://domainname.com/, one hop
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) https://domainname.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://domainname.com/$1 [R=301,L]
</IfModule>
# 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
I’m guessing the redirect from www to non-www is due to the address you’ve set in your settings (WordPress dashboard → Settings → General).
For the redirect you want to achieve, you have to add rewrite conditions for the www. in your .htaccess
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
I've got a domain name with three different Top-Level-Domain (TLD), using WordPress and hosted on OVH.
I'd like to redirect each of them to the .com one, using the HTTPS protocol and the www subdomain.
I usually use the following code sample to redirect my websites to https://www.example.com
RewriteCond %{SERVER_PORT} ^80$ [OR]
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]
The problem:
Since I'm using WordPress, there already is a redirection. Furthermore, I've node idea how to properly combine the TLD redirection to the https://www one.
Here is the .htaccess code from 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>
Fastest solution (though not the best one) by modifying the htaccess
Not the best one because WP can modify/overwrite the htaccess. This is not true anymore, since WP uses this function to only rewrite its own part of the htaccess (between # BEGIN WordPress and # END WordPress). Simply add your rules before WP's main rule. Here is an example combining all conditions in one rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} =off [OR,NC]
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTP_HOST} !\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
</IfModule>
Plugin solution without touching the htaccess
You could use a plugin to do that properly. You can find plenty of them by searching on the web.
WordPress' rewrite rules are placed in between # BEGIN WordPress and # END WordPress block:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
...
</IfModule>
# END WordPress
And WordPress uses its insert_with_markers() function to make sure WordPress is only altering its own rewrite rules.
So WordPress wouldn't alter any rewrite rules outside of that block, and therefore you could safely add yours in the .htaccess file.
Now to redirect from a domain to another domain, the following worked well for me:
# BEGIN Redirect to another domain name
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} ^80$ [OR]
RewriteCond %{HTTP_HOST} ^(www.)?(example.net|example.info|example.xyz) [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]
</IfModule>
# END Redirect to another domain name
# Other rules here, if any.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
...
</IfModule>
# END WordPress
Pages on our WordPress site are not automatically redirecting to the HTTPS version even though we have set the site url and home url to use HTTPS.
This is the code we have at the top of our wp-config.php file:
define('WP_HOME','https://webwisemedia.co.uk');
define('WP_SITEURL','https://webwisemedia.co.uk');
define('FORCE_SSL_ADMIN', true);
Our .htaccess file looks like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
If you visit a page on our site, like http://webwisemedia.co.uk/web-wise-in-the-press/, it doesn't redirect to the HTTPS version.
Any ideas why?
You mixed up the Wordpress part of .htaccess and that is why it isn't working. That part is changed every time you update, for example, permalinks. Your .htaccess should look like this:
# Rewrite HTTP to HTTPS
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L]
# 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 leaves the Wordpress part intact and it forces to use https. From now on if you change anything in .htaccess do it below the Wordpress part.
I have a wordpress website somewebsite.com
If I visit it naked like that, it works brilliantly, however if I visit it from www.somewebsite.com I get
Warning: Cannot modify header information - headers already sent by (output started at /home/somewebsite/public_html/index.php:2) in /home/somewebsite/public_html/wp-includes/pluggable.php on line 1121
In the wordpress website general settings it's set to somewebsite.com
and in CPANEL I have the following
an A record with somewebsite.com pointing to the ip address
a CNAME record with www.somewebsite.com pointing to somewebsite.com
Just for kicks I changed the setting in the wordpress general settings to www.somewebsite.com which fixed it for if I was to visit it with the www but broke it for the naked domain, so I switched it back.
my .htaccess looks like
# 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
Any help would be greatly appreciated
Simple is best :-) Redirect non-www to www or www to non-www using .htaccess, so user is going to be redirected before loading any script in php.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
In your example:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Remember to change chmod for .htaccess - it can be overwritten by WordPress in some cases.
We had several domains parked on top of our main website. This was a normal HTML website and we used the following redirect so that the domain would 301 redirect to the proper url to avoid getting dinged by Google.
rewriteCond %{HTTP_HOST} ^ourmainsiteurl\.com$
rewriteRule ^.*$ http://www.ourmainsiteurl.com%{REQUEST_URI} [R=permanent,L]
So the above htaccess code just rewrites the url to www.ourmainsiteurl.com, which is what we want.
Now here's the problem... we installed Wordpress and it has the following default htaccess code:
# 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
I tried to add my redirect code in but that broke Wordpress. Can anyone tell me what I need to use to make this work?
Simply put I want all the parked domains (ie: parkedurl1.com, parkedurl2.com, etc) to be redirected to www.ourmainsiteurl.com with the htaccess file.
Make sure the redirect is before any wordpress rules:
rewriteCond %{HTTP_HOST} ^ourmainsiteurl\.com$ [NC]
rewriteRule ^.*$ http://www.ourmainsiteurl.com%{REQUEST_URI} [R=permanent,L]
# 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
If it's still not working, check to see if wordpress has something turned on that is redirecting, or adding/removing the "www" from the hostname.