【问题标题】:wordpress htaccess how to remove all the php extension except index.phpwordpress htaccess 如何删除除 index.php 之外的所有 php 扩展
【发布时间】:2013-01-03 18:06:41
【问题描述】:

我有一个 WordPress 博客,我想在其中添加一些自定义页面,并从 url 中删除 .php 扩展名和查询字符串。

所有 WordPress 页面 url 都类似于 index.php?pagename=,所以如果我尝试在 .htaccess 文件中添加 RewriteRule ^(.*)$ $1.php,其他页面返回 500 internal server error。如何从除index.php 和login.php 之外的所有内容中删除 php 扩展名?

这是我现有的.htaccess 文件:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# RewriteRule ^(.*)$ $1.php  # add this line will broken the wordpress rewrite rule
</IfModule>
# END WordPress

【问题讨论】:

  • 您想为您的自定义页面获取什么确切的 URL 架构或 URL?

标签: wordpress apache .htaccess url-rewriting


【解决方案1】:

如果您想添加一些自定义 php 页面,您应该在标准 Wordpress 部分之前添加自己的重写,但在 RewriteBase / 之后。

在这里,我添加了一些重写,能够处理存储在私有目录 custom 中的自定义页面。

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # *** START CUSTOM PART ***
    RewriteRule ^page1$     /custom/page1.php    [L,QSA]
    RewriteRule ^page2$     /custom/page2.php    [L,QSA]
    # *** END CUSTOM PART ***

    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress

如果您的网站在http://www.example.com 上可用,您的自定义页面将可以使用http://www.example.com/page1、http://www.example.com/page2 等访问...

关于重写标志的几句话[L,QSA]:

  • L - 如果重写匹配,立即停止重写过程并且不再应用任何规则。
  • QSA - 将原始请求 URL 中的任何查询字符串附加到自定义页面。这意味着调用http://www.example.com/page1?foo=bar&amp;c=1 将在内部重写为/custom/page1.php?foo=bar&amp;c=1

请注意:

  1. 如果您的 wordpress 站点中已经有一个名为“page1”的页面,该页面将被添加到重写规则中的自定义页面覆盖。
  2. 这些 Apache httpd 指令是标准的,由 Wordpress 生成,配置 Permalink Settings。我刚刚添加了自定义部分。
  3. 任何时候您更改Permalink Settings,您的.htaccess 将被覆盖,最好更改.htaccess 权限使文件只读。而且,无论如何,请保存一份 .htaccess 配置的副本。

另一方面,如果您只希望您的 Wordpress 的 URL 对 SEO 更友好,即没有 .php 页面和完整查询字符串但只有帖子/页面 slug 作为唯一标识符的 URL,您可以访问 Wordpress Permalink Settings并选择Post name 或您喜欢的选项:

当您激活Permalink Settings 时,Apache httpd 的工作仅限于 .htaccess 部分。这意味着 Apache 通过 .htaccess 将所有内容(请求标头和正文)重定向到 index.php 页面,而 Wordpress 在内部实现每次重写。 如果您需要更多与 Wordpress 配置相关的建议,我建议您将问题写在https://wordpress.stackexchange.com/

【讨论】:

  • +1 票,现在我也使用这种方法,但我有一个自定义页面,这导致 htaccess 大小为 +200kb。所以打开这样一个问题。也许在糟糕的情况下,我想尝试将自定义页面放入新的folder,在自定义结构中设置永久链接:/archives/%post_name% 和RewriteRule ^custom/([^/\.]+)/?$ /custom/$1.php [L,QSA],我准备再次将页面移动到新文件夹中。
【解决方案2】:
# 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

好吧,这不会将 .php 留在索引上,但至少可以在没有 500 个内部服务器错误的情况下工作。为什么需要保留 index.php ? Login.php 将采用旧格式。

【讨论】:

    【解决方案3】:

    WordPress 内置了 URL 重写功能,无需手动修改 htaccess 文件。您可以在“设置”->“永久链接”下选择您希望您的 URL 在 WordPress 管理员中的显示方式。

    WordPress 还提供了一个类 WP_Rewrite,如果需要,您可以使用它来进一步调整 URL。

    【讨论】:

      【解决方案4】:

      对您的问题最明显的答案是在您的 RewriteRule 之前添加一个 RewriteCond

      RewriteCond %{REQUEST_URI} !^/(index|login).php$
      

      这将完全实现您想要的。除了 index.php 和 login.php 之外的任何请求都将根据您的规则将 .php 添加到其中,因此请求它们不会起作用。

      目前您的规则将 index.php 和 login.php 转换为 index.php.php 和 login.php.php。解决它的更合乎逻辑的方法可能是添加它:

      RewriteCond %{REQUEST_URI} !.*php$
      

      然后它只是将任何没有 .php 的东西变成与 .php 相同的东西

      请注意,您的 RewriteRule 并没有剥离查询字符串,即使您说您想要它。如果您将其更改为,它将删除查询字符串:

      RewriteRule ^(.*)$ $1.php?
      

      如果不添加 ?默认情况下它会附加query_string

      【讨论】:

        猜你喜欢
        • 2018-12-04
        • 2021-12-08
        • 2011-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-14
        • 2023-03-18
        相关资源
        最近更新 更多