【问题标题】:Wordpress htaccess in custom PHP自定义 PHP 中的 Wordpress htaccess
【发布时间】:2016-09-13 07:18:51
【问题描述】:

我目前正在创建一个使用自定义 PHP 代码的新网站,但我也在从远程 wordpress 检索 Wordpress 帖子。

在我的根文件夹中(就像 wordpress 一样),我有默认的 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

我还有一个 index.php 文件,它给了我来自 WP 的帖子:

<?php
// Include the wp-load'er
include('wp/wp-load.php');


// Get the last 10 posts
$recent_posts = wp_get_recent_posts(array(
    'numberposts' => 10,
    'post_status' => 'publish'
));

...
?>

在我的项目中,我还有其他 custom php 文件,例如 search.phpdetail.php 等。

所有 Wordpress 帖子链接都使用如下格式的 slug 进行格式化:www.mywebsite.com/my-post/

问题是,当 URL 不是 .php 文件时,如何更改 .htaccess 文件以便向 index.php 传递 slug 参数? (如 index.php?slug=my-post)

谢谢。

【问题讨论】:

    标签: php wordpress .htaccess


    【解决方案1】:

    您可以尝试类似的方法。 希望对你有所帮助。

    RewriteEngine On
    RewriteCond %{REQUEST_URI}  ^/index\.php$
    RewriteCond %{QUERY_STRING} ^slug=(.*)$
    RewriteRule ^(.*)$ http://www.mywebsite.com/page/%1.html [R=302,L]
    
    # 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 WordPres
    

    【讨论】:

    • 是的,可能是这样,但它不起作用:-(
    【解决方案2】:

    这就是解决方案!:

    RewriteEngine On
    RewriteRule ^(.*)/$ /index.php?slug=$1 [L]
    
    # 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 WordPres
    

    【讨论】:

      猜你喜欢
      • 2014-05-01
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      • 1970-01-01
      • 2016-04-26
      • 2018-03-20
      相关资源
      最近更新 更多