HTACCESS: Change domain root to sub-directory

泪湿孤枕 提交于 2019-12-22 04:32:29

问题


I'm trying to use a sub-directory as the root folder for one of my domains. Using .htaccess, I use mod_rewrite's to get the job done. Here's the code I already have:

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/domain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /domain/$1

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ /domain/index.php [L]

This gets the job done, but not entirely. For example:

If I goto http://domain.com/, it displays index.php from inside the domain folder. If I goto http://domain.com/test/, it will display the contents (or 403) of the test folder. BUT if I goto http://domain.com/test (for use of shortcuts, or even to display the folder) I get redirected to http://domain.com/domain/test/.

That is not supposed to happen. If anything, it either does a mask from the .htaccess (if test is being used) or should just goto http://domain.com/test/. I have tried to figure out a way around this, and I cannot. So I am seeking your help! :)

Any and all help is greatly appreciated.


回答1:


Try this: its a little crude, but should do what you want. There is a little bit of confusion: some of what I've tried to do will depend on your RewriteBase (you might need to remove one or more / characters).

I've basically added an initial block that specifically looks for directories within your /domain/ folder that don't end with a trailing slash and added one. Let me know if it works at all.

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/domain/
RewriteCond /domain/%{REQUEST_URI} -d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /domain/$1/

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/domain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /domain/$1

RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ /domain/index.php [L]



回答2:


The commenter is right, if you want to use a sub-directory as the root folder for one of my domains , just configure it in you apache virtual host configuration (DocumentRoot).




回答3:


RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(.*)$ /domain/$1/ [L]

The above code successfully does everything I wanted it to do.




回答4:


Use RewriteBase like this:

RewriteBase /f2f/f2fweb




回答5:


What helped me is auto.htaccess generator on my host!!!!

DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://(www\.)?domain.com/TEST//.*$ [NC]
RewriteRule \.(js|css|jpg|gif|png|bmp|mp4|3gp|m4a|m4r|aac|mp3|ogg|wave)$ - [F]


来源:https://stackoverflow.com/questions/5986473/htaccess-change-domain-root-to-sub-directory

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