php 如何在 ubuntu 上的 LAMP 中启用 mod_rewrite?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17745310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:21:49  来源:igfitidea点击:

How to enable mod_rewrite in LAMP on ubuntu?

phpmod-rewriteubuntuubuntu-12.04lamp

提问by PHPLover

I'm using Ubuntu 12.04 LTS linux on my machine. I've already installed LAMP on it. Now I want to enable the mod_rewrite module. I did google a lot and tried lots of tricks but couldn't be able to enable mod_rewrite. Can anyone help me to enable the mod_rewrite? Thanks in advance.

我在我的机器上使用 Ubuntu 12.04 LTS linux。我已经在上面安装了 LAMP。现在我想启用 mod_rewrite 模块。我做了很多谷歌并尝试了很多技巧,但无法启用 mod_rewrite。谁能帮我启用mod_rewrite?提前致谢。

回答by mikedugan

TL;DR version -- do the following in your terminal:

TL;DR 版本——在你的终端中执行以下操作:

sudo a2enmod rewrite && sudo service apache2 restart

With explanations -- do the following in your terminal:

有解释——在你的终端中执行以下操作:

ls -l /etc/apache2/mods-available/rewrite.load    ///if it prints out rewrite.load, it's there and ready to go

sudo a2enmod rewrite   //enables the mod

ls -l /etc/apache2/mods-enabled/rewrite.load // shows created symlink

sudo vi /etc/apache2/sites-available/default   //opens the file in vi (you can also use vim or nano)

Replace occurrences of "AllowOverride None" with "AllowOverride all" as necessary

根据需要将出现的“AllowOverride None”替换为“AllowOverride all”

sudo service apache2 restart    ///restarts apache

Edit your virtual host entry in /etc/apache2/sites-available and add AllowOverride Allto the DocumentRoot. Your virtual host should ultimately look something like this:

在 /etc/apache2/sites-available 中编辑您的虚拟主机条目并将其添加AllowOverride All到 DocumentRoot。您的虚拟主机最终应如下所示:

<VirtualHost *:80>
  ServerName example.com
  DocumentRoot /var/www/vhosts/example.com
  <Directory /var/www/vhosts/example.com>
    AllowOverride all
  </Directory>
</VirtualHost>

Although this isn't suitable for production environments, it works just fine for local development.

虽然这不适合生产环境,但它适用于本地开发。

回答by jmarceli

You didn't mention what commands did you try, so I will start with the basic one:

您没有提到您尝试了哪些命令,所以我将从基本的命令开始:

sudo a2enmod rewrite

You can also check if mod rewrite is already enable using:

您还可以使用以下命令检查 mod 重写是否已启用:

apache2ctl -M