.htaccess routing to subdirectory on server? performance/loading times

走远了吗. 提交于 2019-12-11 01:43:39

问题


I wonder if it is possible to use an .htaccess file to rout my domain directly to a subdirectory on my server?

I bought a webhosting package from a regular webhoster where my domain.com is connected to my root directory of my server. I wonder if it is somehow possible to upload a .htaccess file to my root directory on my server that automatically routs domain.com (/index.php or /index.html) to domain.com/some-directory/ …

And of course I don't want the addressbar to update to domain.com/some-directory. I want my regular domain just to grab its files from the subdirectory instead of my root directory.

Just for better overview on my server.

So again, when calling mydomain.com it renders index.html (or .php) from my root directory on my server. However I want mydomain.com to render /subdirectory/index.html.

If this is possible, how can I do it? Is this bad in some way? Is there something I didn't think of, e.g. are there longer loading times or anything like that with that approach?

Thank you in advance. Matt

update: or is this possible with a DNS setting on my web-admin-panel. I'm able to edit all DNS entries.


回答1:


Try putting this in the htaccess file in your document root:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/some-directory/
RewriteCond %{DOCUMENT_ROOT}/some-directory%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/some-directory%{REQUEST_URI} -d
RewriteRule ^(.*)$ /some-directory/$1 [L]

Keep in mind if you try to access a directory and are missing a trailing slash, mod_dir will redirect the browser to the URL with the trailing slash and will expose "/some-directory/".


EDIT

Additionally, the 2 -f and -d conditions are to make sure the request is actually for a resource that exists. This isn't required, and if you have further htaccess files with rewrite rules, you probably don't want these. They ensure that if someone requests some bogus URL, the 404 message will be handled outside of the /some-directory/ directory. Otherwise the 404 message will say something along the lines of:

The requested URL /some-directory/blahblahblah was not found on this server.

Exposing that everything is being routed through /some-directory/.



来源:https://stackoverflow.com/questions/12042347/htaccess-routing-to-subdirectory-on-server-performance-loading-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!