.htaccess 在子目录中重写
.htaccess rewrite in subdirectory
我目前正在通过一个托管帐户部署多个站点。我将所有网站都放在自己的文件夹中,包括主域。我遇到的问题是,当我用当前代码重写主域地址时,它包含其中的子目录。所以目前,如果我输入 http://www.example.com/url
,它会重写为 https://example.com/folder/url
。我只是想让它在没有文件夹的情况下重写。
任何想法。我知道 运行 我的主域在子目录中,这让事情变得复杂,只是想尽可能地清理托管。
在我的 public_html
.htaccess
文件中:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder/
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ folder/index.php [L]
并在 public_html/folder
.htaccess
:
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/ [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
So currently if I type in http://www.site.whatever/url
it rewrites to https://site.whatever/folder/url
.
这是 "redirect",不是重写。
发生这种情况是因为在 public_html/folder
.htaccess
文件的 HTTP 到 HTTPS 重定向中使用了 REQUEST_URI
服务器变量:
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
REQUEST_URI
服务器变量包含请求的完整 URL 路径,在调用子目录的 .htaccess
文件时,该路径已更新为包含 /folder
.
您需要:
- 将您的规范 www 移动到非 www 并将 HTTP 移动到 HTTPS 重定向到文档根目录中的
.htaccess
文件。 (如果您的 public_html/folder
.htaccess
文件中没有其他 mod_rewrite 指令,这会更好。)
或者,
修改上述指令以使用 </code> 反向引用(到捕获的 <code>RewriteRule
模式 ),就像您在之前的 www 到非 www 重定向。例如:
RewriteRule (.*) https://%{HTTP_HOST}/ [R=301,L]
(请注意,这最终应该是一个 301 重定向,一旦您确认它工作正常。)
并且不要忘记转义正则表达式中的文字点。
我目前正在通过一个托管帐户部署多个站点。我将所有网站都放在自己的文件夹中,包括主域。我遇到的问题是,当我用当前代码重写主域地址时,它包含其中的子目录。所以目前,如果我输入 http://www.example.com/url
,它会重写为 https://example.com/folder/url
。我只是想让它在没有文件夹的情况下重写。
任何想法。我知道 运行 我的主域在子目录中,这让事情变得复杂,只是想尽可能地清理托管。
在我的 public_html
.htaccess
文件中:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder/
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ folder/index.php [L]
并在 public_html/folder
.htaccess
:
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/ [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
So currently if I type in
http://www.site.whatever/url
it rewrites tohttps://site.whatever/folder/url
.
这是 "redirect",不是重写。
发生这种情况是因为在 public_html/folder
.htaccess
文件的 HTTP 到 HTTPS 重定向中使用了 REQUEST_URI
服务器变量:
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
REQUEST_URI
服务器变量包含请求的完整 URL 路径,在调用子目录的 .htaccess
文件时,该路径已更新为包含 /folder
.
您需要:
- 将您的规范 www 移动到非 www 并将 HTTP 移动到 HTTPS 重定向到文档根目录中的
.htaccess
文件。 (如果您的public_html/folder
.htaccess
文件中没有其他 mod_rewrite 指令,这会更好。)
或者,
修改上述指令以使用
</code> 反向引用(到捕获的 <code>RewriteRule
模式 ),就像您在之前的 www 到非 www 重定向。例如:RewriteRule (.*) https://%{HTTP_HOST}/ [R=301,L]
(请注意,这最终应该是一个 301 重定向,一旦您确认它工作正常。)
并且不要忘记转义正则表达式中的文字点。