dongwubao4785 2014-03-13 05:03
浏览 40
已采纳

Wordpress页面重写

How can I rewrite a Wordpress URL such that, if I have a page A hosted at www.domain.com/A, Wordpress also displays that page for www.domain.com/A/ABC, www.domain.com/A/ABCDEF, etc.? I understand that this probably isn't a good idea in general. I have a very specific use case for this. My testing server runs Ubuntu while the live server runs Windows Server 2003, both sites are on Apache.

What I've tried:

  • Modifying .htaccess directly:

    RewriteRule A/?.* A/

(I attempted to debug why this didn't work by turning on RewriteLog in my Apache server configuration. I found out that all rewrites end up at Wordpress's index.php anyway, so I figured it might be more appropriate to use Wordpress's rewrite tools for this.)

  • Using WP_Rewrite (in functions.php):

    function A_rewrite( $wp_rewrite ) { $wp_rewrite->rules["A/?.*"] = "A/"; return $wp_rewrite->rules; } add_filter('generate_rewrite_rules', 'A_rewrite');

Both of these approaches failed to rewrite the URL, i.e. the request still went to /A/ABCD, yielding a 404.

Would greatly appreciate guidance on what I did wrong with either of these approaches, or a better solution.

  • 写回答

2条回答 默认 最新

  • dqm74406 2014-03-14 00:00
    关注

    For lack of a better solution that works for me, I ended up doing the following in my functions.php:

    $original_request = $_SERVER['REQUEST_URI'];
    $new_request = preg_replace("/\/A\/?.*\?id\=([A-Za-z0-9]+)$/", "/A/?id=$1", $original_request);
    
    if ($original_request !== $new_request) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://" . $_SERVER['HTTP_HOST'] . $new_request);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $ret = curl_exec($ch);
        echo $ret;
        die();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog