php 在 Wordpress 上启用漂亮的永久链接 - apache 配置不起作用

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

Enabling pretty permalinks on Wordpress - apache configuration does not work

phpwordpressapache.htaccessmod-rewrite

提问by testuser

Probably this error has a pretty easy solution but I've been looking way to long at this and still don't get the error. I think I've tried whatever I could.

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

Problem: when I enable pretty permalinks on my wordpress installation (so, that it is using /%postname%/), it doesn't work. I get a 404 on all pages except for the homepage.

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

This page http://codex.wordpress.org/Permalinkstells me the requirements for permalinks to work:

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

  • Apache web server with the mod_rewrite module installed
  • In WordPress's home directory,
    • The FollowSymLinks option enabled
    • FileInfo directives allowed (e.g. AllowOverride FileInfo or AllowOverride All)
    • An .htaccess file (if this file is missing, WordPress will try to create it when you activate "pretty" permalinks)
    • If you want WordPress to update the .htaccess file automatically, WordPress will need write access to the file.
  • 安装了 mod_rewrite 模块的 Apache Web 服务器
  • 在 WordPress 的主目录中,
    • 已启用 FollowSymLinks 选项
    • 允许使用 FileInfo 指令(例如 AllowOverride FileInfo 或 AllowOverride All)
    • .htaccess 文件(如果此文件丢失,WordPress 将在您激活“漂亮”永久链接时尝试创建它)
    • 如果您希望 WordPress 自动更新 .htaccess 文件,WordPress 将需要对该文件的写访问权限。

Apache web server has been installed, the mod_rewrite module has been loaded with a2enmod rewrite command (and the server has been restarted multiple times after). So, under /etc/apache2/mods-enabled, the symlink to rewrite.load is present. Also, when I run phpinfo command, I see that the mod_rewrite module has been loaded. You can check this as well here: http://namorti.com/phpinfo.php

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

Then, in /etc/apache2/sites-enabled, there was no "default" present. I copied 000-default.conf to default and edited default afterwards. It contains the following: DocumentRoot /var/www

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

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

So as far as I'm concerned, FollowSymLinks has been enabled and FileInfo directives are allowed.

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

As for the last two points, in my wordpress home directory (/var/www), .htaccess is present and writeable by Wordpress (I updated the permalink structure a couple of times and it updates the .htaccess file accordingly). Right now it contains the following:

至于最后两点,在我的 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

So, as far as I know, it SHOULD be working. I restarted the server (service apache2 restart) several times. I don't see what I'm missing. Anyone has a clue here?

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

Thanks in advance!

提前致谢!

* EDIT *

* 编辑 *

So, I did what calcinai told me to do... I edited my /etc/apache2/sites-enabled/default file (containing the vhost). It now looks like this:

所以,我做了 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

I've restarted apache again, but unfortunately it still doesn't work. Honestly it would have surprised me, because moving the directives from the .htaccess file to the vhost would work if the htaccess on itself would work, as everything else seemed correct enough to me...

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

Any other suggestions? Thanks for the effort!

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

采纳答案by testuser

Solved it myself.

自己解决了。

It had to do with the file "000-default.conf" - I copied it to "default" and edited "default", like I already mentioned in my question.

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

Apparently the service is using the file "000-default.conf" on itself. By copying the "default" file back to "000-default.conf" and restarting the apache service, everything works now.

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

One caveat: calcinai's suggestion seems like it should work, but with his suggestion in my vhost in the correct "000-default.conf" file, I got a 403 Forbidden error. When I replaced his content with my original content

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

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

And then once again restarting the apache service, it all worked.

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

Thanks calcinai for your effort to try to help me :)

感谢 calcinai 努力帮助我 :)

回答by calcinai

Make sure the AllowOverridesdirective is set to allin the vhost- that's what tells apache to look for .htaccessfiles in the webroot.

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

A better solution is actually to put your rewrite directives in the vhost, as you clearly have write access to the file. When you have AllowOverrides on, apache will look in the directory and all parent directories for .htaccess files on every request, which can be a big performance hit.

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

For Wordpress, your vhost should look something like:

对于 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>

回答by Thanga Mariappan P

Permalink Problem

固定链接问题

Inside of this file, we want to change all things.

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

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

- should look something like this:

- 应该是这样的:

    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

If you dont see like this then paste the above code inside virtual host

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

When you are finished, save and close the file.

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

Next, we need to enable the rewrite module, which allows you to modify URLs. You can do this by typing:

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

sudo a2enmod rewrite

After you have made these changes, restart Apache:

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

sudo service apache2 restart

回答by Rangel R. Morais

This may help you solve this:

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

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

回答by Stranger

After spending really lots of time, here is what worked successfully in my Linux machine's LAMP server. Following changes are needed for me to make it work perfectly,

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

  1. Add this to your /etc/hostsfile.

    127.0.0.1   sitename.com
    
  2. Add host entry as follows in the path /etc/apache2/sites-available/sitename.com.conf

    <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. Create symlink to sites-enabled.

    sudo ln -s /etc/apache2/sites-available/sitename.com.conf /etc/apache2/sites-enabled/sitename.com.conf
    
  4. Restart apache by using the command,

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

    127.0.0.1   sitename.com
    
  2. 在路径中添加如下主机条目 /etc/apache2/sites-available/sitename.com.conf

    <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
    

This works perfectly for me. Instructions for adding host entry taken from, https://www.digitalocean.com/community/questions/wordpress-permalinks-not-working-on-ubuntu-14-04

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