通过 Wordpress Rewrite 或 .htaccess 重写 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16664579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:14:47  来源:igfitidea点击:

URL rewriting via Wordpress Rewrite or .htaccess

wordpress.htaccessurl-rewriting

提问by johnRivs

JUMP TO EDIT8 TO SEE HOW I SOLVED THIS

跳转到 EDIT8 看看我是如何解决这个问题的



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.

假设我有一个 Wordpress 博客:www.animals.com. 我的主题目录中有一个特定的 PHP 文件:www.animals.com/wp-content/themes/mytheme/db.php. 另外,我有一个用于该页面的自定义模板,因此我在 Wordpress 管理面板中创建了一个新页面以显示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.).

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

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

现在,我想访问www.animals.com/database/?animal=lionwww.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?

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

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?

RewriteBase /默认情况下,根目录中的内容来自 Wordpress。我应该写什么来实现我想要的?我应该把它放在现有代码之前还是之后?

EDIT: this is my public_html.htaccess and this is what I reallywant to do: I have a website: www.domain.comand when you type this http://www.domain.com/dios/?dios=agniit shows you the info about the god Agni. I would like to type www.domain.com/dioses/agnito access the info about the god Agni. The PHP file is in www.domain.com/wp-content/themes/smitespain/dios.phpIt's not working x_x

编辑:这是我的public_html.htaccess,这是我真正想做的:我有一个网站:www.domain.com当你输入http://www.domain.com/dios/?dios=agni它时,它会向你显示有关烈火神的信息。我想输入www.domain.com/dioses/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= [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 notactual 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) =/

EDIT2:我正在使用多站点(检查 EDIT4),并且 /database/ /dios/ 和 /dioses/不是我创建的实际文件夹,这是我在 Wordpress 中创建的页面的名称。问题是,现在它在选项卡标题中显示了神的名字:S,这意味着变量 $_GET['dios'] 已设置,但它不加载文件 (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

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

EDIT4: I decided to remove the multisite thing

EDIT4:我决定删除多站点的东西

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=thorfor (www.domain.com/wp-content/themes/smitespain/dios.php)

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: i was reading something in the wordpress codex..and realized that, if i wanna go to domain.com/diosesi can access it by going to domain.com/index.php?pagename=diosesor domain.com/?pagename=dioses
So, i added this between the Rewritebase /and the next rule: RewriteRule example/test ?pagename=diosesand domain.com/example/testredirects me to domain.com/diosesbut it also changes the url in the address bar :(
The thing is, if i try this: RewriteRule example/test ?pagename=dios&dios=thorit will send me to the page 'dios' without the '?dios=thor'

EDIT6:我正在阅读 wordpress codex 中的一些内容..并意识到,如果我想去,domain.com/dioses我可以通过转到domain.com/index.php?pagename=dioses或访问它,domain.com/?pagename=dioses
所以,我在Rewritebase /规则RewriteRule example/test ?pagename=dioses和下一个规则之间添加了这个:domain.com/example/test并将我重定向到domain.com/dioses但它也改变了地址栏中的 url :(
问题是,如果我尝试这样做:RewriteRule example/test ?pagename=dios&dios=thor它会将我发送到没有 '?dios=thor' 的页面 'dios'

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

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' );

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

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

EDIT8: and finally, I managed to make it work =)
the first thing i needed to know is, the new ?dios=XXXXwill 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

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');

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

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

回答by huksley

There is another way to solve it, without touching WordPress.

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

Add to .htaccess

添加到 .htaccess

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

Create file indexw.php:

创建文件 indexw.php:

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

How it works? mod_rewrite sets REDIRECT_URI as specified in E=URI:value argument. indexw.php just overwrites REQUEST_URI with correct value (Thanks to Ravi Thapliyal for an insight on this)

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

回答by Ravi Thapliyal

WordPress puts a global redirect on root for everything unidentified i.e. not an existing file or a folder to its /index.php. You on the other hand now want to redirect them to ?animal=unidentified. So, unless the list of animal keywords is fixed any solution proposed could mess up your WordPress.

WordPress 将全局重定向放在 root 上,用于所有未识别的内容,即不是现有文件或文件夹到其/index.php. 另一方面,您现在想要将它们重定向到?animal=unidentified. 因此,除非修复了动物关键字列表,否则提出的任何解决方案都可能弄乱您的 WordPress。

If you had like 10-odd animals you could add them like below to your .htaccess (at root /)

如果你喜欢 10 多只动物,你可以像下面这样将它们添加到你的 .htaccess(在根 /)

RewriteEngine on
RewriteBase /

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

# WordPress rules come here

For 40-odd animals I would suggest you to have a directory (need not exist) prefix for your animals.

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

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

This would redirect any /a/critterto database/?animal=critterand you won't have to add them to your .htaccess manually any more. You could also have both the rules co-exist so that if you haven't modified .htaccess for /pantheryet you could still access it at /a/panther.

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

EDIT:
Okay, I looked into it and it isn't possible without writing a PHP script to intercept this request and forward it to index.php. Here's how mutisite works: since, none of your rewrites match it goes to index.php; the entry point for WordPress's php code. Somewhere deep, the code checks the REQUEST_URI header to see if it matches one of your multisites (/dios) and if it does, forwards the request to the page configured (dios.php).

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

When we do an .htaccess redirect for /dioses/agniwe're able to hit index.php(by removing the [L]) but the REQUEST_URI header still remains the same (/dioses/agni) and it has no mulisite configured for it. Hence, the redirection fails.

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

回答by Arafin Rubab

RewriteEngine On
RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^www\.animals\.com/lion$ /www.animals.com/database/?animal=lion [R=301,NE,NC,L]

Just use this code. I hope that it will works.

只需使用此代码。我希望它会起作用。

回答by gvlasov

URL rewriting wouldn't work for me, neither by calling add_filter('rewrite_rules_array', function() {}), nor by calling add_rewrite_rule(). Apparently that was because filter 'rewrite_rules_array'was never triggered when opening frontend website pages, so the rewrites weren't generated.

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

The solution was to go to admin area and just open page Settings -> Permalinks. I need to do that every time I want to change my rewrites.

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

I don't know if vanilla Wordpress works like that, this is probably caused by an plugin/theme a have installed.

我不知道 vanilla Wordpress 是否像那样工作,这可能是由已安装的插件/主题引起的。