悬赏说明:我已经使用 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 作为我的基础,但我正在编写应用程序端,使用不同的表。
我尽量避免使用自定义帖子类型来实现上述目标。
我尝试了以下方法。
在 view_sales.php 中创建模板文件 view_sales.php 我将需要
$_GET['id']
、$_GET['userid']
和$_GET['uid']
以便从 mysql 检索信息。在
view_sales.php
中,我还使用了不同的头文件,并且有get_header('sales')
;在 header-sales.php 中,我添加了从上述堆栈溢出页面获取的以下代码。
$path_components = explode('/', $_GET['url']); $id=$path_components[0]; $userid=$path_components[1]; $uid=$path_components[2];
我创建了一个带有 slug sales 的新 wp 页面,所以现在该网站是 http://cp.example.com/sales/?id=123456&userid=123456&token=98917397219372iheu1i
我的
/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 错误。
最佳答案
终于解决了困扰我几天的问题。
似乎特定域的重写规则不起作用。
如果你想改变
http://subdomain.example.com/listing?action=add(e.g. or DEL or VIEW)
Wordpress MULTISITE 到
http://subdomain.example.com/add/ (e.g.or DEL or 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 的工作。感谢上帝。
关于php - 如何将 get 字符串转换为 slug?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36957082/