感谢任何能花点时间看一看的人。
最近我创建了一个新的部分";子域";在我的网站和这个新文件夹中,我包括一个Joomla CMS安装,网址如下:http://www.example.com/subdomain/
在这个文件夹中,我添加了一个htaccess文件。
## No directory listings
# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
当我尝试访问sayhttp://example.com/subdomain/anytrailingstring那么它不会将我重定向到http://www.example.com/subdomain/anytrailingstring正如我所料,它正在重定向到http://www.example.com/anytrailingstring去掉了/子域/,这当然是一个不存在的页面,因此是一个404。
这是个问题。
我在root.htaces文件中没有任何指令,除了这个:
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
有人能理解为什么子域名htaccess没有正确重定向到吗?我错过什么了吗?
我一点都不擅长htaccess,如果有人能帮助我,我会非常感激的。谢谢
RewriteCond %{HTTP_HOST} !^www. [NC] RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
您需要使用REQUEST_URI
服务器变量,而不是backreference($1
)。RewriteRule
模式(第一个参数)匹配的URL路径是相对于当前目录的,因此不包括父子目录(即示例中的/subdomain
)。
改为这样做:
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
您需要清除浏览器缓存,因为错误的(301-永久)重定向将被浏览器缓存。使用302(临时)重定向进行测试,以避免潜在的缓存问题。
然而,有几个问题:
- 为什么不使用HTTPS?(您正在重定向到父
.htaccess
文件中的HTTPS,但这现在被子目录中的mod_rewrite指令覆盖。) - 为什么不将其包含在父
.htaccess
文件中
更新:因此,考虑到以上几点。。。如果您想将此规则移动到根目录中的父.htaccess
文件,则如下所示:
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine on
# Redirect non-www to www (and HTTPS)
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
指令的顺序是确保最多只有一个重定向(假设您没有实现HSTS)。
您不必要地复制了RewriteEngine
指令(所以我删除了第二个实例)。
未使用RewriteBase
指令。
HTTP到HTTPS规则中的捕获子组不是必需的。即在这种情况下^
比^(.*)$
好。
旁白:
。。。一个新的部分";子域";在我的网站和这个新文件夹中,我包含了一个Joomla CMS安装,url看起来像这样:
http://www.example.com/subdomain/
这是子目录,而不是";子域";。
这是一个";子域":
http://subdomain.example.com/