跳转到编辑8以查看我如何解决此问题



假设我有一个Wordpress博客:www.animals.com。我的主题目录中有一个特定的PHP文件:www.animals.com/wp-content/themes/mytheme/db.php。另外,我为该页面有一个自定义模板,因此我在Wordpress管理面板中创建了一个新页面以显示db.phpwww.animals.com/database

因此,如果我想阅读有关狮子的内容,请直接转到:www.animals.com/database/?animal=lion(因为这就是我决定编写PHP的方式,使用PDO等将$ _GET ['animal']中的值插入查询中) 。

现在,我想以www.animals.com/database/?animal=lion身份访问www.animals.com/lion

我应该使用.htaccess还是Wordpress Rewrite?我的.htaccess文件应该放在Wordpress文件夹的根目录(带有wp-config.php和那些文件)中还是主题目录中的什么位置?

默认情况下,根目录中的一个具有RewriteBase /,而Wordpress中的则具有。我应该写些什么来实现自己想要的?我应该在现有代码之前还是之后?

编辑:这是我的public_html .htaccess,这是我真正想要做的:
我有一个网站:www.domain.com,当您键入此http://www.domain.com/dios/?dios=agni时,它会向您显示有关Agni神的信息。我想输入www.domain.com/dioses/agni以访问有关Agni神的信息。 PHP文件在www.domain.com/wp-content/themes/smitespain/dios.php
它不起作用x_x

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^dioses/(\w*)$ dios/?dios=$1 [NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress


EDIT2:我正在使用多站点(请检查EDIT4),/ database // dios /和/ dioses /不是我创建的实际文件夹,多数民众赞成在Wordpress中创建的页面的名称。问题是,现在它在选项卡标题:S中显示了上帝的名字:这意味着已设置变量$ _GET ['dios'],但不会加载文件(dios.php)= /

EDIT3:我需要专门访问domain.com/dios/?dios=agni,因为那是让PHP文件(dios.php)从wordpress加载get_header()的URL,所以我无法直接访问该文件

EDIT4:我决定删除多站点内容

EDIT5:这些是我拥有的Wordpress页面:
domain.com/dioses/表示(www.domain.com/wp-content/themes/smitespain/dioses.php)
domain.com/dios/?dios=thor表示(www.domain.com/wp-content/themes/smitespain/dios.php)

EDIT6:我正在阅读Wordpress Codex中的内容,并意识到,如果我想转到domain.com/dioses,可以通过转到domain.com/index.php?pagename=diosesdomain.com/?pagename=dioses来访问它。
因此,我在Rewritebase /和下一条规则之间添加了此代码:RewriteRule example/test ?pagename=diosesdomain.com/example/test将我重定向到domain.com/dioses,但它还会更改地址栏中的url :(
问题是,如果我尝试这样做:RewriteRule example/test ?pagename=dios&dios=thor它将把我发送到页面“ dios”,而没有“?dios = thor”

EDIT7:我开始使用wordpress重写,将其添加到主题的functions.php中:

function my_rewrite_rules() {
    add_rewrite_rule(
        'blah',
        'index.php?pagename=dios&dios=agni',
        'top'
    );
}
add_action( 'init', 'my_rewrite_rules' );


并再次..它加载页面dios,而没有?dios = agni

EDIT8:最后,我设法使其起作用=)
我需要知道的第一件事是,新的?dios=XXXX将不再对$_GET['dios']可用,您需要调用$wp_query->query_vars['dios'],所以我将其添加到了主题的functions.php中

function add_query_vars($new_var) {
$new_var[] = "dios";
return $new_var;
}
add_filter('query_vars', 'add_query_vars');

function add_rewrite_rules($rules) {
$new_rules = array('([^/]+)' => 'index.php?pagename=dios&dios=$matches[1]');
$rules = $new_rules + $rules;
return $rules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');


我确保确实设置了$wp_query->query_vars['dios']
然后我只添加常规规则

最佳答案

还有另一种解决方法,无需接触WordPress。

添加到.htaccess

RewriteRule ^lion /indexw.php?pagename=db&dios=$1 [L,E=URI:/detail/]


创建文件indexw.php:

<?php $_SERVER["REQUEST_URI"] = $_SERVER["REDIRECT_URI"]; include("index.php");


这个怎么运作? mod_rewrite设置E = URI:value参数中指定的REDIRECT_URI。
indexw.php只是用正确的值覆盖REQUEST_URI(感谢Ravi Thapliyal对此提供的见解)

08-25 10:14