【问题标题】:How to convert get strings to slug?如何将get字符串转换为slug?
【发布时间】:2023-03-13 02:37:01
【问题描述】:

赏金注意:我使用 add_rewrite_rules 找到了我的问题的解决方案。我将奖励任何能够以 apache RewriteRules 格式为我提供相同解决方案的人。

我已经阅读了How can I create friendly URLs with .htaccess?,但对我来说仍然很困难和复杂。

我正在运行 WordPress Multisite,我有一个子域网站 cp.example.com,我正在这个子域上编写一个 php 应用程序。

我有一个如下所示的网址:

http://cp.example.com/sales.php?id=12345&userid=123456&uid=83hkuhdqhuhd873xsuhdqwiuhdiq

我是否可以让用户通过以下方式访问网站:

http://cp.example.com/sales/12345/userid/123456/83hkuhdqhuhd873xsuhdqwiuhdiq

如果我这样做,php 仍然能够对值执行$_GET 吗?

例如$_GET['id']$_GET['userid']$_GET['uid']?

我使用 WordPress 作为我的基础,但我正在使用不同的表编写应用程序端。

我试图避免使用自定义帖子类型来实现上述目的。

我尝试了以下方法。

  1. 在 view_sales.php 中创建模板文件 view_sales.php 我需要 $_GET['id']$_GET['userid']$_GET['uid'] 以便从 mysql 中检索信息。

  2. view_sales.php 中,我还使用了不同的头文件并拥有get_header('sales')

  3. 在 header-sales.php 中,我添加了从上面的堆栈溢出页面获取的以下代码。

    $path_components = explode('/', $_GET['url']);
    $id=$path_components[0];
    $userid=$path_components[1];
    $uid=$path_components[2];
    
  4. 我创建了一个带有 slug 销售的新 wp 页面,所以现在网站是 http://cp.example.com/sales/?id=123456&userid=123456&token=98917397219372iheu1i

  5. 我的/public_html/example.com 域中只有一个.htacess 网站,因为它是一个多站点,所以我将上面建议的代码添加到我的.htaccess,现在它看起来像这样。

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    
    # add a trailing slash to /wp-admin
    RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
    RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
    RewriteRule . index.php [L]
    
    <IfModule mod_rewrite.c>
     RewriteEngine On
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
    </IfModule>  
    

目前没有任何工作,仍然重定向到丑陋的网址。

已编辑:

我尝试了以下方法

RewriteCond %{HTTP_HOST} !^cp\.example\.com [NC]
RewriteRule ^listing/([a-z0-9]+)$ listing/?action=$1 [L,NC] 
RewriteRule ^view/([a-z9-9]+)$ view/?id=$ [L,NC]

我尝试了以上所有方法的不同变体,但没有任何效果。

针对我尝试过的 add_rewrite_rule 方法进行了编辑

function pa_custom_rules() {
    add_rewrite_rule('^/listing/([^/]+)/$', 'index.php?pagename=listing&action=$matches[1]', 'top');
}
add_action('init', 'pa_custom_rules');

打印了重写数组,我可以看到新规则

      [^/listing/([^/]+)/$] => index.php?pagename=listing&action=$matches[1]

访问 index.php 有效,但访问 /listing/test/ 失败。它返回 404 错误。

【问题讨论】:

    标签: php wordpress .htaccess security mod-rewrite


    【解决方案1】:

    终于解决了这几天困扰我的问题。

    似乎重写特定域的规则不起作用。

    如果你想改变

    http://子域.example.com/listing?action=add(例如,DEL 或 VIEW)

    Wordpress MULTISITE

    http://子域.example.com/add/(例如 DEL 或 VIEW)

    您必须不仅添加重写规则,还必须将标签重写/添加到 query_var

    您需要添加以下功能或创建一个插件来启动它。

    function pa_custom_rules() {
        add_rewrite_rule('listing/(.*)$', 'index.php?pagename=listing&action=$matches[1]', 'top');
       add_rewrite_tag('%action%', '(.*)');
    }
    add_action('init', 'pa_custom_rules');
    

    完成后,您将能够访问 http://subdomain.example.com/listing/add

    在您的页面中(在本例中,我的页面名称为“listing”),您将不再收到 404 错误,您可以通过使用

        http://subdomain.example.com/listing/**add**
        $a = get_query_var('action');
        echo $a; //prints **add**
    

    注意:您无法使用 $_GET 获取 action 的值,就像 RewriteRule 的工作原理一样,但这应该能够完成大多数事情。

    我可以继续在 wordpress 上工作并获得所有我自定义的漂亮链接,并放弃我过去 3 天探索 Laravel 和 Symfony3 的工作。感谢上帝。

    【讨论】:

      【解决方案2】:

      我有一个这样的网站

       http://cp.example.com/sales.php?id=12345&userid=123456&uid=83hkuhdqhuhd873xsuhdqwiuhdiq

      我可以让用户通过以下方式访问网站吗

       http://cp.example.com/sales/12345/userid/123456/83hkuhdqhuhd873xsuhdqwiuhdiq

      是的,有可能,例如:

      RewriteEngine On
      RewriteRule ^sales/(\d+)/userid/(\d+)/([a-z0-8]+)$ sales.php?id=$1&userid=$2&uid=$3 [L,NC]
      

      如果我这样做.. php 仍然能够在 价值观?

      例如 $_GET['id']、$_GET['userid'] 和 $_GET['uid']

      是的,当然 php 可以做到。

      【讨论】:

      • 这会破坏 wordpress 多站点吗?
      • @Dave Teu 不幸的是我不知道。这取决于 wordpress 如何确定当前站点的名称。
      • 我从其他网站读到 Rewritecond 所以我添加了以下 RewriteCond %{HTTP_HOST} !^cp.[NC] RewriteCond %{HTTP_HOST} ^(.+?)\.example.com$ [ NC] RewriteRule ^listing/([a-z0-9]+)$ listing/?action=$1 [L,NC] 但它一直给我 404 错误
      猜你喜欢
      • 2017-03-31
      • 1970-01-01
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      • 2015-06-24
      相关资源
      最近更新 更多