如何将网站重定向到特定的文件


How to redirect website to particular file?

有域名。假设是domain.com

当用户只输入domain.com时,他应该被重定向到地址domain.com/file.html。我怎么能做到这一点与.htaccess文件?

另外,RewriteRule ^index'.php$ - [L]是什么意思?对我有帮助吗?

添加到。htaccess文件

DirectoryIndex file.html 

查看这个站点:.htaccess技巧和提示这是一个很好的重写规则参考。

RewriteRule ^index.php$ - [L]是什么意思?它会忽略任何以index。php结尾的重写规则,意思是

要重定向,你可以创建一个索引页(用户访问的第一个页面)

DirectoryIndex file.html

根据这个链接修改你的。htaccess文件

对于RewriteRule,您需要在apache中启用mod_rewrite模块,然后在您的。htaccess文件

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule (.*) file.html [L]
</IfModule>

RewriteRule中,您可以使用正则表达式来识别url并将用户重定向到您想要的文件(在这种情况下,您将重定向到file.html的所有链接)。更多信息在这里。

此外,在apache服务器的默认配置文件中,您可能有以下配置:

#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
    DirectoryIndex index.php index.php4 index.php3 index.cgi index.pl index.html index.htm index.shtml index.phtml
</IfModule>

所以,默认情况下如果你在你的域名。com的webroot中有一个名为index.php的文件总是会首先调用这个文件

尝试将以下内容放入位于domain.com根目录下的。htaccess文件

RewriteEngine On
RewriteBase /
#put all the following rules before any other rules in the .htaccess file
RewriteRule ^file'.html$ - [L]
#domain.com or any subdomain 
RewriteCond %{HTTP_HOST} ^(.+)'.domain'.com$ [NC]
#the root of domain   
RewriteCond %{REQUEST_URI} ^/?$ [NC]
#return file.html
RewriteRule . file.html [L]