简体   繁体   中英

How to redirect non www to www using htaccess in wordpress

I want to redirect this both url http://example.com/robots.txt and http://example.com/sitemap.xml to http://www.example.com/robots.txt and http://www.example.com/sitemap.xml .

Also I have also redirected my http://example.com to http://www.example.com but sitemap and robots open in both url (non www) and (www) so I only want that it only open in www format.

I tried using below code

RewriteRule sitemap\.xml  http://example.com/sitemap. [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

And

RewriteRule ^sitemap\.xml http://www.example.com/sitemap.xml [L,R=301]

But didnt work for me please help me to resolve this issue.

Your first redirect rule is wrong. Use this:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(sitemap\.xml|robots\.txt)$  http://www.%{HTTP_HOST}/$1 [L,R=301,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM