简体   繁体   English

如何在Htacess中重写Wordpress URL

[英]How to do Wordpress url rewrite in htacess

I am a newbie of Wordpress. 我是Wordpress的新手。 I come cross a problem about url rewrite rule now. 我现在遇到一个有关url重写规则的问题。

I made a search form in "homepage.php" and three fields in this form. 我在“ homepage.php”中创建了一个搜索表单,并在此表单中创建了三个字段。 If user submit this form, three parameters will be sent to another Wordpress page "Results"(slug: results). 如果用户提交此表单,则三个参数将被发送到另一个Wordpress页面“结果”(结果:结果)。

<form class="filter-form" action="<?php echo get_permalink( get_page_by_title( 'Results' ) ); ?>" method="get" role="form">

I create a page named "Results" using "results.php" as a custom page template. 我使用“ results.php”作为自定义页面模板创建了一个名为“结果”的页面。

These three variables are: 1.degree(string) 2.postcodecity(string) 3.department(number eg: 75) 这三个变量是:1.degree(string)2.postcodecity(string)3.department(数字,例如75)

But these three variables are not always have value. 但是这三个变量并不总是有价值的。 Here are the 5 situations: 这是5种情况:

1.degree 2.postcodecity 3.department 4.degree & postcodecity 5.degree & department 1.学位2.postcodecity 3.部门4.degree&postcodecity 5.degree&department

I have a URL like this now: 我现在有一个像这样的URL:

www.example.com/results/?degree=college&postcodecity=&department= www.example.com/results/?degree=college&postcodecity=&department=

I need rewrite this URL to its friendly version: 我需要将此URL重写为友好版本:

1.www.example.com/results/college (degree) 1.www.example.com/results/college(degree)

2.www.example.com/results/75003 - Paris (postcodecity) 2.www.example.com/results/75003-巴黎(邮政编码)

3.www.example.com/results/75 (department) 3.www.example.com/results/75(部门)

4.www.example.com/results/college/75003 - Paris (degree & postcodecity) 4.www.example.com/results/college/75003-巴黎(学位和邮政编码)

5.www.example.com/results/college/75 (degree & department) 5.www.example.com/results/college/75(学位和系)

Note: I want to do this directly in [.htaccess] file. 注意:我想直接在[.htaccess]文件中执行此操作。

Is there someone can help me? 有没有人可以帮助我? Very gratefull about that. 非常感谢。

Finally, I gave up the method with changing .htaccess file and refers to use Wordpress method. 最后,我放弃了更改.htaccess文件的方法,而是使用Wordpress方法。 Coding in php is easier to control. 用php编码更容易控制。

To realize my objective, there are two steps should do: 要实现我的目标,应该执行两个步骤:

1.Use wp_redirect (Wordpress function) to generate the pretty URL format as you want. 1.使用wp_redirect(Wordpress函数)根据需要生成漂亮的URL格式。

2.Use add_rewrite_rule (Wordpress function) to add custom rewrite rules in order to tell Wordpress how to interpret your pretty URL. 2.使用add_rewrite_rule(Wordpress函数)添加自定义重写规则,以告诉Wordpress如何解释漂亮的URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM