使用 .htaccess 将所有子域和子目录重定向到索引页面
Redirect all subdomains and subdirectories to index page using .htaccess
我的 Detroit iOS & Android Mobile App Development 网站只有一个网页:index.html。
站点的源代码是here。
我想将用户重定向到 thefirstprototype.com,而不是显示 404 错误页面,如果他们试图去其他任何地方或尝试在之后放置任何内容。
例如:
- mail.thefirstprototype.com 将用户带到 thefirstprototype.com
- thefirstprototype.com/mail 将用户带到 thefirstprototype.com
我知道可以使用根文件夹中的 .htaccess
来完成此操作,但我不确定如何操作。有很多教程展示了如何在不同域之间进行操作,但对我的具体情况没有任何帮助。我该怎么做?
谢谢
Edit1:请注意,我没有使用任何 CMS,例如 Wordpress。我只是普通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]
但是,这是否会捕获对 mail.
子域的请求将取决于该子域是否指向与您的主域相同的位置。 (对于 cPanel 共享主机,情况不一定如此。)
仅当您测试了 302(临时)重定向到 301 正常时,才将其更改为 301 - 以避免与 301(永久)重定向相关的潜在缓存问题。
作为额外的好处,您可以将对 index.html
的任何直接请求重定向回根目录。例如,在上述两个规则块之间添加如下:
# Remove "index.html" if requested directly
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.html$ / [R=302,L]
检查 REDIRECT_STATUS
环境变量的条件是确保我们不会得到重定向循环,因为 mod_dir 在内部将请求重写为 index.html
.
我的 Detroit iOS & Android Mobile App Development 网站只有一个网页:index.html。
站点的源代码是here。
我想将用户重定向到 thefirstprototype.com,而不是显示 404 错误页面,如果他们试图去其他任何地方或尝试在之后放置任何内容。
例如:
- mail.thefirstprototype.com 将用户带到 thefirstprototype.com
- thefirstprototype.com/mail 将用户带到 thefirstprototype.com
我知道可以使用根文件夹中的 .htaccess
来完成此操作,但我不确定如何操作。有很多教程展示了如何在不同域之间进行操作,但对我的具体情况没有任何帮助。我该怎么做?
谢谢
Edit1:请注意,我没有使用任何 CMS,例如 Wordpress。我只是普通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]
但是,这是否会捕获对 mail.
子域的请求将取决于该子域是否指向与您的主域相同的位置。 (对于 cPanel 共享主机,情况不一定如此。)
仅当您测试了 302(临时)重定向到 301 正常时,才将其更改为 301 - 以避免与 301(永久)重定向相关的潜在缓存问题。
作为额外的好处,您可以将对 index.html
的任何直接请求重定向回根目录。例如,在上述两个规则块之间添加如下:
# Remove "index.html" if requested directly
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.html$ / [R=302,L]
检查 REDIRECT_STATUS
环境变量的条件是确保我们不会得到重定向循环,因为 mod_dir 在内部将请求重写为 index.html
.