[英]Remove index.php from Wordpress URLs
我是 Wordpress 的新手,但尝试了所有文档和 Stack Overflow 帖子以寻求可能的解决方案,但到目前为止它们都没有奏效。
我已经验证 mod rewrite 已启用并按预期工作。 遵循此处提到的所有步骤。 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>
我花了大量时间尝试在这里或其他地方回答的许多不同方法,但没有一个解决了我的问题。
这就是我所做的,它解决了我的问题。
httpd.conf 文件中的条目:
<Directory "/var/www/html/blog">
Options FollowSymLinks
AllowOverride All
假设 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) 在 Web 根目录中创建 .htaccess。
touch /var/www/html/.htaccess
将以下内容插入 .htaccess
RewriteEngine On
6 配置URL Rewrite (注:假设为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 介绍和 Apache 的mod_rewrite 官方文档。
简而言之,正如所提到的@Cnu,互联网上很少有资源涉及的问题是,您的 apache 配置必须包含一个 AllowOverride FileInfo 指令,以便 wordpress 在没有 /index.php/ 的情况下重写工作。
从 Apache 2.4 开始,AllowOverride 默认设置为“无”,这通常是使“漂亮的 url”工作的障碍(在这种情况下放弃 index.php)。 正如@cnu 提到的,您应该仔细阅读此资源: https ://wordpress.org/support/article/using-permalinks/。
<?php phpinfo();?>
)在您博客的根目录并调用https://domainofyourb.log/信息.php )<Directory></Directory>
部分中包含指令 AllowOverride FileInfoPS:写这个答案是因为我无法对 cnu 的答案发表评论(没有代表),并且想要更正 1.allowoverride 不需要设置为 ALL,它可以设置为简单的“FileInfo”,2 . 更新 wordpress 文档的链接,以及 3. 提供导致此问题的 apache 2.4 更改的背景。
转到管理页面,仪表板 -> 设置 -> 固定链接设置 -> 自定义结构,选择 /%postname%/ 或 /%year%/%monthnum%/%day%/%postname%/ 由您决定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.