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.
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]
The url gets redirected to https://2day.news/ instead of https://www.2day.news/.
This happens on every browser/mobile, cleared caches. I don't think this has anything to do with a server back-up as it goes blank when I change the redirect to a page that doesn't exist.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^2day.news [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.2day.news/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Add 301, www and https after RewriteBase
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.2day\.news$ [NC]
RewriteRule ^(.*)$ https://www.2day.news/$1 [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
On some servers, redirecting from non-www to www in the .htaccess will cause Too many redirections error. So I recommend changing the siteurl in the database.
You can change the URL of the site in the wp_options table, in the option_value column of the following options: siteurl, home.
Or alternatively in the WP admin, under Settings > General (Wordpress URL and Site URL).
You need to add the www. prefix for both options.
Please note:
Both records should be the same (or some functions may not work because of cross-domain issues)
You need to login again to WP admin after the changes
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 created a website few days back with a url http://abctest.com in wordpress. Now the website has been published to a domain http://www.bcdefg.com.
The server is same, I have not migrated the site, only the domain has been pointed to the same server. I have changed the wphome and wpsite url to respective domain.
What I want to achieve is to redirect all http://abctest.com or http://www.abctest.com requests to http://www.bcdefg.com.
I tried to use .htaccess redirection but that leads to Too many redirection error. Any solution ?
Below is my htaccess code.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !bcdefg.com$ [NC]
RewriteRule ^(.*)$ http://www.bcdefg.com/$1 [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Error is on your rewriteCond : your host is www.bcdef.com and not bcdef.com (www is important)
try replacing this
RewriteCond %{HTTP_HOST} !bcdefg.com$ [NC]
by this :
RewriteCond %{HTTP_HOST} !www.bcdefg.com$ [NC]
I have a blog under example.com/blog, but now I changed to blog.example.com, which redirects to the same old folder.
I don't want users to access directly the old URL, so, redirecting /blog to blog. How can I do it?
I would prefer using .htaccess. Current is WordPress default:
# 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
You can try this
RewriteRule ^blog/(.*)$ http://blog.example.com/$1 [R=301,L]
or
You could do something like this that would check from the main domain - This checks if you are already at blog.example.com otherwise it will redirect.
RewriteCond %{HTTP_HOST} !^blog\.example\.com$ [NC]
RewriteRule ^blog/$ http://blog.example.com [L,NC,R=301]