Score:0

Redirect all pages to non www and one page to www

cn flag

I want to redirect all my internal pages (eg: example.com/about) to their non-www version but I want my homepage (https://example.com) to redirect to its www version (https://www.example.com).

I searched many articles on the internet but it could not work.

If set both rules then the internal pages get redirected fine but the homepage gets trapped in a loop.

I don't know how to code so please it's a request if you could write the code which I should paste in my .htaccess file

Currently, I have set all pages to redirect to their non-www versions (even homepage) with the below code:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
</IfModule>
# BEGIN Sitepad
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
# END Sitepad
Score:0
kz flag

I searched many articles on the internet but it could not work.

Probably because this is a strange requirement. I would be surprised if you could find any code that you could simply copy/paste that does this.

However, assuming you are constructing the correct absolute URL for all your internal links (since you must not internally link to a URL that redirects) then this is relatively trivial to implement in .htaccess if you understand your existing directives.

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

The regex .* in the RewriteRule pattern matches everything. Specifically, it matches 0 or more of any character, as denoted by the * quantifier. It matches the empty URL-path (ie. the "homepage") and every non-empty URL-path (ie. everything else).

You need to alter the above regex so that it matches a non-empty URL-path (ie. everything except the homepage). And create another rule (in the other direction) that matches an empty URL-path only.

(This uses the fact that mod_dir is serving index.php for requests to the homepage, not the mod_rewrite directives that follow.)

To match 1 or more (ie. non-empty URL-path), you can use the + (plus) quantifier, instead of * (star/asterisk).

For example:

# Redirect everything except the homepage (from www to non-www)
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.+)$ https://example.com/$1 [L,R=301]

# Redirect the homepage only (from non-www to www)
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^$ https://www.example.com/ [L,R=301]

You will need to clear your browser cache before testing, since the earlier 301 (permanent) redirect that redirected the homepage from www to non-www will have been cached by the browser.

Test first with 302 (temporary) redirects to avoid potential caching issues.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.