无论我做什么,.htaccess 都无法完成工作
Whatever I do, .htaccess won't do the job
使用 .htacess
文件对我来说一直是非常令人沮丧的经历。有人请帮忙。
这就是我想要实现的:
- 我是运行宁Ubuntu14.04.
- 将我的整个网站
(example.com)
重定向到 maintenance.html
页面。
- 屏蔽除一个IP以外的所有其他人,例如,我只需要允许
123.456.789.0
这是我的文件:
- 我的
index.html
的位置是 /var/www/html
- 我的
maintenance.html
的位置是 /var/www/html
- 我的
.htaccess
文件的位置是 /var/www/html
我的 .htaccess
文件的内容:
#Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com[nc]
RewriteRule ^(.*)$ http://www.example.com/ [r=301,nc]
#301 Redirect Old File
Redirect 301 /index.html /maintenance.html
#Block users by IP
order allow,deny
deny from all
allow from 123.456.789.0
请帮助我理解:
- 以上每个文件的位置对吗?在什么情况下,
页面以
500 internal server error
结束?
- 我应该做哪些改变
/etc/apache2/apache.conf
/etc/apache2/sites-enabled/000-default.conf
或
/etc/apache2/sites-available/000-default.conf
- 是否有必要 运行
a2enmod rewrite
?
- 我应该在上述任何
config
文件中添加 <IfModule mod_rewrite.c>
和 </IfModule>
作为页眉和页脚吗?
抱歉问题太多,但我真的很想一直知道。
提前致谢。
Is the location of each of the above files right? In what cases, the
page ends up in 500 internal server error
?
“500 Internal Server Error”消息表示出现错误,您需要检查服务器日志以了解确切的详细信息。 Apache 不会显示错误信息给大家看。
What changes should I make
这取决于问题是什么。如果问题是“500 Internal Server Error”,那意味着我们还不知道问题出在哪里。
Is is necessary to run a2enmod rewrite?
该命令启用 mod_rewrite
。如果未启用,则需要启用它。如果已启用,则无需启用它。
值得注意的是,这个命令不是官方 Apache 发行版的一部分。一些 Linux 发行版(即 Debian 和衍生产品)会更改第三方软件包以匹配其配置首选项,例如本例。
Should I add <IfModule mod_rewrite.c>
and </IfModule>
as header
and footer in any of the above config files?
正如 documentation 所解释的,当给定模块未安装时,此块可用于忽略指令。这对于要分发的配置模板和可选功能很有用。在您的情况下,如果 mod_rewrite 不可用,它会默默地忽略您的代码——您不希望这样。
最后但同样重要的是:
order allow,deny
deny from all
allow from 123.456.789.0
... 属于旧的(而且真的很难理解)Apache/2.2 语法。如果您使用 Apache/2.4*,您可能想尝试 Require.
(*) 有些发行版讨厌捆绑最近的软件,但 2.4 已经存在好几年了
感谢@OlafDietsche 和@ÁlvaroGonzález 提供的快速帮助。我将他们的建议保留在这里,以便像我这样的人会发现它有用。
问题出在我的目标上,而不是语法上。通过他们的评论和回答,我开始知道我的两个目标是相互矛盾的。
我配置 .htaccess
进行页面重定向和 IP 阻止。但如果我阻止(几乎)所有人访问该网站,页面重定向就没有意义。
.htaccess
中要求的配置是:
#Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com[nc]
RewriteRule ^(.*)$ http://www.example.com/ [r=301,nc]
#301 Redirect Old File
Redirect 301 /index.html /maintenance.html
使用 .htacess
文件对我来说一直是非常令人沮丧的经历。有人请帮忙。
这就是我想要实现的:
- 我是运行宁Ubuntu14.04.
- 将我的整个网站
(example.com)
重定向到maintenance.html
页面。 - 屏蔽除一个IP以外的所有其他人,例如,我只需要允许
123.456.789.0
这是我的文件:
- 我的
index.html
的位置是/var/www/html
- 我的
maintenance.html
的位置是/var/www/html
- 我的
.htaccess
文件的位置是/var/www/html
我的 .htaccess
文件的内容:
#Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com[nc]
RewriteRule ^(.*)$ http://www.example.com/ [r=301,nc]
#301 Redirect Old File
Redirect 301 /index.html /maintenance.html
#Block users by IP
order allow,deny
deny from all
allow from 123.456.789.0
请帮助我理解:
- 以上每个文件的位置对吗?在什么情况下,
页面以
500 internal server error
结束? - 我应该做哪些改变
/etc/apache2/apache.conf
/etc/apache2/sites-enabled/000-default.conf
或/etc/apache2/sites-available/000-default.conf
- 是否有必要 运行
a2enmod rewrite
? - 我应该在上述任何
config
文件中添加<IfModule mod_rewrite.c>
和</IfModule>
作为页眉和页脚吗?
抱歉问题太多,但我真的很想一直知道。 提前致谢。
Is the location of each of the above files right? In what cases, the page ends up in
500 internal server error
?
“500 Internal Server Error”消息表示出现错误,您需要检查服务器日志以了解确切的详细信息。 Apache 不会显示错误信息给大家看。
What changes should I make
这取决于问题是什么。如果问题是“500 Internal Server Error”,那意味着我们还不知道问题出在哪里。
Is is necessary to run a2enmod rewrite?
该命令启用 mod_rewrite
。如果未启用,则需要启用它。如果已启用,则无需启用它。
值得注意的是,这个命令不是官方 Apache 发行版的一部分。一些 Linux 发行版(即 Debian 和衍生产品)会更改第三方软件包以匹配其配置首选项,例如本例。
Should I add
<IfModule mod_rewrite.c>
and</IfModule>
as header and footer in any of the above config files?
正如 documentation 所解释的,当给定模块未安装时,此块可用于忽略指令。这对于要分发的配置模板和可选功能很有用。在您的情况下,如果 mod_rewrite 不可用,它会默默地忽略您的代码——您不希望这样。
最后但同样重要的是:
order allow,deny
deny from all
allow from 123.456.789.0
... 属于旧的(而且真的很难理解)Apache/2.2 语法。如果您使用 Apache/2.4*,您可能想尝试 Require.
(*) 有些发行版讨厌捆绑最近的软件,但 2.4 已经存在好几年了
感谢@OlafDietsche 和@ÁlvaroGonzález 提供的快速帮助。我将他们的建议保留在这里,以便像我这样的人会发现它有用。
问题出在我的目标上,而不是语法上。通过他们的评论和回答,我开始知道我的两个目标是相互矛盾的。
我配置 .htaccess
进行页面重定向和 IP 阻止。但如果我阻止(几乎)所有人访问该网站,页面重定向就没有意义。
.htaccess
中要求的配置是:
#Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com[nc]
RewriteRule ^(.*)$ http://www.example.com/ [r=301,nc]
#301 Redirect Old File
Redirect 301 /index.html /maintenance.html