Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用.htaccess将所有子域和子目录重定向到索引页_.htaccess_Redirect_Http Status Code 301_Http Redirect - Fatal编程技术网

使用.htaccess将所有子域和子目录重定向到索引页

使用.htaccess将所有子域和子目录重定向到索引页,.htaccess,redirect,http-status-code-301,http-redirect,.htaccess,Redirect,Http Status Code 301,Http Redirect,我的网站只有一个网页:index.html 该网站的源代码是 我不想显示404错误页面,而是想将用户重定向到firstprototype.com,如果他们试图转到其他任何地方或试图在后面添加任何内容 例如: mail.thefirstprototype.com将用户带到firstprototype.com firstprototype.com/mail将用户带到firstprototype.com 我知道可以使用根文件夹中的.htaccess来执行此操作,但我不确定如何执行。有很多教程展示了

我的网站只有一个网页:index.html

该网站的源代码是

我不想显示404错误页面,而是想将用户重定向到firstprototype.com,如果他们试图转到其他任何地方或试图在后面添加任何内容

例如:

  • mail.thefirstprototype.com将用户带到firstprototype.com
  • firstprototype.com/mail将用户带到firstprototype.com
我知道可以使用根文件夹中的
.htaccess
来执行此操作,但我不确定如何执行。有很多教程展示了如何在不同的域之间进行此操作,但没有针对我的具体情况。我该怎么做

谢谢

编辑1:请注意,我没有使用像Wordpress这样的CMS。我只是通过普通FTP将静态HTML、CSS、JS网页推送到托管服务器

尝试以下操作:

DirectoryIndex index.html

RewriteEngine On

# Redirect non-canonical hostnames (eg. mail)
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^ http://example.com/ [R=302,L]

# Redirect 404 to root
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . / [R=302,L]
但是,这是否捕获对
邮件的请求。
子域将取决于该子域是否指向与主域相同的位置。(对于cPanel共享主机,情况未必如此。)

只有在测试了302(临时)重定向是否正常后,才将其更改为301-以避免与301(永久)重定向相关的潜在缓存问题

作为额外的好处,您可以将对
index.html
的任何直接请求重定向回根目录。例如,在上述两个规则块之间添加以下内容:

# Remove "index.html" if requested directly
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.html$ / [R=302,L]

检查
REDIRECT\u STATUS
环境变量的条件是确保我们不会得到重定向循环,因为mod\u dir在内部将请求重写为
index.html

我更新了答案,只是为了添加一个优化(防止mod\u dir重写URL后进行不必要的文件系统检查)为了稍微整理一下代码(您不必在后续规则的替换字符串中指定绝对URL,因为这在第一条规则中是选中的。)只是为了添加,如果您打算在以后添加更多页面,最好将“重定向404到根目录”保留为302(临时)重定向,因为浏览器会持久缓存301s。