Cârnăciov
Cârnăciov

Reputation: 1144

http to www while preserving subfolder?

I have the following rule in .htaccess to redirect from domain.tld to www.domain.tld and it's working fine.

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

However, when trying to access domain.tld/forum , I get redirected to www.domain.tld

How can I redirect all non-www to www while preserving whatever subfolder the visitor is in?

Upvotes: 2

Views: 29

Answers (1)

anubhava
anubhava

Reputation: 785541

Use REQUEST_URI variable:

RewriteEngine On

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

%{REQUEST_URI} will have full request URI rather than relative path (from current directory) captured in $1

Upvotes: 2

Related Questions