Redirecting root of site to subdirectory using .htaccess

我只是一个虾纸丫 提交于 2019-12-25 14:45:45

问题


A site of mine has all the files stored in a subdirectory. Previously, I used a 301 redirect to send visitors from site.com/specific/page to site.com/subdirectory/specific/page.

I am now using this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?site.com$
RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdirectory/$1
RewriteCond %{HTTP_HOST} ^(www.)?site.com$
RewriteRule ^(/)?$ subdirectory/index.php [L]

This means the URL is cleaner. However, is there a way to make the htaccess redirect people using the subdirectory to the cleaner URL? i.e. if someone goes to site.com/subdirectory/specific/page, is there a way to make the URL site.com/specific/page, while showing the content from site.com/subdirectory/specific/page?


回答1:


Yes, buit instead of rewriting, youn would redirect..

RedirectMatch 301 "^/subdirectory/(.*)" "/$1"

That way, if they hit any subdirectory directly, they will be redirected to the cleaner URL, where your rules above will kick in.



来源:https://stackoverflow.com/questions/6898946/redirecting-root-of-site-to-subdirectory-using-htaccess

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