如何为 Apache 2.2 启用 mod_rewrite

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/869092/
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-09-13 17:36:40  来源:igfitidea点击:

How to enable mod_rewrite for Apache 2.2

apachemod-rewrite

提问by Jakub Arnold

I've got fresh install of Apache 2.2 on my Vista machine, everything works fine, except mod rewrite.

我在我的 Vista 机器上全新安装了 Apache 2.2,一切正常,除了 mod 重写。

I've uncommented

我已取消评论

LoadModule rewrite_module modules/mod_rewrite.s

but none of my rewrite rules works, even simple ones like

但是我的重写规则都不起作用,即使是像这样的简单规则

RewriteRule not_found %{DOCUMENT_ROOT}/index.php?page=404

All the rules I'm using are working on my hosting, so they should be ok, so my question is, is there any hidden thing in apache configuration, that could block mod rewrite?

我使用的所有规则都适用于我的主机,所以它们应该没问题,所以我的问题是,apache 配置中是否有任何隐藏的东西,可能会阻止 mod 重写?

回答by Jonathan Mayhak

In order to use mod_rewriteyou can type the following command in the terminal:

为了使用,mod_rewrite您可以在终端中键入以下命令:

sudo a2enmod rewrite

Restart apache2 after

之后重启apache2

sudo /etc/init.d/apache2 restart

or

或者

sudo service apache2 restart

or as per new unified System Control Way

或按照新的统一系统控制方式

sudo systemctl restart apache2

Then, if you'd like, you can use the following .htaccessfile.

然后,如果您愿意,可以使用以下.htaccess文件。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

The above .htaccessfile (if placed in your DocumentRoot) will redirect all traffic to an index.phpfile in the DocumentRootunless the file exists.

上述.htaccess文件(如果放在您的 中DocumentRoot)会将所有流量重定向到 中的index.php文件,DocumentRoot除非该文件存在。

So, let's say you have the following directory structure and httpdocs is the DocumentRoot

所以,假设您有以下目录结构,httpdocs 是 DocumentRoot

httpdocs/
    .htaccess
    index.php
    images/
        hello.png
    js/
        jquery.js
    css/
        style.css
includes/
    app/
        app.php

Any file that exists in httpdocs will be served to the requester using the .htaccessshown above, however, everything else will be redirected to httpdocs/index.php. Your application files in includes/appwill not be accessible.

httpdocs 中存在的任何文件都将使用.htaccess上面显示的方式提供给请求者,但是,其他所有文件都将重定向到httpdocs/index.php. 您的应用程序文件includes/app将无法访问。

回答by Mike Wazowski

For my situation, I had

对于我的情况,我有

RewriteEngine On

in my .htaccess, along with the module being loaded, and it was not working.

在我的.htaccess,以及正在加载的模块中,它无法正常工作。

The solution to my problem was to edit my vhost entry to inlcude

我的问题的解决方案是编辑我的 vhost 条目以包含

AllowOverride all

in the <Directory>section for the site in question.

在相关<Directory>网站的部分。

回答by Jarrod

Try setting: AllowOverride All.

尝试设置:AllowOverride All



Second most common issue is not having mod rewrite enabled: a2enmod rewriteand then restart apache.

第二个最常见的问题是没有启用 mod 重写:a2enmod rewrite然后重新启动 apache。

回答by Martin Sax

If non of the above works try editing /etc/apache2/sites-enabled/000-default

如果上述方法均无效,请尝试编辑 /etc/apache2/sites-enabled/000-default

almost at the top you will find

几乎在顶部你会发现

<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

Change the AllowOverride Noneto AllowOverride All

更改AllowOverride NoneAllowOverride All

this worked for me

这对我有用

回答by nickleefly

In Ubuntu:

在 Ubuntu 中:

Run:

跑:

a2enmod rewrite

and then:

进而:

service apache2 restart

mod_rewritewill now be enabled!

mod_rewrite现在将启用!

回答by Kripa Jayakumar

There are many ways how you can fix this issue, if you know the root of the issue.

如果您知道问题的根源,有很多方法可以解决此问题。

Problem 1

问题一

Firstly, it may be a problem with your apache not having the mod_rewrite.c module installed or enabled.

首先,这可能是您的 apache 没有安装或启用 mod_rewrite.c 模块的问题。

For this reason, you would have to enable it as follows

因此,您必须按如下方式启用它

  1. Open up your console and type into it, this:

    sudo a2enmod rewrite

  2. Restart your apache server.

    service apache2 restart

  1. 打开你的控制台并输入,这个:

    sudo a2enmod rewrite

  2. 重新启动您的 apache 服务器。

    service apache2 restart

Problem 2

