【问题标题】:Enabling pretty permalinks on Wordpress - apache configuration does not work在 Wordpress 上启用漂亮的永久链接 - apache 配置不起作用
【发布时间】:2013-09-17 11:03:26
【问题描述】:

可能这个错误有一个非常简单的解决方案,但我一直在寻找这种方法,但仍然没有得到错误。我想我已经尽力了。

问题:当我在我的 wordpress 安装中启用漂亮的永久链接时(因此,它使用 /%postname%/),它不起作用。我在除主页之外的所有页面上都收到 404。

此页面http://codex.wordpress.org/Permalinks 告诉我永久链接工作的要求:

  • 安装了 mod_rewrite 模块的 Apache Web 服务器
  • 在 WordPress 的主目录中,
    • FollowSymLinks 选项已启用
    • 允许使用 FileInfo 指令(例如 AllowOverride FileInfo 或 AllowOverride All)
    • .htaccess 文件(如果此文件丢失,WordPress 将在您激活“漂亮”永久链接时尝试创建它)
    • 如果您希望 WordPress 自动更新 .htaccess 文件,WordPress 将需要对该文件的写入权限。

Apache web 服务器已经安装,mod_rewrite 模块已经加载了 a2enmod rewrite 命令(并且服务器已经多次重新启动)。因此,在 /etc/apache2/mods-enabled 下,存在 rewrite.load 的符号链接。此外,当我运行 phpinfo 命令时,我看到 mod_rewrite 模块已加载。你也可以在这里查看:http://namorti.com/phpinfo.php

然后,在 /etc/apache2/sites-enabled 中,没有“默认”存在。我将 000-default.conf 复制到默认值,然后编辑了默认值。它包含以下内容: DocumentRoot /var/www

    <Directory />
            Options FollowSymLinks Indexes
            AllowOverride FileInfo
    </Directory>

就我而言,FollowSymLinks 已启用,并且允许使用 FileInfo 指令。

至于最后两点,在我的 wordpress 主目录 (/var/www) 中,.htaccess 存在并且可由 Wordpress 写入(我更新了永久链接结构几次,它相应地更新了 .htaccess 文件)。现在它包含以下内容:

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

# END WordPress

所以,据我所知,它应该可以工作。我重新启动了服务器(服务 apache2 重新启动)几次。我没有看到我错过了什么。有人在这里有线索吗?

提前致谢!

* 编辑 *

所以,我做了 calcinai 告诉我的事情……我编辑了我的 /etc/apache2/sites-enabled/default 文件(包含虚拟主机)。现在看起来像这样:

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin www.namorti.com
    DocumentRoot /var/www

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

            RewriteEngine On
            RewriteBase /
            RewriteRule ^index\.php$ - [L]
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule . /index.php [L]
    </Directory>
    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

我已经重新启动了 apache,但不幸的是它仍然无法正常工作。老实说,这会让我感到惊讶,因为如果 htaccess 本身可以工作,将指令从 .htaccess 文件移动到 vhost 就可以工作,因为其他一切对我来说似乎都足够正确......

还有其他建议吗?感谢您的努力!

