wordpress htaccess如何删除除index.php之外的所有php扩展

fis*_*man 4 apache wordpress .htaccess url-rewriting

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

所有WordPress的网页网址都喜欢index.php?pagename=,所以如果我尝试添加RewriteRule ^(.*)$ $1.php了在.htaccess文件中,其他页面返回500 internal server error.如何从除了index.php和之外的所有内容中删除php扩展login.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
Run Code Online (Sandbox Code Playgroud)

fre*_*dev 6

如果你想加入,例如,一些自定义的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
Run Code Online (Sandbox Code Playgroud)

如果您的网站可用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&c=1将在内部重新编写/custom/page1.php?foo=bar&c=1

请注意:

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

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

Wordpress永久链接设置

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