14

JUMP TO EDIT8 TO SEE HOW I SOLVED THIS


Let's say I have a Wordpress blog: www.animals.com. I have a certain PHP file in my theme directory: www.animals.com/wp-content/themes/mytheme/db.php. Also, I have a custom template for that page, so I create a new page in the Wordpress administration panel to show db.php: www.animals.com/database.

So if I want to read something about lions, I just go to: www.animals.com/database/?animal=lion (because that's the way I decided to write the PHP, inserting the value from $_GET['animal'] into the query, using PDO etc.).

Now, I would like to access www.animals.com/database/?animal=lion as www.animals.com/lion.

Should I use .htaccess or Wordpress Rewrite? Where should I place my .htaccess, in the root of the Wordpress folder (with wp-config.php and those files) or in my theme directory?

The one from the root has RewriteBase / and stuff from Wordpress by default. What should I write to achieve what I want? Should I put it before or after the existing code?

EDIT: this is my public_html .htaccess and this is what I really want to do: I have a website: www.domain.com and when you type this http://www.domain.com/dios/?dios=agni it shows you the info about the god Agni. I would like to type www.domain.com/dioses/agni to access the info about the god Agni. The PHP file is in www.domain.com/wp-content/themes/smitespain/dios.php It's not working 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: i'm using multisite (check EDIT4), and again, /database/ /dios/ and /dioses/ are not actual folders I created, thats the name for the page I created in Wordpress. The thing is, now it shows the name of the god in the tab title :S that means the variable $_GET['dios'] is set, but it doesnt load the file (dios.php) =/

EDIT3: I need to access domain.com/dios/?dios=agni specifically, because thats the URL that will let the PHP file (dios.php) to load get_header() from wordpress, so i cant access the file directly

EDIT4: I decided to remove the multisite thing

EDIT5: these are the Wordpress pages I have: domain.com/dioses/ for (www.domain.com/wp-content/themes/smitespain/dioses.php)
domain.com/dios/?dios=thor for (www.domain.com/wp-content/themes/smitespain/dios.php)

EDIT6: i was reading something in the wordpress codex..and realized that, if i wanna go to domain.com/dioses i can access it by going to domain.com/index.php?pagename=dioses or domain.com/?pagename=dioses
So, i added this between the Rewritebase / and the next rule: RewriteRule example/test ?pagename=dioses and domain.com/example/test redirects me to domain.com/dioses but it also changes the url in the address bar :(
The thing is, if i try this: RewriteRule example/test ?pagename=dios&dios=thor it will send me to the page 'dios' without the '?dios=thor'

EDIT7: i started using wordpress rewrite, i added this to my theme's functions.php:

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

and again..it loads the page dios, without the ?dios=agni

EDIT8: and finally, I managed to make it work =)
the first thing i needed to know is, the new ?dios=XXXX will be no longer available to $_GET['dios'] instead, you need to call $wp_query->query_vars['dios'] so i added this to my theme's 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');

i make sure $wp_query->query_vars['dios'] is actually set
then i just add the regular rule

4

4 回答 4

3

还有另一种解决方法,无需接触 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 对此的见解)

于 2014-11-13T20:24:55.630 回答
1

WordPress 为所有无法识别的内容(即不是现有文件或指向其/index.php. 另一方面,您现在想将它们重定向到?animal=unidentified. 因此,除非动物关键字列表得到修复,否则提出的任何解决方案都可能会弄乱您的 WordPress。

如果你有 10 多种动物,你可以像下面这样将它们添加到你的 .htaccess 中(在根目录 /)

RewriteEngine on
RewriteBase /

RewriteRule ^(lion|leopard|tiger|dog|cat)/?$ database/?animal=$1 [NC,L]

# WordPress rules come here

对于 40 多种动物,我建议您为您的动物设置一个目录(不需要存在)前缀。

RewriteRule ^a/(.+?)/?$ database/?animal=$1 [NC,L]

这会将任何重定向/a/critterdatabase/?animal=critter,您不必再手动将它们添加到您的 .htaccess 中。你也可以让这两个规则共存,这样如果你还没有修改 .htaccess ,/panther你仍然可以在 .htaccess 访问它/a/panther

编辑:
好的,我调查了一下,如果不编写 PHP 脚本来拦截此请求并将其转发到index.php. 这是 mutisite 的工作原理:因为,你的重写都不匹配它index.php;WordPress 的 php 代码的入口点。在某处深处,代码检查 REQUEST_URI 标头以查看它是否与您的多站点之一 (/dios) 匹配,如果匹配,则将请求转发到配置的页面 (dios.php )。

当我们执行 .htaccess 重定向时,/dioses/agni我们能够命中index.php(通过删除 [L]),但 REQUEST_URI 标头仍然保持不变(/dioses/agni),并且没有为它配置 mulisite。因此,重定向失败。

于 2013-05-21T08:24:49.727 回答
1
RewriteEngine On
RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^www\.animals\.com/lion$ /www.animals.com/database/?animal=lion [R=301,NE,NC,L]

只需使用此代码。我希望它会奏效。

于 2014-02-18T10:45:50.747 回答
0

URL重写对我不起作用,无论是通过调用add_filter('rewrite_rules_array', function() {})还是调用add_rewrite_rule()。显然这是因为'rewrite_rules_array'在打开前端网站页面时从未触发过滤器,因此没有生成重写。

解决方案是转到管理区域,然后打开页面设置 -> 永久链接。每次我想改变我的重写时,我都需要这样做。

我不知道香草 Wordpress 是否可以这样工作,这可能是由安装的插件/主题引起的。

于 2019-06-12T07:23:31.053 回答