htaccess 文件重定向到另一个文件

htaccess file make redirection to another file

我下面有 htaccess 文件

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ .html

Redirect /index.html /login.html

ErrorDocument 404 /404.html
ErrorDocument 403 /403.html


Options -Indexes

重定向 /index.html /login.html

当用户进入索引页面时,它应该重定向到 login.html 页面。 但是没用。

Redirect /index.html /login.html

im requesting /index

如果您请求 /index,那么我不确定您为什么要在规则中检查 /index.html。但是,您应该使用 mod_rewrite 来构造此重定向,因为您已经在使用 mod_rewrite 进行内部重写。

所以,改成这样:

Options -Indexes

ErrorDocument 404 /404.html
ErrorDocument 403 /403.html

RewriteEngine On
RewriteBase /

# Redirect "/index" to "/login"
RewriteRule ^index$ /login [R=302,L]

# Append ".html" if target file exists
RewriteCond %{DOCUMENT_ROOT}/.html -f
RewriteRule (.*) .html [L]

我还“修复”了您的 .html 重写,因为这可能会导致某些请求出现 500 错误,并且在检查请求之前无需检查请求是否映射到目录确实映射到文件。

请参阅以下有关 ServerFault 的问题,该问题详细介绍了有关 .html 删除的潜在问题。 https://serverfault.com/questions/989333/using-apache-rewrite-rules-in-htaccess-to-remove-html-causing-a-500-error