简体   繁体   English

.htaccess 在 amazon ec2 ubuntu 实例中不起作用

[英].htaccess not working in amazon ec2 ubuntu instance

I have a server from amazon's ec2 service running on Linux Ubuntu ( Ubuntu Server 13.04 64 bit) and I have installed apache, php, and mysql.我有一台运行在 Linux Ubuntu(Ubuntu Server 13.04 64 位)上的亚马逊 ec2 服务的服务器,我已经安装了 apache、php 和 mysql。 I have added a .htaccess file in my document root (ie /var/www/).我在我的文档根目录(即 /var/www/)中添加了一个 .htaccess 文件。

Here is the code in .htaccess file as follows:这是 .htaccess 文件中的代码如下:

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

If I remove .php from url like "index1" instead of "index1.php", it returns 404 browser error.如果我从像“index1”而不是“index1.php”这样的 url 中删除 .php,它会返回 404 浏览器错误。 It works properly in my previous server.它在我以前的服务器中正常工作。

I have .htaccess enabled in server.我在服务器中启用了 .htaccess。 I did it using command "sudo vim /etc/apache2/sites-available/default" and changed "AllowOverride None" to "AllowOverride All".我使用命令“sudo vim /etc/apache2/sites-available/default”做到了,并将“AllowOverride None”更改为“AllowOverride All”。

I have also checked .htaccess working by passing invalid value in htaccess file and it returns "Internal server error - 500" in browser.我还通过在 htaccess 文件中传递无效值来检查 .htaccess 是否工作,它在浏览器中返回“内部服务器错误 - 500”。

Here is the link of my server information : http://54.200.58.45/mytest.php这是我的服务器信息的链接:http: http://54.200.58.45/mytest.php

Any help in this regard will be highly appreciated.在这方面的任何帮助将不胜感激。

This is wat worked for me on a fresh EC2 instance with Ubuntu 13.10:这在 Ubuntu 13.10 的新 EC2 实例上对我有用:

  • a2enmod rewrite a2enmod 重写
  • vim /etc/apache2/sites-enabled/000-default.conf vim /etc/apache2/sites-enabled/000-default.conf
  • add the following to the VirtualHost将以下内容添加到 VirtualHost
<Directory "/var/www">
  AllowOverride All
</Directory>
  • service apache2 restart服务 apache2 重启

This happens because the rewrite module doesn't come enabled by default for security reasons.发生这种情况是因为出于安全原因,默认情况下未启用重写模块。

Create a new file called rewrite.conf in /etc/apache2/mods-enabled in the file put this line LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so/etc/apache2/mods-enabled文件中创建一个名为rewrite.conf的新文件把这一行LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

Now reload server sudo service apache2 restart现在重新加载服务器sudo service apache2 restart

This worked for me and hopefully for you, but I don't advice this for production servers.这对我有用,希望对你有用,但我不建议将其用于生产服务器。 This is information for regular Ubuntu users not for a live server.这是针对普通 Ubuntu 用户而不是实时服务器的信息。

The problem is by default in apache installed on Ubuntu doesn't have .htaccess enabled so first you have to manually enable it in a config file and restart apache.问题是默认情况下,Ubuntu 上安装的 apache 没有启用 .htaccess,因此首先您必须在配置文件中手动启用它并重新启动 apache。

Here are the steps.以下是步骤。

1) Login to your EC2 instance with SSH. 1) 使用 SSH 登录到您的 EC2 实例。

ssh -i "Yourkey.pem" ubuntu@yourpublicip ssh -i "Yourkey.pem" ubuntu@yourpublicip

2) Enable Mode Rewrite, this is just incase if it's not enabled. 2) 启用模式重写,这只是在未启用的情况下。

sudo a2enmod rewrite须藤 a2enmod 重写

3) Go to the following directory. 3) 进入以下目录。

cd /etc/apache2/sites-available cd /etc/apache2/sites-available

If you use LS command here you will see the following file.如果您在这里使用 LS 命令,您将看到以下文件。

000-default.conf 000-default.conf

That's the file with default apache configuration which is applied to your sites in /var/www/html folder.这是具有默认 apache 配置的文件,该文件应用于/var/www/html文件夹中的站点。

4) Open this file for editing. 4) 打开此文件进行编辑。

sudo nano 000-default.conf须藤纳米 000-default.conf

5) Add following lines after DocumentRoot /var/www/html line. 5) 在 DocumentRoot /var/www/html 行之后添加以下行。

Options FollowSymLinks AllowOverride All Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all选项 FollowSymLinks AllowOverride All Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all

6) Save the file and restart the apache. 6) 保存文件并重启apache。

sudo service apache2 restart须藤服务 apache2 重启

that's it and now your .htaccess file will work就是这样,现在您的 .htaccess 文件将起作用

您需要先检查是否启用了 mod rewrite 模块。

Tried everything and nothing worked...尝试了一切,但没有任何效果...

2 hours later.. Finally fixed it. 2小时后..终于修好了。

So first the things to rule out.所以首先要排除的事情。

  1. Make sure that you have made the changes in /etc/apache2/apache2.conf and in / etc/apache2/sites-enabled/000-default.conf that everyone is talking about... (AllowOverride All and AccessFileName .htaccess must not be commented out)请确保您已在/etc/apache2/apache2.conf中,并在修改/ etc / apache2的/网站启用/ 000-default.conf,每个人都在谈论...(所有的AllowOverride和AccessFileName的.htaccess不得被注释掉)

  2. Make sure that the Directory /var/www/html is pointing to the right folder.确保目录 /var/www/html 指向正确的文件夹。 Some times it has /html and some times it's just /var/www (however it must not end in / i think, but that might not matter.)有时它有 /html 有时它只是 /var/www (但是它不能以 / 我认为,但这可能无关紧要。)

    1. Finally, and this is what worked for me.最后,这对我有用。 Restart the whole server, not just Apache.重新启动整个服务器,而不仅仅是 Apache。 I figured out that this was my problem, after i tried stopping apache2 (s ervice apache2 stop ) and I was still able to access my website.我发现这是我的问题,在我尝试停止 apache2(服务 apache2 stop )之后我仍然能够访问我的网站。 So I did a full reboot and then all the changes I have been making for the last 2 hours, finally kicked in.所以我完全重新启动,然后我在过去 2 小时内所做的所有更改终于开始了。

Success :)成功 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM