Changing the root folder via .htaccess
I've got a shared host开发者_如何学Cing account associated with a domain name and the root folder (correct me if that's the wrong term) is set to /
so that all files on the server are public / accessible through the browser.
Can I use .htaccess or something to change the root folder to something like /example.com/public/
?
If I'm understanding correctly, the following should work
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,R=301]
This will redirect all requests that do not begin with /public/
to URL that does.
Hope that helps.
This is how i always use it in my framework:
Rewritecond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /example.com/public/$1 [L,NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule !^example.com/public/(.*) /example.com/public/$1 [L,NC]
The DocumentRoot directive can't be set in a .htaccess file, only in the server config. As you most likely don't have the privileges to modify the server settings your only solution is to use some rewrite magic as clmarquart already mentioned.
I use bluehost... this is what works for me: This is helpful when you are on shared hosting, and have multiple domain names.
Your primary domain is set to public_html but your add-on domains are subfolders inside public_html
This makes it so you don't have to have all your primary domain name files be mixed with add-on domain folders... each domain can be in their own folder...
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?PUTYOURDOMAINNAMEHERE.com$
RewriteCond %{REQUEST_URI} !^/PUTYOURFOLDERHERE/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /PUTYOURFOLDERHERE/$1
RewriteCond %{HTTP_HOST} ^(www.)?PUTYOURDOMAINNAMEHERE.com$
RewriteRule ^(/)?$ PUTYOURFOLDERHERE/ [L]
Options +SymLinksIfOwnerMatch
I use this:
# .htaccess main domain to subdirectory redirect
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !example/public/
RewriteRule (.*) /example/public/$1 [L]
精彩评论