.htaccess 将子文件夹设为主域名的主文件夹,但当直接在浏览器中键入 URL 时,会出现冗余重定向



问题:

我正在使用 .htaccess 将子文件夹设为主域名的主文件夹。一切似乎都工作正常,除了我注意到当我直接输入 URL 时,例如"primarydomain.com/blog/",它重定向到"http://primarydomain.com/primarydomain.com/blog/"。

详:

目前,我在同一虚拟主机帐户下有多个域。这个虚拟主机最初将我的文件组织为:

  • public_html/对于我的主域名的所有网络文件 (primarydomain.com(
  • public_html/domain2.com/用于我的其他域之一的所有网络文件
  • public_html/domain3.com/用于我的另一个域的所有网络文件

使用.htaccess和RewriteRule,我已经能够将我的文件重新组织成:

  • public_html/主域.com/
  • public_html/域2.com/
  • public_html/域3.com/

。在该根目录中使用上述 .htaccess 文件:

  • public_html/.htaccess

这个.htaccess文件如下:

<IfModule mod_rewrite.c>
RewriteEngine On
# Change yourdomain.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?primarydomain.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/primarydomain.com/
# Don't change this line.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /primarydomain.com/$1
# Change yourdomain.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?primarydomain.com$
RewriteRule ^(/)?$ primarydomain.com/ [L]
# this last line I commented out because it was causing all the links (direct links) to all my files to hit 404 errors
# I have included this line as a reference because it was in the original tutorial I used
# RewriteRule ^(.*)(/)?$ primarydomain.com/$1 [L]
</IfModule>

我使用的教程:http://support.lunarpages.com/knowledge_bases/article/549(我不使用月球页面,但结构是一样的(

不确定我做错了什么。 任何提示/提示将不胜感激。 谢谢!

如果您只想将域所在的物理目录移动到primarydomain.com文件夹,则以下规则应该有效。我省略了您原始规则的最后一部分,因为我不确定它们的目的是做什么。

RewriteEngine on
RewriteBase /
#if the host is on primarydomain.com or www.primarydomain.com
RewriteCond %{HTTP_HOST} ^(www.)?primarydomain.com$ [NC]
#and URI does not already start with /primarydomain.com/
RewriteCond %{REQUEST_URI} !^/primarydomain.com/ [NC]
# And not for an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#then rewrite all requests that to primarydomain.com/ folder
RewriteRule (.*) primarydomain.com/$1 [L]

如果是缺少斜杠的问题,您可以尝试DirectorySlash Off,当目录缺少尾部斜杠时,mod_dir自动重定向。它有时会干扰mod_rewrite。请注意,关闭它时可能存在安全问题。

最新更新