通过 Wordpress Rewrite 或 .htaccess 重写 URL

编程入门 行业动态 更新时间:2024-10-10 02:16:11
本文介绍了通过 Wordpress Rewrite 或 .htaccess 重写 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

Let's say I have a Wordpress blog: www.animals. I have a certain PHP file in my theme directory: www.animals/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/database.

所以如果我想读一些关于狮子的东西,我就去:www.animals/database/?animal=lion(因为这是我决定编写 PHP 的方式,插入从 $_GET['animal'] 到查询的值,使用 PDO 等).

So if I want to read something about lions, I just go to: www.animals/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.).

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

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

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

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?

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

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?

编辑:这是我的public_html .htaccess,这是我真正想做的:我有一个网站:www.domain,当你输入这个 www.domain/dios/?dios=agni 时,它会显示信息关于烈火神.我想输入www.domain/dioses/agni 来访问有关烈火神的信息.PHP 文件位于 www.domain/wp-content/themes/smitespain/dios.php它不起作用x_x

EDIT: this is my public_html .htaccess and this is what I really want to do: I have a website: www.domain and when you type this www.domain/dios/?dios=agni it shows you the info about the god Agni. I would like to type www.domain/dioses/agni to access the info about the god Agni. The PHP file is in www.domain/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:我正在使用多站点(检查 EDIT4),而且/database//dios/和/dioses/不是> 我创建的实际文件夹,这就是我在 Wordpress 中创建的页面的名称.问题是,现在它在选项卡标题中显示了上帝的名字:S,这意味着变量 $_GET['dios'] 已设置,但它不加载文件 (dios.php) =/

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:我需要专门访问 domain/dios/?dios=agni,因为这是让 PHP 文件 (dios.php) 从 wordpress 加载 get_header() 的 URL,所以我不能直接访问文件

EDIT3: I need to access domain/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:我决定删除多站点内容

EDIT4: I decided to remove the multisite thing

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

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

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

EDIT6: i was reading something in the wordpress codex..and realized that, if i wanna go to domain/dioses i can access it by going to domain/index.php?pagename=dioses or domain/?pagename=dioses So, i added this between the Rewritebase / and the next rule: RewriteRule example/test ?pagename=dioses and domain/example/test redirects me to domain/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:我开始使用 wordpress 重写,我将此添加到我的主题的functions.php:

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

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

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

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

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

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

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

推荐答案

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

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

添加到 .htaccess

Add to .htaccess

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

创建文件 indexw.php:

Create file 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 对此的见解)

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)

更多推荐

通过 Wordpress Rewrite 或 .htaccess 重写 URL

本文发布于:2023-11-01 11:14:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1549187.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   Rewrite   Wordpress   URL   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!