简体   繁体   English

如何在运行ubuntu的amazon ec2中启用.htaccess

[英]How to enable .htaccess in amazon ec2 running ubuntu

I have an instance running ubuntu in ec2. 我有一个在ec2中运行ubuntu的实例。 I have this .htaccess file :- 我有这个.htaccess文件: -

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

This file is located inside the var/www/html folder. 该文件位于var/www/html文件夹中。 I am aiming to achieve loading of amazon-public-dns.com/index.php as amazon-public-dns.com/index . 我的目标是将amazon-public-dns.com/index.php加载为amazon-public-dns.com/index

Now, I have tried these steps :- 现在,我尝试了以下步骤: -
1) Creating the rewrite.conf in /etc/apache2/mods-enabled and in the file putting the line LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so . 1)在/etc/apache2/mods-enabled创建rewrite.conf ,并在文件中放入LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so ( Refer this answer ) 参考这个答案
2) Running the command apache2 enable module rewrite . 2)运行命令apache2 enable module rewrite Also, in the file /etc/apache2/sites-available/000-default.conf and writing this in the end :- 另外,在文件/etc/apache2/sites-available/000-default.conf并将其写入最后: -

 <Directory /var/www/html>
        Options Indexes FollowSymLinks MultiViews
        # changed from None to FileInfo
        AllowOverride All
        Order allow,deny
        allow from all
 </Directory>

Note that this answer told to edit the default . 请注意, 此答案告诉您编辑default But, since that file was not there in my case, I edited the 000-default.conf file. 但是,由于我的文件不存在,我编辑了000-default.conf文件。
I restarted apache as told. 据我所说,我重启了apache。 But the link amazon-public-dns.com/index gives me a 404 :( Please help me. 但链接amazon-public-dns.com/index给了我404 :(请帮助我。

EDIT : I put some random junk in the htaccess file. 编辑 :我在htaccess文件中放了一些随机垃圾。 But the index.php is not giving any 500 internal server error which means that the htaccess file is being ignored. 但是index.php没有给出任何500内部服务器错误,这意味着忽略了htaccess文件。 Now in 现在进来

https://help.ubuntu.com/community/EnablingUseOfApacheHtaccessFiles https://help.ubuntu.com/community/EnablingUseOfApacheHtaccessFiles

, they say to enable htaccess edit this file - /etc/apache2/sites-available/default . ,他们说要启用htaccess编辑这个文件 - /etc/apache2/sites-available/default BUT THERE IS NO SUCH FILE 但是没有这样的文件

The default conf file /etc/apache2/sites-available/000-default.conf only work if its having a symlink in sites-enabled folder. 默认配置文件/etc/apache2/sites-available/000-default.conf仅在其启用了站点的文件夹中具有符号链接时才起作用。

You have done all right steps. 你已经做了所有正确的步骤。 Its may be problem of sysmlink of sites-available folder. 它可能是站点可用文件夹的sysmlink问题。 You can either create a symlink or update 000-default.conf with AllowOverride None to AllowOverride All inside file /etc/apache2/sites-enabled/000-default.conf 您可以创建一个符号链接,也可以使用AllowOverride None更新000-default.conf更新为AllowOverride All内部文件/etc/apache2/sites-enabled/000-default.conf

following command works for me Great ! 以下命令对我有用!

sudo a2enmod rewrite
sudo service apache2 restart

To check loaded modules 检查加载的模块

sudo apache2ctl -M

Today I faced this exact same problem on an ubuntu EC2 instance. 今天我在ubuntu EC2实例上遇到了同样的问题。

This worked for me: 这对我有用:

Place your .htaccess configuration inside the <IfModule ..> directive: .htaccess配置放在<IfModule ..>指令中:

<IfModule mod_rewrite.c>
  RewriteEngine on
  # your config here
</IfModule>

Not sure if /var/www/html is actually taken into account by the server. 不确定服务器是否实际考虑了/var/www/html Try changing this: 尝试改变这个:

<Directory /var/www/html>
    # your current implementation
    AllowOverride All
</Directory>

for this one: 对于这一个:

<Directory /var/www>
    # your current implementation
    AllowOverride All
</Directory>

This directive should already by there in your /etc/apache2/apache2.conf file. 该指令应该已经存在于/etc/apache2/apache2.conf文件中。

If that does not work try to comment out your configuration, particularly the part that has the allow,deny and allow from all 如果这不起作用,请尝试注释掉您的配置,特别是具有allow,denyallow from all

<Directory /var/www>
    # your current implementation
    AllowOverride All
    # Order allow,deny # try to comment this one out 
    # allow from all # also this one
</Directory>

Last but not least, check that the rewrite a2enmod module is loaded as suggested by @kamal-kumar and do not forget to restart you apache. 最后但并非最不重要的是,检查是否按照@ kamal-kumar的建议加载了重写a2enmod模块,并且不要忘记重新启动apache。

This worked for me, hope this helps someone else :) 这对我有用,希望这可以帮助别人:)

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

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