Redirecting root of site to subdirectory using .ht

2019-08-30 18:46发布

问题:

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.