在Wordpress上启用相当永久链接 - apache配置不起作用

tes*_*ser 8 php apache wordpress .htaccess mod-rewrite

可能这个错误有一个非常简单的解决方案,但我一直在寻找方法来解决这个问题,但仍然没有得到错误.我想我尽我所能.

问题:当我在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>
Run Code Online (Sandbox Code Playgroud)

因此,就我而言,已启用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
Run Code Online (Sandbox Code Playgroud)

所以,据我所知,它应该工作.我重启了服务器(service apache2 restart)几次.我不明白我错过了什么.有人在这里有线索吗?

提前致谢!

*编辑*

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

<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
Run Code Online (Sandbox Code Playgroud)

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

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

cal*_*nai 12

确保AllowOverrides指令allvhost中设置为- 这就是告诉apache .htaccess在webroot中查找文件的内容.

更好的解决方案实际上是将重写指令放在vhost中,因为您显然拥有该文件的写入权限.当你打开AllowOverrides时,apache会在每个请求中查看目录和所有父目录中的.htaccess文件,这可能是一个很大的性能损失.

对于Wordpress,您的vhost应该类似于:

<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>
Run Code Online (Sandbox Code Playgroud)


tes*_*ser 5

自己解决了.

它与文件"000-default.conf"有关 - 我将其复制到"默认"并编辑为"默认",就像我在我的问题中已经提到过的那样.

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

一个警告:calcinai的建议似乎应该可行,但是在我的vhost中他在正确的"000-default.conf"文件中的建议,我得到了403 Forbidden错误.当我用原始内容替换他的内容时

DocumentRoot /var/www
<Directory />
        Options FollowSymLinks Indexes
        AllowOverride FileInfo
</Directory>
Run Code Online (Sandbox Code Playgroud)

然后再次重新启动apache服务,一切正常.

感谢calcinai努力帮助我:)


小智 5

固定链接问题

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

sudo nano /etc/apache2/sites-available/000-default.conf
000-default.conf 
Run Code Online (Sandbox Code Playgroud)

- 应该是这样的:

    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
Run Code Online (Sandbox Code Playgroud)

如果你没有看到这样,那么将上面的代码粘贴到虚拟主机中

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

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

sudo a2enmod rewrite
Run Code Online (Sandbox Code Playgroud)

进行这些更改后,重新启动 Apache:

sudo service apache2 restart
Run Code Online (Sandbox Code Playgroud)