【问题标题】:Remove index.php from Wordpress URLs从 Wordpress URL 中删除 index.php
【发布时间】:2018-07-20 14:02:02
【问题描述】:

我是 Wordpress 的新手,但尝试了所有文档和 Stack Overflow 帖子以寻找可能的解决方案,但到目前为止都没有奏效。

我已验证 mod 重写已启用并按预期工作。遵循此处提到的所有步骤。 https://wordpress.stackexchange.com/questions/105795/remove-index-php-from-permalinks

还重新启动了 Apache 几次,但当我从永久链接设置中删除 index.php 路径时仍然出现 404 错误。

将 wordpress 目录重命名为 blog 以访问位于 www.xyz.com/blog 的站点。现在的要求是访问任何博客文章 www.xyz.com/blog/2018/02/09/my-wp-post URL 中没有 index.php。

除了 WP 之外,我们没有任何其他 CMS 内容用于博客网站。

固定链接设置(自定义结构): /index.php/%year%/%monthnum%/%day%/%postname%/

.htaccess 文件内容:

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

【问题讨论】:

  • 你的问题是 Too Broad 。通过添加永久链接结构来编辑您的问题,现在您的 url 的外观是什么,您想要实现什么,您的 wordpress 安装是否包含其他一些 cms/frameworks/static html,它是否位于子文件夹中,提供 .htaccess 文件和等
  • 请查看编辑并让我知道是否需要任何其他详细信息。

标签: wordpress apache .htaccess mod-rewrite permalinks


【解决方案1】:

我花了很多时间尝试在这里或其他地方回答的许多不同方法,但没有一个能解决我的问题。

这就是我所做的,它解决了我的问题。

  1. 更改永久链接设置以删除 index.php
  2. 如果在 WP 管理面板中更改永久链接设置时不允许自动更新,则更新/保存位于 WP 安装根目录的 .htaccess 内容。
  3. 这是重要的一步,因为除了 wordpress 文档here 之外,没有多少问答详细提到这一点。具体检查 AllowOverride 设置并将其更改为 All,如果没有此更改,Apache httpd.conf 将不会加载 .htaccess 内容。当然,如果尚未启用 mod_rewrite,则必须在服务器中启用它。在我们的例子中,它是默认启用的,所以不必搞砸这一步。
  4. 还要确保按照 WP 文档中的说明启用 FollowSymLinks 选项。
  5. 最后但同样重要的是,确保您重新启动 Apache 服务/服务器以使更改生效。

进入httpd.conf文件:

<Directory "/var/www/html/blog">
Options FollowSymLinks
AllowOverride All

【讨论】:

    【解决方案2】:

    假设 Ubuntu 16.04 和 Apache2

    1) 激活 mod_rewrite。它可用,但未通过干净的 Apache 2 安装启用。

    sudo a2enmod rewrite
    

    2) 重启 Apache

    sudo systemctl restart apache2.service
    

    3) 设置 .htaccess

    (注意:Apache 建议使用服务器配置文件而不是在 .htaccess 中插入规则,但是,对于本示例,将规则插入到 .htaccess 中就足够了,因为性能损失可以忽略不计。)

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

    在 000-default.conf 中插入以下内容

    <VirtualHost *:80>
        <Directory /var/www/html>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Require all granted
        </Directory>
    </VirtualHost>
    

    4) 重启 Apache

    重复第 2 步

    5) 在网络根目录中创建 .htaccess。

    touch /var/www/html/.htaccess
    

    将以下内容插入 .htaccess

    RewriteEngine On
    

    6 配置URL重写 (注:假设index.php)

    sudo nano /var/www/html/.htaccess
    
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    

    mod_rewrite 是一个有用的 Apache 模块,可以有效地用于确保人类可读的 URL 和良好的 SEO 结果。如果您想了解有关 mod_rewrite 的更多信息,请查看 Apache 的 mod_rewrite Introduction 和 Apache 的 official documentation for mod_rewrite

    【讨论】:

      【解决方案3】:

      简而言之,正如@Cnu 所提到的,互联网上很少有资源涉及的问题是,您的 apache 配置必须包含 AllowOverride FileInfo 指令,以便 wordpress 重写才能在没有 /index.php/ 的情况下工作。

      自 Apache 2.4 起,AllowOverride 默认设置为“无”,这通常是使“漂亮网址”工作的障碍(在这种情况下放弃 index.php)。正如@cnu 提到的,你应该仔细阅读这个资源:https://wordpress.org/support/article/using-permalinks/

      1. 确保启用了 mod_rewrite ->(确保创建一个 info.php,稍后您将删除包含行 &lt;?php phpinfo();?&gt;)在您博客的根目录并调用 https://domainofyourb.log/info.php
      2. 确保您的 .htaccess 可由 wordpress 写入。 (权限和所有权应该允许您的网络服务器(通常使用用户名“apache”)编辑文件。
      3. 更改您的 wordpress 永久链接设置,并检查 .htaccess 文件是否正确写入。
      4. 检查您的 apache 配置(某些 linux 发行版中的 etc/httpd/conf/httpd.conf)是否在您博客的 &lt;Directory&gt;&lt;/Directory&gt; 部分中包含指令 AllowOverride FileInfo
      5. FollowSymLinks 的 Options 指令应该是默认值,但如果提到 Options 指令,请添加 FollowSymLinks 以获得更好的效果。
      6. 完成所有操作后,不要忘记重新启动 Apache 服务器。 (在我的情况下,sudo service httpd restart,ymmv)。

      PS:写这个答案是因为我不能对 cnu 的答案发表评论(没有代表),并且想要更正 1.allowoverride 不需要设置为 ALL,它可以简单地设置为“FileInfo ",2. 更新 wordpress 文档的链接,以及 3. 提供导致此问题的 apache 2.4 更改的背景。

      【讨论】:

        【解决方案4】:

        进入管理页面,仪表板 -> 设置 -> 固定链接设置 -> 自定义结构,选择 /%postname%/ 或 /%year%/%monthnum%/%day%/%postname%/ 由您决定。

        【讨论】:

          猜你喜欢
          • 2012-08-19
          • 1970-01-01
          • 1970-01-01
          • 2015-12-14
          • 2015-05-22
          • 2016-05-14
          • 2014-10-28
          • 2012-06-15
          • 2011-05-20
          相关资源
          最近更新 更多