M
M
Mikhail Kacherov2015-10-19 11:37:04
Search Engine Optimization
Mikhail Kacherov, 2015-10-19 11:37:04

How to write 301 redirect in htaccess file from www to non-www for directories beyond the second one?

Task
Make a 301 redirect for all pages of the site from addresses with www to addresses without www.
Now this solution works for the main and first-level sections:

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

For sections of the second level and below, when entering a url with www, it gives 404.
Question
What should be added to htaccess so that all pages of the site with www are redirected to pages without www, regardless of nesting?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Savenko, 2015-10-29
@savenko

Hello!
Proven solution:

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question