【发布时间】: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 作为我的基础,但我正在使用不同的表编写应用程序端。
我试图避免使用自定义帖子类型来实现上述目的。
我尝试了以下方法。
在 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 销售的新 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 错误。
【问题讨论】:
标签: php wordpress .htaccess security mod-rewrite