WordPress页面地址重写

发布于 2024-08-25 13:04:10 字数 2296 浏览 4 评论 0原文

更新

我尝试使用内部 WordPress 重写。我要做的就是这样一个地址:

http://example.com/galleria/artist-name

发送到 gallery.php 页面,其中包含一个包含 artist-name 的变量。

我按照 WordPress 的文档使用了这些规则:

// REWRITE RULES (per gallery) {{{
add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('query_vars','wp_insertMyRewriteQueryVars');
add_filter('init','flushRules');

// Remember to flush_rules() when adding rules
function flushRules(){
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}

// Adding a new rule
function wp_insertMyRewriteRules($rules)
{
    $newrules = array();
    $newrules['(galleria)/(.*)$'] = 'index.php?pagename=gallery&galleryname=$matches[2]';
    return $newrules + $rules;
}

// Adding the id var so that WP recognizes it
function wp_insertMyRewriteQueryVars($vars)
{
    array_push($vars, 'galleryname');
    return $vars;
}

现在奇怪的是,在我的本地 WordPress 测试安装上,效果很好:调用图库页面并传递 galleryname 变量。另一方面,在真实的网站上,初始 URL 被接受(因为它不会进入 404),但它会更改为 http://example.com/gallery (我的意思是它实际上在浏览器的地址栏中发生了变化)并且该变量未在 gallery.php 中定义。

知道什么可能导致这种不同的行为吗?

或者,我想不出任何其他方法可以达到前三行中描述的相同效果,这是完全可以的。


老问题

我需要做的是重写这个地址:

(1) http://localhost/wordpress/fake/text-value

注释

(2) http://localhost/wordpress/gallery?somevar=text-value

  • 重新映射必须是透明的:用户总是必须看到地址 (1)
  • gallery 是到的永久链接一个wordpress页面,不是一个真实的地址

我基本上需要先重写地址(修改它),然后再次将其反馈给mod重写(让wordpress以自己的方式解析它)。

问题

如果我只是简单地

RewriteRule ^fake$ http://localhost/wordpress/gallery [L]

这样做,但浏览器中的地址发生了变化,这不好,如果我这样做,

RewriteRule ^fake$ /wordpress/gallery [L]

我会得到 404。我尝试了不同的标志而不是 [L] 但没有效果。我怎样才能让它发挥作用?

编辑:完整的.htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^fake$ /wordpress/gallery [R]

RewriteBase /wordpress/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>

# END WordPress

UPDATE

I tried using the internal wordpress rewrite. What I have to do is an address like this:

http://example.com/galleria/artist-name

sent to the gallery.php page with a variable containing the artist-name.

I used these rules as per Wordpress' documentation:

// REWRITE RULES (per gallery) {{{
add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('query_vars','wp_insertMyRewriteQueryVars');
add_filter('init','flushRules');

// Remember to flush_rules() when adding rules
function flushRules(){
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}

// Adding a new rule
function wp_insertMyRewriteRules($rules)
{
    $newrules = array();
    $newrules['(galleria)/(.*)

what's weird now is that on my local wordpress test install, that works fine: the gallery page is called and the galleryname variable is passed. On the real site, on the other hand, the initial URL is accepted (as in it doesn't go into a 404) BUT it changes to http://example.com/gallery (I mean it actually changes in the browser's address bar) and the variable is not defined in gallery.php.

Any idea what could possibly cause this different behavior?

Alternatively, any other way I couldn't think of which could achieve the same effect described in the first three lines is perfectly fine.


Old question

What I need to do is rewriting this address:

(1) http://localhost/wordpress/fake/text-value

to

(2) http://localhost/wordpress/gallery?somevar=text-value

Notes:

  • the remapping must be transparent: the user always has to see address (1)
  • gallery is a permalink to a wordpress page, not a real address

I basically need to rewrite the address first (to modify it) and then feed it back to mod rewrite again (to let wordpress parse it its own way).

Problems

if I simply do

RewriteRule ^fake$ http://localhost/wordpress/gallery [L]

it works but the address in the browser changes, which is no good, if I do

RewriteRule ^fake$ /wordpress/gallery [L]

I get a 404. I tried different flags instead of [L] but to no avail. How can I get this to work?

EDIT: full .htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^fake$ /wordpress/gallery [R]

RewriteBase /wordpress/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>

# END WordPress
] = 'index.php?pagename=gallery&galleryname=$matches[2]'; return $newrules + $rules; } // Adding the id var so that WP recognizes it function wp_insertMyRewriteQueryVars($vars) { array_push($vars, 'galleryname'); return $vars; }

what's weird now is that on my local wordpress test install, that works fine: the gallery page is called and the galleryname variable is passed. On the real site, on the other hand, the initial URL is accepted (as in it doesn't go into a 404) BUT it changes to http://example.com/gallery (I mean it actually changes in the browser's address bar) and the variable is not defined in gallery.php.

Any idea what could possibly cause this different behavior?

Alternatively, any other way I couldn't think of which could achieve the same effect described in the first three lines is perfectly fine.


Old question

What I need to do is rewriting this address:

to

Notes:

  • the remapping must be transparent: the user always has to see address (1)
  • gallery is a permalink to a wordpress page, not a real address

I basically need to rewrite the address first (to modify it) and then feed it back to mod rewrite again (to let wordpress parse it its own way).

Problems

if I simply do

it works but the address in the browser changes, which is no good, if I do

I get a 404. I tried different flags instead of [L] but to no avail. How can I get this to work?

EDIT: full .htaccess

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梨涡 2024-09-01 13:04:10

试试这个规则:

RewriteRule ^fake/([^/]+)$ gallery?somevar=$1 [L]

Try this rule:

RewriteRule ^fake/([^/]+)$ gallery?somevar=$1 [L]
萌酱 2024-09-01 13:04:10

解决了,这是 Permalink Redirect 插件,需要配置为跳过所需的“虚拟”URL,否则会发出 301 永久移动。

Solved, it was the Permalink Redirect plugin which needed to be configured to skip the desired "virtual" URLs, else it would issue a 301 Permanently moved.

停顿的约定 2024-09-01 13:04:10

对于这里非常延迟的答案表示歉意,但我相信您需要做的是 Gumbo 建议的内容,但使用 添加 passthrough/PT 标志

该标志强制重写引擎将内部 request_rec 结构的 uri 字段设置为 filename 字段的值。
...
如果您想混合来自允许 URL 到文件名转换器的不同模块的指令,则必须使用此标志。

尽管 WordPress(通过 mod_php)在 mod_rewrite 完成后运行,但它通常会获取原始(重写前)uri,并使用它来进行自己的内部重写,而不是重写的文件名值。

所以尝试一下:

RewriteRule ^fake/([^/]+)$ gallery?somevar=$1 [QSA,PT]

我添加了 QSA,这样万一有人访问 /wordpress/fake/something?page=3,它就会正确传递。让我知道这是如何运作的。

Apologies for the very-delayed answer here, but I believe what you need to do is what Gumbo suggested but with the addition of the passthrough/PT flag:

This flag forces the rewrite engine to set the uri field of the internal request_rec structure to the value of the filename field.
...
You must use this flag if you want to mix directives from different modules which allow URL-to-filename translators.

Although Wordpress (via mod_php) runs after mod_rewrite is done, it normally gets the original (pre-rewrite) uri, and it uses that to do its own internal rewriting, rather than the re-written filename value.

So try:

RewriteRule ^fake/([^/]+)$ gallery?somevar=$1 [QSA,PT]

I added QSA so that, just in case someone went to /wordpress/fake/something?page=3, it will get passed along properly. Let me know how that works.

乖乖兔^ω^ 2024-09-01 13:04:10
  1. 使用您最喜欢的 HTML/PHP 编辑器,打开 wp-admin/includes/misc.php

  2. 查找函数 function got_mod_rewrite 大约在第 18 行左右。

  3. 注释掉现有函数:/* this is commented out */ — 如果您确定解决方案有效,则可以删除此函数,但为了安全起见,只需将其注释掉,以防万一需要把它放回去。

  4. 添加以下内容:

    函数got_mod_rewrite() {
        $got_rewrite = true;
        返回 apply_filters('got_rewrite', $got_rewrite);
    }
    
  5. 保存并上传。

  1. Using your favorite HTML/PHP editor, open wp-admin/includes/misc.php

  2. Find function function got_mod_rewrite Its around line 18 or so.

  3. Comment out the existing function: /* this is commented out */ — You can delete this function if you are sure the solution works, but to be safe, just comment it out in case you need to put it back.

  4. Add the following instead:

    function got_mod_rewrite() {
        $got_rewrite = true;
        return apply_filters(‘got_rewrite’, $got_rewrite);
    }
    
  5. Save and upload.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文