How to redirecting non-www to www with .htaccess for all page's? in my main page(index) works, but if i go mywebsite.com/page2, will not redirect me to my preference domain www.mywebsite.com/page2. How can i fix it?

Dani AI

Generated

Quick expert note that ties the thread together: the examples from and point in the right direction (check Host, redirect and keep the path), but the symptom described by — index redirecting while other pages do not — almost always comes from one of three things: a syntax error in the condition (flags accidentally written inside the regex), the redirect block being placed after other rewrite rules (so CMS/front-controller rules run first), or a cached 301 response in the browser.

A clean, modern (Apache 2.4+) pattern that preserves the full path and the scheme looks like this:

RewriteEngine On
# canonicalize to www.mywebsite.com, preserve http/https and the request path
RewriteCond %{HTTP_HOST} !^www\.mywebsite\.com$ [NC]
RewriteRule ^ %{REQUEST_SCHEME}://www.mywebsite\.com%{REQUEST_URI} [L,R=301]

Notes: replace mywebsite.com with the real domain and escape dots. %{REQUEST_URI} keeps /page2 (and any query string). %{REQUEST_SCHEME} is available on Apache 2.4+. For older Apache, use two rules that check %{HTTPS} on/off and redirect to the explicit https:// or http:// URL.

Troubleshooting checklist (order matters): put the redirect block at the very top of .htaccess before any CMS/RewriteBase rules; ensure flags are in square brackets after the condition/rule (not embedded in the regex); avoid or change Options +FollowSymLinks if the host forbids it; clear browser cache or test with curl -I to avoid a cached 301; check server error/access logs if redirects still do not appear. The referer lines auto-inserted into the footer are unlikely to stop a simple host redirect, but the redirect must run before other rules that internally rewrite requests.

Recommended Answers

All 12 Replies

There are several examples all over the internet. if you just perform a quick search, you'd find them.

here is one example...

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$  [R=301,L]

Yes man i know that, but works just from index page, if i type myweb.com/page1 dosen't redirect me to www.myweb.com/page1

it may be helpful if show your rule(s).

the $1 parameter passes everything after .com to the rewritten url
it should work
except when something is missed or changed on the server config.
many servers are locked down, and if the locking is . .. . less than perfect . ..
try

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*)$  [R=301,L]

which covers the more common apache config errors

one stupid little carat

Hi again, i use this

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mywebsite.com[nc]
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [r=301,nc]

And almostbob i try your version but still dosen't work.

then its not an apache config error
your code is not good

line 4, there should not be a nocase [NC] flag it should be L,
rewrite condition in line 3 creates an infinite loop
search for mysite.com in the host string, redirect to www
then do it again to the redirect url, and again and again

the code samples given all have a rewrite condition
RewriteCond %{HTTP_HOST} !^www\.
search for url that do NOT include www., and redirect to www.

Soo how i can redirect each page separately?

Give me an exemple for: mywebsite.com/page1

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*)  [R=301,L]

I assume you replaced 'site' with your site name

now i see in the footer of the htacces create's automatically some lines..

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://mywebsite.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^      [NC]
RewriteCond %{HTTP_REFERER} !^http://www.mywebsite.com/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^      [NC]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.