Yaroslav Velychko Yaroslav Velychko - 1 year ago 99
Apache Configuration Question

redirect htaccess from non-www to www for subapplication

I have sub application (IPB forum) in my frontend part of yii2-advanced application in web folder. I need to make redirect for this forum in htaccess from non-www to www.
I have same problem in frontend htaccess and I solve it successfully with next rules:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?r=$1 [NC,L,QSA]

I tried this one, and doesn't work

RewriteEngine on
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} forum
RewriteRule ^(.*)$ http://www.example.com/forum/$1 [R,L]

My link for frontend part:
example.com -> www.example.com

And I need same for subapplication:
example.com/forum -> www.example.com/forum

Answer Source

Try it like this

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download