问题二

  1. You may also, in addition to the above, if it does not work, have to change the override rule from the apache conf file (either apache2.conf, http.conf , or 000-default file).

  2. Locate "Directory /var/www/"

  3. Change the "Override None" to "Override All"

  1. 除了上述之外,如果它不起作用,您还可以更改 apache conf 文件(apache2.conf、http.conf 或 000-default 文件)中的覆盖规则。

  2. 找到“目录/var/www/”

  3. 将“无覆盖”更改为“全部覆盖”

Problem 3

问题三

If you get an error stating rewrite module is not found, then probably your userdir module is not enabled. For this reason you need to enable it.

如果您收到一条错误消息,指出未找到重写模块,则可能是您的 userdir 模块未启用。为此,您需要启用它。

  1. Type this into the console:

    sudo a2enmod userdir

  2. Then try enabling the rewrite module if still not enabled (as mentioned above).

  1. 在控制台中输入:

    sudo a2enmod userdir

  2. 如果仍未启用,则尝试启用重写模块(如上所述)。

To read further on this, you can visit this site: http://seventhsoulmountain.blogspot.com/2014/02/wordpress-permalink-ubuntu-problem-solutions.html

要进一步阅读,您可以访问此站点:http: //seventhsoulmountain.blogspot.com/2014/02/wordpress-permalink-ubuntu-problem-solutions.html

回答by Ijas Ahamed N

New apache version has change in some way. If your apache version is 2.4 then you have to go to /etc/apache2/. There will be a file named apache2.conf. You have to edit that one(you should have root permission). Change directory text like this

新的 apache 版本在某些方面发生了变化。如果您的 apache 版本是 2.4,那么您必须转到/etc/apache2/. 将有一个名为apache2.conf. 你必须编辑那个(你应该有 root 权限)。像这样更改目录文本

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

Now restart apache.

现在重启apache。

service apache2 reload

Hope it works.

希望它有效。

回答by Umesh Patil

Open terminal and typin a2enmod rewrite, It will enable your mod_rewritemodule for Apache.

打开终端并打字a2enmod rewrite,它将mod_rewrite为 Apache启用您的模块。

Then go to /etc/apache2/sites-availableand edit default file. (For this you must have writable permissions to this file and sites-available folder.)

然后转到/etc/apache2/sites-available并编辑默认文件。(为此,您必须对此文件和站点可用文件夹具有可写权限。)

Replace below with existing lines 4 to 14

用现有的第 4 到 14 行替换下面

DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>

Now restart your apache by /etc/init.d/apache2 restartor service apache2 restart

现在通过/etc/init.d/apache2 restart或重新启动您的apacheservice apache2 restart

Take clean URL test again and this time it will be passed.

再次进行干净的 URL 测试,这次它将通过。

回答by Danny Battison

<edit>

<编辑>

Just noticed you said mod_rewrite.s instead of mod_rewrite.so - hope that's a typo in your question and not in the httpd.conf file! :)

刚刚注意到您说的是 mod_rewrite.s 而不是 mod_rewrite.so - 希望这是您的问题而不是 httpd.conf 文件中的错字!:)

</edit>

</编辑>

I'm more used to using Apache on Linux, but I had to do this the other day.

我更习惯于在 Linux 上使用 Apache,但前几天我不得不这样做。

First off, take a look in your Apache install directory. (I'll be assuming you installed it to "C:\Program Files" here)

首先,查看您的 Apache 安装目录。(我假设你将它安装到“C:\Program Files”这里)

Take a look in the folder: "C:\Program Files\Apache Software Foundation\Apache2.2\modules" and make sure that there's a file called mod_rewrite.so in there. (It should be, it's provided as part of the default install.

查看文件夹:“C:\Program Files\Apache Software Foundation\Apache2.2\modules”并确保其中有一个名为 mod_rewrite.so 的文件。(应该是,它作为默认安装的一部分提供。

Next, open up "C:\Program Files\Apache Software Foundation\Apache2.2\conf" and open httpd.conf. Make sure the line:

接下来,打开“C:\Program Files\Apache Software Foundation\Apache2.2\conf”并打开 httpd.conf。确保该行:

#LoadModule rewrite_module modules/mod_rewrite.so

is uncommented:

未注释:

LoadModule rewrite_module modules/mod_rewrite.so

Also, if you want to enable the RewriteEngine by default, you might want to add something like

此外,如果您想默认启用 RewriteEngine,您可能需要添加类似

<IfModule mod_rewrite>
    RewriteEngine On
</IfModule>

to the end of your httpd.conf file.

到 httpd.conf 文件的末尾。

If not, make sure you specify

如果没有,请确保您指定

RewriteEngine On

somewhere in your .htaccess file.

在您的 .htaccess 文件中的某处。

回答by user3470929

I just did this

我只是这样做了

sudo a2enmod rewrite

then you have to restart the apache service by following command

然后你必须通过以下命令重新启动apache服务

sudo service apache2 restart