首页 文章

如何为Apache 2.2启用mod_rewrite

提问于
浏览
427

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

我没有注释

LoadModule rewrite_module modules/mod_rewrite.s

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

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

我正在使用的所有规则都在处理我的托管,所以它们应该没问题,所以我的问题是,apache配置中是否有任何隐藏的东西,可以阻止mod重写?

16 回答

  • 2

    旧线程,只是想把那个没有设置AllowOverride而不是使用你想要使用的特定mod,

    AllowOverride mod_rewrite mod_mime
    

    这一行应该是未评论的

    LoadModule rewrite_module modules/mod_rewrite.so
    

    Refrences

  • 13

    我在一篇文章中写过:http://www.jarrodoberto.com/articles/2011/11/enabling-mod-rewrite-on-ubuntu

    尝试设置: AllowOverride All .


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

  • 42

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

    ErrorDocument 404 /index.php?page=404
    
  • 3

    在Ubuntu中:

    跑:

    a2enmod rewrite
    

    然后:

    service apache2 restart
    

    mod_rewrite 现在将启用!

  • 25

    新的apache版本在某种程度上有所改变 . 如果您的apache版本是2.4,那么您必须转到 /etc/apache2/ . 将有一个名为 apache2.conf 的文件 . 你必须编辑那个(你应该有root权限) . 像这样更改目录文本

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

    现在重启apache .

    service apache2 reload
    

    希望它有效 .

  • 4

    我第一次无视mod_rewrite规则而忽略了我的流量,我(令人沮丧地)得知我把它们放在了错误的 <VirtualHost> 中,这意味着无论它们写得多好,我的流量都会忽略它们中的 all . 确保没有发生这种情况:

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

    如果执行Apache的正常重启,这些参数将被激活,因此您可以将它们回收并密切监视mod_rewrite行为 . 解决问题后,将RewriteLogLevel重新调低并庆祝 .

    在我100%的经验中,我发现RewriteLog帮助我发现了重写规则的问题 . 我不能推荐这个 . 祝你好运!

    此外,这个书签是你最好的朋友:http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritelog

  • 70

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

    sudo a2enmod rewrite
    

    之后重启apache2

    sudo /etc/init.d/apache2 restart
    

    要么

    sudo service apache2 restart
    

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

    sudo systemctl restart apache2
    

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

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

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

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

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

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

  • 907

    我强烈建议所有想要在.htacces文件中启用mod_rewrite指令的人使用 AllowOverride FileInfo 而不是允许所有内容,因为本页面上的所有答案都表明了这一点 . 很遗憾看到所有这些答案都采取了长期的工作原理's-okay-approach instead of trying to understand the consequences of the proposed '解决方案 . 尝试了解您在服务器上正在做什么,以及如何使用 AllowOverride All 限制您刚刚赠送的权限 . RTFM!,关于这个问题很清楚 . 来吧,伙计,它只是一个网络服务器!

  • 27

    如果不是上述工作尝试编辑/ etc / apache2 / sites-enabled / 000-default

    几乎在顶部你会发现

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

    AllowOverride None 更改为 AllowOverride All

    这对我有用

  • 2

    什么对我有用(在ubuntu中):

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

    另外,如前所述,请确保在 /etc/apache2/sites-available/default 的相关部分中设置 AllowOverride all

  • 217

    <edit>

    刚刚注意到你说mod_rewrite.s而不是mod_rewrite.so - 希望你的问题是一个拼写错误,而不是在httpd.conf文件中! :)

    </edit>

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

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

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

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

    #LoadModule rewrite_module modules/mod_rewrite.so
    

    没有注释:

    LoadModule rewrite_module modules/mod_rewrite.so
    

    此外,如果要在默认情况下启用RewriteEngine,您可能希望添加类似的内容

    <IfModule mod_rewrite>
        RewriteEngine On
    </IfModule>
    

    到你的httpd.conf文件的末尾 .

    如果没有,请确保指定

    RewriteEngine On
    

    你的.htaccess文件中的某个地方 .

  • 16

    对于我的情况,我有

    RewriteEngine On
    

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

    我的问题的解决方案是将我的vhost条目编辑为inlcude

    AllowOverride all
    

    在相关网站的 <Directory> 部分中 .

  • 2

    打开终端和打印 a2enmod rewrite ,它将启用你的 mod_rewrite 用于Apache的模块 .

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

    在下面用现有的第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>
    

    现在通过 /etc/init.d/apache2 restartservice apache2 restart 重启你的apache

    再次进行干净的URL测试,这次它将被传递 .

  • 1

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

    Problem 1

    首先,您的apache没有安装或启用mod_rewrite.c模块可能会出现问题 .

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

    • 打开你的控制台并输入它,这个:

    sudo a2enmod rewrite

    • 重新启动Apache服务器 .

    service apache2 restart

    Problem 2

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

    • 找到“目录/ var / www /”

    • 将“覆盖无”更改为“覆盖全部”

    Problem 3

    如果您收到错误,指出未找到重写模块,那么可能您的userdir模块未启用 . 因此,您需要启用它 .

    • 在控制台中键入:

    sudo a2enmod userdir

    • 然后尝试启用重写模块(如果仍未启用)(如上所述) .

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

  • 2

    我刚刚这样做了

    sudo a2enmod rewrite
    

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

    sudo service apache2 restart
    
  • 77

    使用以下命令

    sudo a2enmod rewrite
    

    并通过以下命令重启apache

    sudo service apache2 restart
    

相关问题