【问题讨论】:

    标签: php wordpress apache .htaccess mod-rewrite


    【解决方案1】:

    确保在 vhost 中将 AllowOverrides 指令设置为 all - 这就是告诉 apache 在 webroot 中查找 .htaccess 文件的原因。

    更好的解决方案实际上是将您的重写指令放在虚拟主机中,因为您显然对文件具有写入权限。当您启用 AllowOverrides 时,apache 将在每个请求的目录和所有父目录中查找 .htaccess 文件,这可能会对性能造成很大影响。

    对于 Wordpress,您的虚拟主机应如下所示:

    <VirtualHost *:80>
        ServerName example.com
        DocumentRoot /var/www/site
    
        <Directory /var/www/site> 
            Options Indexes FollowSymLinks
            AllowOverride None 
            Order allow,deny 
            allow from all 
    
            RewriteEngine On
            RewriteBase /
            RewriteRule ^index\.php$ - [L]
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule . /index.php [L]
        </Directory> 
    </VirtualHost>
    

    【讨论】:

    • 不幸的是它没有用。查看问题,我对其进行了编辑以显示我的虚拟主机现在的样子。
    • 解决了。它与 000-default.conf 文件有关 - 我将它复制到默认值并编辑了默认值;但显然该服务本身使用文件 000-default.conf 。另外:您的建议不起作用,在我的 000-default.conf 中的虚拟主机中,我收到了 403 Forbidden 错误。使用建议的 Wordpress 内容(我最初默认的内容),这一切都像一个魅力。感谢您的帮助!
    • 这可能只是因为省略了 FollowSymLinks 指令。对不起!
    • 完全正确,我有点傻,不检查日志或看不到它只是FollowSymLinks。现在它就像一个魅力,所以要保持这样,因为它更有效。谢谢!!
    • 酷。我将 Indexes 指令从您的原始选项中保留在那里,但如果您实际上没有使用索引,我建议您将其删除,因为有几个 Wordpress 插件在打开时容易受到攻击。
    【解决方案2】:

    自己解决了。

    这与文件“000-default.conf”有关 - 我将其复制到“默认”并编辑了“默认”,就像我在问题中已经提到的那样。

    显然该服务正在使用文件“000-default.conf”。通过将“默认”文件复制回“000-default.conf”并重新启动 apache 服务,现在一切正常。

    一个警告:calcinai 的建议似乎应该可行,但是根据他在我的虚拟主机中正确的“000-default.conf”文件中的建议,我收到了 403 Forbidden 错误。当我用我的原始内容替换他的内容时

    DocumentRoot /var/www
    <Directory />
            Options FollowSymLinks Indexes
            AllowOverride FileInfo
    </Directory>
    

    然后再次重启apache服务,一切正常。

    感谢 calcinai 努力帮助我 :)

    【讨论】:

    • 对此感到抱歉 - 我只是没有正确复制您的 Options 指令。我几乎敢打赌,如果您在获得 403 时查看 apache 日志,它会告诉您添加 FollowSymLinks
    • 记住 sudo a2enmod rewrite
    【解决方案3】:

    这可能会帮助您解决这个问题:

    sudo chown -R www-data:www-data /var/www
    

    【讨论】:

      【解决方案4】:

      花了很多时间之后,这就是在我的 Linux 机器的 LAMP 服务器上成功运行的方法。我需要进行以下更改才能使其完美运行,

      1. 将此添加到您的 /etc/hosts 文件中。

        127.0.0.1   sitename.com
        
      2. /etc/apache2/sites-available/sitename.com.conf路径下添加host入口如下

        <VirtualHost *:80>
            ServerAdmin webmaster@localhost
            ServerName sitename.com
        
            DocumentRoot /var/www/sitename.com/public_html
            <Directory />
                Options FollowSymLinks
                AllowOverride None
            </Directory>
            <Directory /var/www/sitename.com/public_html>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
            </Directory>
        
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
        </VirtualHost>
        
      3. 创建指向已启用站点的符号链接。

        sudo ln -s /etc/apache2/sites-available/sitename.com.conf /etc/apache2/sites-enabled/sitename.com.conf
        
      4. 使用命令重启apache,

        sudo service apache2 restart
        

      这对我来说非常有效。添加主机条目的说明来自https://www.digitalocean.com/community/questions/wordpress-permalinks-not-working-on-ubuntu-14-04

      【讨论】:

        【解决方案5】:

        固定链接问题

        在这个文件里面,我们想要改变所有的东西。

        sudo nano /etc/apache2/sites-available/000-default.conf
        000-default.conf 
        

        - 应该是这样的:

            ServerAdmin webmaster@localhost
            DocumentRoot /var/www/html
            <Directory /var/www/html/>
                Options Indexes FollowSymLinks
                AllowOverride All
                Require all granted
            </Directory>
        
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
        

        如果您没有看到这样的内容,请将上面的代码粘贴到虚拟主机中

        完成后,保存并关闭文件。

        接下来,我们需要启用重写模块,它允许您修改 URL。您可以通过键入:

        sudo a2enmod rewrite
        

        完成这些更改后,重新启动 Apache:

        sudo service apache2 restart
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-03-26
          • 2018-04-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-16
          • 2015-04-28
          • 2016-12-07
          相关资源
          最近更新 更多