简体   繁体   English

使用.htaccess将所有文件/目录重定向到子目录,但不包括主页

[英]Using .htaccess redirect all files/directories to a subdirectory but exclude the homepage

I set up my web server having created a .htaccess file and subdirectory of 'www' in which I placed my Wordpress install. 我设置了Web服务器,创建了.htaccess文件和“ www”子目录,并在其中放置了Wordpress安装文件。

Having run the install, everything appears to be redirecting as would be expected with the exception of the home page which throws a 404 error as the redirect rules contained in the .htaccess file are adding /www/ to the URL. 运行安装程序后,一切似乎都在按预期方式进行重定向,除了主页外,由于.htaccess文件中包含的重定向规则将/ www /添加到URL,该页面引发404错误。

Guessing it's probably something simple, but need some objective help. 猜测可能很简单,但需要一些客观帮助。

Obviously my actual domain has been substituted for 'domain.com' 显然,我的实际域名已被替换为“ domain.com”

# Turn off indexing of files and directories.
Options -Indexes

# Turn on rewrites.
RewriteEngine on

# Force HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Don't apply to URLs that go to existing files or folders.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Only apply to URLs that aren't already in the subdirectory.
RewriteCond %{REQUEST_URI} !^/www/

# Rewrite all those to insert the subdirectory name.
RewriteRule ^(.*)$ /www/$1

# Redirect the root folder.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ www/ [L,R=301]

Turns out it's a simple fix just as I had first thought. 事实证明,就像我最初想到的那样,这是一个简单的修复。

By replacing [L,R=301] in the final set of rules with [L] prevents the home page coming back to itself with the addition of the subdirectory. 通过将最终规则集中的[L,R=301]替换为[L]可以防止主页通过添加子目录而返回到其自身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM