2016-04-30 117 views
5

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

我读过How can I create friendly URLs with .htaccess?但它仍然困难和复杂的我。

我正在运行WordPress多站点,并且我有一个子域网站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. in header-sales.php我已经添加了从上面的堆栈溢出页面获得的以下代码。

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

  5. 我只有一个.htacess网站在我/public_html/example.com领域,因为它是一个多点,所以我说上面的代码提示我的.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] 

我试过的所有上述但没有任何工程不同的变体。

编辑的,我试过

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的作品,但会add_rewrite_rule方法/列表/测试/失败。它返回404错误。

回答

1

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

它出现重写特定域的规则不起作用。

如果你想改变

的http:// 子域 .example.com的/上市? 行动=添加(例如或DEL或VIEW)

一个 WordPress的MultiSite的

HTTP的

:// 子域 .example.com的/ 添加/(叶戈尔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:// 子域。实例。com/listing/add

在您的页面(在这种情况下,我的页面名称是'listing'),您将不会再收到404错误,并且您可以获取“action”的值(在本例中为Add /删除/查看),通过使用

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

注意:不能得到行动使用$ _GET怎么样的RewriteRule作品的价值,但是这应该是能够得到做得最多的事情。

我可以继续在wordpress上工作,并获得我所有定制的美丽链接,并扔掉我最后3天的工作探索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的 值?

e.g $ _GET [ '身份证'],$ _GET [ '用户ID']和$ _GET [ 'UID']

是的,当然PHP就能做到这一点。

+0

会打破wordpress多站点? –

+0

@Dave Teu不幸的是我不知道。这取决于wordpress如何确定当前网站的名称。 – Eugeny

+0

我从其他网站上读到关于Rewritecond的内容,所以我添加了以下RewriteCond%{HTTP_HOST}!^ cp。[NC] RewriteCond%{HTTP_HOST} ^(。+?)\。example.com $ [NC] RewriteRule^listing /([a-z0-9] +)$ listing /?action = $ 1 [L,NC]但它一直给我404错误 –