最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url rewriting - How do I redirect to a non-www version and make it the default URL?

programmeradmin0浏览0评论

How can I redirect my website to a non-www version and also make the non-www version the default version.

This is what I want:

/

And this is what is currently appearing on my site:

/

How can I redirect my website to a non-www version and also make the non-www version the default version.

This is what I want:

https://example/

And this is what is currently appearing on my site:

https://www.example/

Share Improve this question edited Feb 24, 2019 at 12:30 Peter Mortensen 2682 silver badges10 bronze badges asked Aug 28, 2017 at 13:38 KaustubhKaustubh 314 bronze badges 2
  • 1 What web server do you using? Apache, nginx? Can you modify htaccess or nginx config? – Anton Lukin Commented Aug 28, 2017 at 13:39
  • What do you have in fields WordPress Address and Site Address on the Settings > General page in admin? Do those contain www? – Milo Commented Aug 28, 2017 at 16:56
Add a comment  | 

1 Answer 1

Reset to default 1

This process can be broken into 2 steps:

Using .htaccess for Redirection

First, you should redirect any traffic from www to non-www version of your website, by using a simple rewrite rule:

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

This works for any website regardless of the domain, and covers both HTTP and HTTPS.

Defining the Canonical

If you are not using any SEO plugin, I suggest you add a line for search engines, to tell them which one of the protocols is the main. This can be done by using a code like this:

add_action( 'wp_head', 'add_my_canonical' );
function add_my_canonical(){
    echo '<link rel="canonical" href="'.site_url().'" />'
}
发布评论

评论列表(0)

  1. 暂无评论