Multisite www redirection (with host) problem

Hello,

I want to redirect all non-file & directory requests to subdomain with www prefix if the requested hosts doesn’t have www in place.

I’m unable to figure out why apache will only redirect subpages, but it wont redirect my main index to www prefix.

This case wont work:
mydomain.com -> should resolve to mydomain.com
mydomain.com/subpage -> will resolve normally to www.mydomain.com/subpage

Heres my .htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    
    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteCond %{HTTP_HOST} !^www\\.(.+)$ [NC]
    RewriteRule ^(.*)$ "http://www.%{HTTP_HOST}%{REQUEST_URI}" [L,R=301]

    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>

The important lines:


    RewriteCond %{HTTP_HOST} !^www\\.(.+)$ [NC]
    RewriteRule ^(.*)$ "http://www.%{HTTP_HOST}%{REQUEST_URI}" [L,R=301]

Does anyone has any idea what I am doing wrong?

Thanks for help

Hello,
this could help:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteRule ^(.*)$ “http://www.%{HTTP_HOST}%{REQUEST_URI}” [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

Hey,

I’m experiencing the same behavior although I added file/directory check before rewrite rule…

Any idea?

The main domain still doesn’t get redirected to www subdomain if no path / request uri.

Thanks for help

If no path/uri is the URL, that doesn’t mean that none was requested. In fact a directory is requested, namely your public html directory. Since that is an existing directory, your rules won’t fire for it.
You’d need an additional rule for just that case


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

Also, there are some things in that config that could be improved. Allow me :slight_smile:


RewriteEngine On
RewriteBase /

You don’t use any of the Redirect* directives, so you don’t need RewriteBase. Remove it.


#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

The .* at the end of the Cond is superfluous: all you care about is that it starts with system, whatever is behind it really doesn’t matter. No need go let the regex engine go looking for something if you don’t care about what it is.
Also, stay clear from :redhot: (.) :redhot: unless you really have no other choice. In this case you do, since you can simply use %{REQUEST_URI} instead of $1 and just match for .? instead of ^(.)$
Lastly, don’t put a / in front of /index.php. If you do, Apache will first go look in the root of the filesystem for your index.php and only look for it in your public html after it can’t be found there. This wastes CPU cycles and hard disk access for nothing.

So:


RewriteCond %{REQUEST_URI} ^system
RewriteRule .? index.php?%{REQUEST_URI} [L]

(Note that %{REQUEST_URI} contains a leading slash, no need to put that in the rule anymore)


#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

Same as above. So:


RewriteCond %{REQUEST_URI} ^application
RewriteRule .? index.php?/%{REQUEST_URI} [L]


#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{HTTP_HOST} !^www\\.(.+)$ [NC]
RewriteRule ^(.*)$ "http://www.%{HTTP_HOST}%{REQUEST_URI}" [L,R=301]

First, don’t create a backreference in the RewriteCond. You’re not using what you’re capturing so there is no need to capture it in the first place. Also, don’t put double quotes around the URL in the RewriteRule, and like I said above, don’t use :redhot: (.*) :redhot:
Lastly, I would remove the empty line half way down here, to make sure the RewriteRule and its attached RewriteConds are nicely grouped together so it’s easier to say they belong together.

So:


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule .? http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


RewriteRule ^(.*)$ index.php?/$1 [L]

See above:


RewriteRule .? index.php?/%{REQUEST_URI} [L]

:slight_smile:

Wow, amazing informative post.

Learned a lot, many thanks for this

One more question:

why didn’t you leave leading slash for application which you did for system folder (which are basicly the same rewrite rules) ?

Thanks

Oops, that was by mistake. You can remove the leading slashes in the target URLs everywhere :slight_smile: