如何将获取的字符串转换为Slug?

5
赏金提示:我已经使用add_rewrite_rules找到了我的问题的解决方案。我将奖励赏金给任何能够以apache RewriteRules格式提供相同解决方案的人。
我已经阅读了如何使用.htaccess创建友好的URL?,但对我来说仍然很困难和复杂。
我正在运行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. Create a template file view_sales.php in view_sales.php I will require $_GET['id'], $_GET['userid'] and $_GET['uid'] in order to retrieve info from mysql.

  2. in view_sales.php I've also used a different header file and have get_header('sales');

  3. in header-sales.php I've added the following code gotten from the above stack overflow page.

    $path_components = explode('/', $_GET['url']);
    $id=$path_components[0];
    $userid=$path_components[1];
    $uid=$path_components[2];
    
  4. I created a new wp page with slug sales so now the website is http://cp.example.com/sales/?id=123456&userid=123456&token=98917397219372iheu1i

  5. I only have one .htacess website in my /public_html/example.com domain since it's a multisite so I added the code suggested above to my .htaccess and now it looks like this.

    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 错误。
2个回答

1

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

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

如果您想要将 Wordpress MULTISITE

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

更改为

http://subdomain.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错误,并且可以通过使用以下方法获取“action”的值(在本例中为Add/Del/View)
    http://subdomain.example.com/listing/**add**
    $a = get_query_var('action');
    echo $a; //prints **add**

注意:您不能像RewriteRule一样使用$_GET获取action的值,但这应该能够完成大部分工作。
我可以继续在WordPress上工作,并获得所有自定义美丽的链接,并放弃我过去三天探索Laravel和Symfony3的工作。感谢上帝。

0
我有一个类似这样的网站:
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吗?例如:$_GET ['id'],$_GET ['userid']和$_GET ['uid']?
当然,PHP仍然能够获取值的 $_GET。

这会破坏WordPress多站点吗? - Someone Special
很抱歉,@Dave Teu,我不知道。这取决于WordPress如何确定当前站点的名称。 - Eugeny
我从其他网站上了解到Rewritecond,所以我添加了以下内容RewriteCond %{HTTP_HOST} !^cp.[NC] RewriteCond %{HTTP_HOST} ^(.+?).example.com$ [NC] RewriteRule ^listing/([a-z0-9]+)$ listing/?action=$1 [L,NC],但它仍然给我404错误。 - Someone Special

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接