Regular expressions are correct and would be met, but your conditions prevent redirection from occurring.
RewriteCond %{REQUEST_URI} !^/public/.*$
- Check if the route /public/
appears in the requested URI,
RewriteRule ^(.*)$ /public/$1 [QSA,L]
- If the condition is true, apply the rule, now look at the flags you place at the end of this condition:
-
[QSA]
means adding, the condition is added to existing ones.
-
[L]
means last , or last rule in English. After applying this rule, the .htaccess
file stops processing.
It is no longer necessary for the following condition to be verified, because you have already found the L
flag that stops everything.
On the other hand, the redirection always stops the processing of the following rules, because in strict sense example.com
and www.example.com
are two different domains.
Solution
Remove the L
tag from your first rule.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1 [QSA]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
You can check all the options of the rediction rules in the official documentation .
p>