Dadou
Dadou

Reputation: 1008

wordpress htaccess redirected www to folder

I have the subdomain sub.main.com which is redirected to www.sub.main.com/sub for some reason, causing a 404.

FWIW, this is what my .htaccess file looks like in the root

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

default wordpress .htaccess in sub


Edit

The solution proposed by @anubhava has fixed the first problem, being that sub.main.com was redirected to www.sub.main.com/sub, removing the www; however, this still redirects to sub.main.com/sub.

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^main\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

Upvotes: 1

Views: 32

Answers (1)

anubhava
anubhava

Reputation: 785156

Keep redirect rule at top and make it target only main domain:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^main\.com$ [NC]
#RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

Make sure to clear your browser cache before testing this change.


Edit:

Removing the www in the RewriteRule has solved the issue.

Upvotes: 1

Related Questions