htaccess www to non-www for subfolders

94 views Asked by At

I have one of the more recently asked Question (but where i can't find a working solution)

I am trying to accomplish that my domains that were before possible to open with www and non-www show ONLY non-www.

Example: http://www.domain.tld/folder1 and http://domain.tld/folder1 could be opened and showed same result.

I would like to have ONLY non-www available. For the other way around i did found a solution

RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Worked like a charm. Is there any way to just exchange it somehow? That i get always non-www instead of www?

I have tried already like 12 different htaccess codes, but non worked for me. The last one

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

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

didn't even redirected me away from http://www.domain.tld/folder1/, instead i got the error "The requested URL /folder1/ was not found on this server."

Maybe i am just too focused on that problem that i oversee the solution, so hopefully somebody can poke my nose into it :)

Kindly

Requested full htaccess:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

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

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

# 480 weeks
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=290304000, public"
</FilesMatch>

# 2 DAYS
<FilesMatch "\.(xml|txt)$">
Header set Cache-Control "max-age=172800, public, must-revalidate"
</FilesMatch>

# 2 HOURS
<FilesMatch "\.(html|htm|php)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>

<IfModule mod_deflate.c>
    <filesMatch "\.(js|css|html|php|jpg|jpeg)$">
        SetOutputFilter DEFLATE
    </filesMatch>
</IfModule>
0

There are 0 answers