dongtuo6562 2018-09-01 01:46
浏览 64

将文件夹屏蔽/重定向到wordpress上的查询字符串

I have a single page in Wordpress that has lookups to a sql server and presents different content based on the querystring.
For instance, the URL I use is https://some-server.com/dynamic-page/?stem=name-of-content-to-be-looked-up

When I access the page and change the querystring, the content changes. Perfect.

Now, I do not want the querystring to be exposed to the end user, so I am attempting to use mod_rewrite such as this:

RewriteRule ^ds/(.*) /dynamic-page/?stem=$1 [PT]

The idea being that the URL to the end user would look like https://some-server.com/ds/name-of-content-to-be-looked-up

When I use a 301 redirect, it works great. When I use the internal redirect, it 404's. I have tried several different flags on the end, but all fail with a 404.

Has anyone ever tried this and done it successfully? Care to share some ideas?

I have tried the following, plus more, none work:

RewriteRule ^ds/(.*) /dynamic-page/?stem=$1 [PT]
RewriteRule ^ds/(.*) /dynamic-page/?stem=$1 [L]
RewriteRule ^ds/(.*) /dynamic-page/?stem=$1 [R,L]
RewriteRule ^ds/(.*) /dynamic-page?stem=$1 [QSA,NS,L]

RewriteCond %{REQUEST_URI} ^/ds/
RewriteRule ^ds/(.*) /index.php/dynamic-page?stem=$1 [PT]

Any ideas?

Thanks, Brian

  • 写回答

1条回答 默认 最新

  • duanqin2026 2018-09-13 02:48
    关注

    OK, I figured it out. Instead of doing it in apache redirect, I was able to add some filters into my wordpress functions.php file:

    function setup_filter_query_vars($query_vars)
    {
        $query_vars[] = 'stem';
        return $query_vars;
    }
    add_filter( 'query_vars', 'setup_filter_query_vars' );
    
    
    function setup_filter_rewrites()
    {
        add_rewrite_rule('ds/([^/]*)/?', 'index.php?stem=$matches[1]&pagename=dynamic-page&', 'top');
    }
    add_action( 'init', 'setup_filter_rewrites' );  
    

    After that, I had to reference the querystring variable in a slightly different fashion from within the page. I had to change the standard "$_GET["stem"];" to "get_query_var( 'stem', 1 );"

    I hope this helps someone else.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大