我已经阅读了如何使用.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,但我正在编写应用程序端,并使用不同的表。
我试图避免使用自定义文章类型来实现上述目的。
我尝试了以下方法。
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.in
view_sales.php
I've also used a different header file and haveget_header('sales')
;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];
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
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 错误。