0

I want to redirect non-www URL to www URL in WordPress.

I added code for the redirect but I see that I only redirect URL internally but not accessible from the externally. I redirect to apache default page

I added coed in .htaccess.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

Not work externally. I also set www in secure/non-secure URL.

Does have any solution?

  • "I see that I only redirect URL internally" - what do you mean by that exactly? (The directive you've posted cannot "redirect internally") You've not stated _where_ you've put this code. Most problems like this with regards to WordPress are caused by putting the directive in the wrong place - the order is important. Any external redirect must occur _before_ the WordPress front-controller. – MrWhite Jun 26 '18 at 10:35
  • I added this code in `.htaccess` at the root of the site. – Dhaduk Mitesh Jun 26 '18 at 10:43
  • I check the site in my internal server then it works fine. But when the site is open from outside of my server then site is not redirected to `www`. – Dhaduk Mitesh Jun 26 '18 at 10:44
  • Is the site accessible from "outside" of your server? Are you using the same URL in both instances? Make sure you've cleared your browser cache, as 301s are cached hard by the browser. – MrWhite Jun 26 '18 at 10:53
  • yes the site accessible from "outside" of my server. I also check both url. – Dhaduk Mitesh Jun 26 '18 at 11:04
  • In that case there doesn't appear to be any reason why local requests work and external requests don't. There is nothing "wrong" with the directives you've posted. Either the external request is seeing a cached response, or they aren't actually reaching your server? – MrWhite Jun 26 '18 at 11:14
  • Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/79377/discussion-between-dhaduk-mitesh-and-mrwhite). – Dhaduk Mitesh Jun 26 '18 at 11:14
  • No, It is not duplicate. – Dhaduk Mitesh Jun 27 '18 at 09:19

1 Answers1

1

I redirect to apache default page

From our discussion in chat, it would seem that example.com is not accessible from external requests and is not even reaching the server. Whilst www.example.com is accessible and accesses the site OK.

This would seem to suggest there is a DNS issue. There should be an A record that points the domain apex (example.com) to your server's IP address. The server must also be configured to accept requests at this host - but this looks as if it should already be configured since local requests work OK.

MrWhite
  • 11,643
  • 4
  • 25
  • 40