开发者

.htaccess: Redirect root url to subdirectory, but keep root URL structure

开发者 https://www.devze.com 2023-03-23 13:33 出处:网络
I have created a site in a subdirectory and would like the site to appear as if it\'s in the root. I used the below Mod_Rewrite code to get the site root to redirect to the subdirectory, but I would

I have created a site in a subdirectory and would like the site to appear as if it's in the root.

I used the below Mod_Rewrite code to get the site root to redirect to the subdirectory, but I would like the folder the files are held in to not appear.

currently: www.example.com/sitefiles/content/

Would like: www.example.com/content

Thanks

Options +FollowSy开发者_运维百科mlinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteRule ^$ /sitefiles/    [L]
RewriteRule (.*) /sitefiles/$1 [L]


If it needs to be done via .htaccess and mod_rewrite, then use this:

Options +FollowSymlinks -MultiViews

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} !^/sitefiles/
RewriteRule (.*) /sitefiles/$1 [L]
  1. Since you do not explicitly specify the page when website root will be hit, then there is no need for this line: RewriteRule ^$ /sitefiles/ [L]

  2. You need to add condition for your main rewrite rule to prevent rewrite loop (when already rewritten URL gets rewritten again): RewriteCond %{REQUEST_URI} !^/sitefiles/


Well, the directory should have no reason to appear in any listing, and that mod_rewrite you posted should allow you to do as you said. Still, I would go about it a different way:

Options +FollowSymLinks +Indexes

RewriteEngine on
RewriteBase /
RewriteRule ^$ sitefiles/ [L]
RewriteRule ^(.+)$ sitefiles/$1 [L]

Hope this works.

0

精彩评论

暂无评论...
验证码 换一张
取 消