简体   繁体   English

如何为 Apache 2.2 启用 mod_rewrite

[英]How to enable mod_rewrite for Apache 2.2

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 重写?

In order to use mod_rewrite you 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 .htaccess file.然后,如果您愿意,可以使用以下.htaccess文件。

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

The above .htaccess file (if placed in your DocumentRoot ) will redirect all traffic to an index.php file in the DocumentRoot unless the file exists.上述.htaccess文件(如果放置在您的DocumentRoot )会将所有流量重定向到DocumentRootindex.php文件,除非该文件存在。

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 .htaccess shown above, however, everything else will be redirected to httpdocs/index.php . httpdocs 中存在的任何文件都将使用上面显示的.htaccess提供给请求者,但是,其他所有文件都将重定向到httpdocs/index.php Your application files in includes/app will not be accessible.您在includes/app应用程序文件将无法访问。

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>部分。

Try setting: AllowOverride All .尝试设置: AllowOverride All


Second most common issue is not having mod rewrite enabled: a2enmod rewrite and then restart apache.第二个最常见的问题是没有启用 mod 重写: a2enmod rewrite ,然后重新启动 apache。

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 None to AllowOverride AllAllowOverride None更改为AllowOverride All

this worked for me这对我有用

In Ubuntu:在 Ubuntu 中:

Run:跑步:

a2enmod rewrite

and then:接着:

service apache2 restart

mod_rewrite will now be enabled! mod_rewrite现在将被启用!

New apache version has change in some way.新的 apache 版本在某些方面发生了变化。 If your apache version is 2.4 then you have to go to /etc/apache2/ .如果您的 apache 版本是 2.4,那么您必须转到/etc/apache2/ There will be a file named apache2.conf .会有一个名为apache2.conf的文件。 You have to edit that one(you should have root permission).你必须编辑那个(你应该有 root 权限)。 Change directory text like this像这样更改目录文本

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

Now restart apache.现在重启apache。

service apache2 reload

Hope it works.希望它有效。

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.重新启动您的 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).除了上述之外,如果它不起作用,您还可以更改 apache conf 文件(apache2.conf、http.conf 或 000-default 文件)中的覆盖规则。

  2. Locate "Directory /var/www/"找到“目录/var/www/”

  3. Change the "Override None" to "Override All"将“无覆盖”更改为“全部覆盖”

Problem 3问题三

If you get an error stating rewrite module is not found, then probably your userdir module is not enabled.如果您收到一条错误消息,指出未找到重写模块,则可能是您的 userdir 模块未启用。 For this reason you need to enable it.为此,您需要启用它。

  1. Type this into the console:在控制台中输入:

    sudo a2enmod userdir

  2. Then try enabling the rewrite module if still not enabled (as mentioned above).如果仍未启用,则尝试启用重写模块(如上所述)。

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

Open terminal and typin a2enmod rewrite , It will enable your mod_rewrite module for Apache.打开终端并输入a2enmod rewrite ,它将为 Apache 启用您的mod_rewrite模块。

Then go to /etc/apache2/sites-available and edit default file.然后转到/etc/apache2/sites-available并编辑默认文件。 (For this you must have writable permissions to this file and sites-available folder.) (为此,您必须对此文件和站点可用文件夹具有可写权限。)

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 restart or service apache2 restart现在通过/etc/init.d/apache2 restartservice apache2 restart重启你的 apache

Take clean URL test again and this time it will be passed.再次进行干净的 URL 测试,这次它将通过。

<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.首先,查看您的 Apache 安装目录。 (I'll be assuming you installed it to "C:\\Program Files" here) (我假设你将它安装到“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.查看文件夹:“C:\\Program Files\\Apache Software Foundation\\Apache2.2\\modules”并确保其中有一个名为 mod_rewrite.so 的文件。 (It should be, it's provided as part of the default install. (应该是,它作为默认安装的一部分提供。

Next, open up "C:\\Program Files\\Apache Software Foundation\\Apache2.2\\conf" and open httpd.conf.接下来,打开“C:\\Program Files\\Apache Software Foundation\\Apache2.2\\conf”并打开 httpd.conf。 Make sure the line:确保该行:

#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 文件中的某处。

I just did this我只是做了这个

sudo a2enmod rewrite

then you have to restart the apache service by following command然后你必须通过以下命令重新启动apache服务

sudo service apache2 restart

Old thread, just want to put that don't set AllowOverride to all instead use specific mod you want to use,旧线程,只想说不要将 AllowOverride 设置为 all 而是使用您要使用的特定 mod,

AllowOverride mod_rewrite mod_mime

And this line should be un-commented这条线应该没有注释

LoadModule rewrite_module modules/mod_rewrite.so

Refrences参考资料

Use below command使用下面的命令

sudo a2enmod rewrite

And the restart apache through below command并通过以下命令重启apache

sudo service apache2 restart

What worked for me (in ubuntu):什么对我有用(在 ubuntu 中):

sudo su
cd /etc/apache2/mods-enabled
ln ../mods-available/rewrite.load rewrite.load

Also, as already mentioned, make sure AllowOverride all is set in the relevant section of /etc/apache2/sites-available/default此外,如前所述,确保在/etc/apache2/sites-available/default的相关部分中设置AllowOverride all

The first time I struggled with mod_rewrite rules ignoring my traffic, I learned (frustratingly) that I had placed them in the wrong <VirtualHost> , which meant that my traffic would ignore all of them no matter how well-written they were.我第一次在 mod_rewrite 规则中遇到忽略我的流量的问题时,我(令人沮丧地)了解到我将它们放在错误的<VirtualHost> ,这意味着无论它们写得多么好,我的流量都会忽略所有这些规则。 Make sure this isn't happening to you:确保这不会发生在您身上:

# Change the log location to suit your system. RewriteLog /var/log/apache-rw.log RewriteLogLevel 2

These parameters will activate if you perform a graceful restart of Apache, so you can recycle them in and closely monitor the mod_rewrite behavior.如果您执行 Apache 的正常重启,这些参数将激活,因此您可以回收它们并密切监视 mod_rewrite 行为。 Once your problem is fixed, turn the RewriteLogLevel back down and celebrate.一旦你的问题得到解决,把 RewriteLogLevel 调低并庆祝。

In 100% of my experience, I've found that the RewriteLog has helped me discover the problem with my rewrite rules.根据我 100% 的经验,我发现 RewriteLog 帮助我发现了重写规则的问题。 I can't recommend this enough.我不能推荐这个。 Good luck in your troubleshooting!祝您在故障排除中好运!

Also, this bookmark is your best friend: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritelog此外,此书签是您最好的朋友: http : //httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritelog

显然有不止一种方法可以做到,但我建议使用更标准的方法:

ErrorDocument 404 /index.php?page=404

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

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