我可以让add_rewrite_rule
在本地与MAMP一起工作,但不能在我的DreamHost制作服务器上工作。
我试图在http://www.pawsnewengland.com/our-dogs-list/为美化宠物的网址做好准备。丑陋的URL遵循以下结构:our-dogs-list /?view = pet-details& id = 12345,自定义函数使用$_GET
变量处理信息。
在我的functions.php
文件中,我已将其包括在内:
function rewrite_pet_url() {
add_rewrite_rule(
"our-dogs-list/pet-details/([0-9]+)/?$",
"index.php/our-dogs-list/?view=pet-details&id=$1",
"top"
);
}
add_action( 'init', 'rewrite_pet_url');
我也尝试了同样的结果:
function rewrite_pet_url() {
add_rewrite_rule(
"our-dogs-list/pet-details/([0-9]+)/?$",
"index.php/our-dogs-list/?view=pet-details&id=$matches[1]",
"top"
);
}
add_action( 'init', 'rewrite_pet_url');
为了简单地测试重写是否会起作用,尝试了这个:
function rewrite_pet_url() {
add_rewrite_rule(
"fake",
"index.php/about",
"top"
);
}
add_action( 'init', 'rewrite_pet_url');
我在测试之前刷新重写规则,并确认重写规则已添加到.htaccess
文件中。但是出于某种原因,我看到的是404页面或白色屏幕并且“未指定输入文件。”
我能够在本地工作,所以我不知道在现场服务器上有什么打破。任何见解?
我已经将重写“工作”,因为它不再导致任何错误。不幸的是,它现在导致不必要的重定向到根URL。
function rewrite_pet_url() {
add_rewrite_tag('%view%','([^&]+)');
add_rewrite_tag('%id%','([^&]+)');
add_rewrite_rule(
'our-dogs-list/pet-details/([0-9]+)?$',
'index.php/?page_id=1663&view=pet-details&id=$1',
'top'
);
}
add_action( 'init', 'rewrite_pet_url' );
有了这个,我可以使用view
访问id
和get_query_var()
变量。但是,WordPress不会尊重example.com/our-dogs-list/pet-details/12345
,而是将页面重定向到example.com/our-dogs-list/
。
知道可能导致这种情况的原因是什么?它有效地使重写规则变得毫无用处。
答案 0 :(得分:5)
这很长时间才能添加评论。
我会将它添加到functions.php中。我已经更改了你的永久链接,我的demo plugin解释了每个函数的作用:
add_filter('generate_rewrite_rules', 'pet_rewrite_url');
add_filter('query_vars', 'pet_query_vars'));
add_filter('admin_init', 'pet_flush_rewrite_rules');
add_action("parse_request", "pet_parse_request");
function pet_rewrite_url( $wp_rewrite ) {
$new_rules = array(
'our-dogs-list/pet-details/([0-9]+)/?$' => sprintf("index.php?pet-details=%s",$wp_rewrite->preg_index(1))
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
return $wp_rewrite->rules;
}
function pet_query_vars( $query_vars ) {
$query_vars[] = 'pet-details';
return $query_vars;
}
function pet_flush_rewrite_rules() {
$rules = $GLOBALS['wp_rewrite']->wp_rewrite_rules();
if ( ! isset( $rules['our-dogs-list/pet-details/([0-9]+)/?$'] ) ) { // must be the same rule as in pet_rewrite_url($wp_rewrite)
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
function pet_parse_request($wp_query) {
if (isset($wp_query->query_vars['pet-details'])) { // same as the first custom variable in pet_query_vars( $query_vars )
// add your code here, code below is for this demo
printf("<pre>%s</pre>",print_r($wp_query->query_vars,true));
exit(0);
}
}
答案 1 :(得分:3)
我已经写了一个关于如何添加自定义永久链接的指南,这里是示例插件(也是available at my site):
<?php
/*
Plugin Name: My Permalink Demo
Plugin URI: http://soderlind.no/archives/2012/11/01/wordpress-plugins-and-permalinks-how-to-use-pretty-links-in-your-plugin/
Description: Demo plugin to show how to implement your custom permalink for your plugin. To test, add the [mypermalink] or [mypermalink val="ipsum"] shortcode to a page or post.
Version: 1.0.1
Author: Per Soderlind
Author URI: http://soderlind.no/
*/
if (!class_exists('my_permalink')) {
class my_permalink {
function __construct(){
// demo shortcode
add_shortcode('mypermalink', array(&$this,'my_permalink_demo_shortcode'));
// permalink hooks:
add_filter('generate_rewrite_rules', array(&$this,'my_permalink_rewrite_rule'));
add_filter('query_vars', array(&$this,'my_permalink_query_vars'));
add_filter('admin_init', array(&$this, 'my_permalink_flush_rewrite_rules'));
add_action("parse_request", array(&$this,"my_permalink_parse_request"));
}
/**************************************************************************
* Demo shortcode
* A simple shortcode used to demonstrate the plugin.
*
* @see http://codex.wordpress.org/Shortcode_API
* @param array $atts shortcode parameters
* @return string URL to demonstrate custom permalink
**************************************************************************/
function my_permalink_demo_shortcode($atts) {
extract(shortcode_atts(array(
// default values
'val' => 'lorem'
), $atts));
return sprintf('<a href="%s">My permalink</a>',$this->my_permalink_url($val));
}
/**************************************************************************
* Create your URL
* If the blog has a permalink structure, a permalink is returned. Otherwise
* a standard URL with param=val.
*
* @param sting $val Parameter to custom url
* @return string URL
**************************************************************************/
function my_permalink_url($val) {
if ( get_option('permalink_structure')) { // check if the blog has a permalink structure
return sprintf("%s/my-permalink/%s",home_url(),$val);
} else {
return sprintf("%s/index.php?my_permalink_variable_01=%s",home_url(),$val);
}
}
/**************************************************************************
* Add your rewrite rule.
* The rewrite rules array is an associative array with permalink URLs as regular
* expressions (regex) keys, and the corresponding non-permalink-style URLs as values
* For the rule to take effect, For the rule to take effect, flush the rewrite cache,
* either by re-saving permalinks in Settings->Permalinks, or running the
* my_permalink_flush_rewrite_rules() method below.
*
* @see http://codex.wordpress.org/Custom_Queries#Permalinks_for_Custom_Archives
* @param object $wp_rewrite
* @return array New permalink structure
**************************************************************************/
function my_permalink_rewrite_rule( $wp_rewrite ) {
$new_rules = array(
'my-permalink/(.*)$' => sprintf("index.php?my_permalink_variable_01=%s",$wp_rewrite->preg_index(1))
/*
// a more complex permalink:
'my-permalink/([^/]+)/([^.]+).html$' => sprintf("index.php?my_permalink_variable_01=%s&my_permalink_variable_02=%s",$wp_rewrite->preg_index(1),$wp_rewrite->preg_index(2))
*/
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
return $wp_rewrite->rules;
}
/**************************************************************************
* Add your custom query variables.
* To make sure that our parameter value(s) gets saved,when WordPress parse the URL,
* we have to add our variable(s) to the list of query variables WordPress
* understands (query_vars filter)
*
* @see http://codex.wordpress.org/Custom_Queries
* @param array $query_vars
* @return array $query_vars with custom query variables
**************************************************************************/
function my_permalink_query_vars( $query_vars ) {
$query_vars[] = 'my_permalink_variable_01';
/*
// need more variables?:
$query_vars[] = 'my_permalink_variable_02';
$query_vars[] = 'my_permalink_variable_03';
*/
return $query_vars;
}
/**************************************************************************
* Parses a URL into a query specification
* This is where you should add your code.
*
* @see http://codex.wordpress.org/Query_Overview
* @param array $atts shortcode parameters
* @return string URL to demonstrate custom permalink
**************************************************************************/
function my_permalink_parse_request($wp_query) {
if (isset($wp_query->query_vars['my_permalink_variable_01'])) { // same as the first custom variable in my_permalink_query_vars( $query_vars )
// add your code here, code below is for this demo
printf("<pre>%s</pre>",print_r($wp_query->query_vars,true));
exit(0);
}
}
/**************************************************************************
* Flushes the permalink structure.
* flush_rules is an extremely costly function in terms of performance, and
* should only be run when changing the rule.
*
* @see http://codex.wordpress.org/Rewrite_API/flush_rules
**************************************************************************/
function my_permalink_flush_rewrite_rules() {
$rules = $GLOBALS['wp_rewrite']->wp_rewrite_rules();
if ( ! isset( $rules['my-permalink/(.*)$'] ) ) { // must be the same rule as in my_permalink_rewrite_rule($wp_rewrite)
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
} //End Class
} //End if class exists statement
if (class_exists('my_permalink')) {
$my_permalink_var = new my_permalink();
}
?>
答案 2 :(得分:2)
您的替换看起来不正确,请尝试:
function rewrite_pet_url() {
add_rewrite_rule(
"our-dogs-list/pet-details/([0-9]+)/?$",
"index.php?view=pet-details&id=$1",
"top"
);
}
add_action( 'init', 'rewrite_pet_url');
请注意使用index.php
而非index.php/our-dogs-list/
作为目的地。
或许它就像这样:
function rewrite_pet_url() {
add_rewrite_rule(
"our-dogs-list/pet-details/([0-9]+)/?$",
"our-dogs-list/index.php?view=pet-details&id=$1",
"top"
);
}
add_action( 'init', 'rewrite_pet_url');
答案 3 :(得分:2)
在你的.htaccess中修改
RewriteRule ^our-dogs-list/pet-details/([0-9]+)?$ /index.php?page_id=1663&view=pet-details&id=$1 [L]
为RewriteRule ^index\.php$ - [L]
添加此内容,以便您.hatcces可以这样:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^our-dogs-list/pet-details/([0-9]+)?$ /wordpress/index.php?page_id=2&view=pet-details&id=$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
<强>更新强>
来自:http://codex.wordpress.org/Rewrite_API/add_rewrite_rule
重要说明:默认情况下,WordPress无法识别自定义查询字符串 用于重写的变量。您必须注册您的查询字符串 WordPress的变量。只需使用add_rewrite_tag()来执行此操作,或 以上改写不起作用!有关捕获的更多信息 重写后的查询字符串变量值可以在这里找到。
到目前为止,我了解您应该在第一个示例中使用:{_ 1}}和view
的add_rewrite_tag()。