简体   繁体   English

将URL重写规则添加到Wordpress

[英]Add URL Rewrite Rule To Wordpress

I've written a Wordpress plugin that adds a query string to the URL. 我写了一个Wordpress插件,它将一个查询字符串添加到URL。 However I can't seem to modify the htaccess to rewrite this. 但是我似乎无法修改htaccess来重写它。 Not sure if Wordpress is overriding it? 不确定Wordpress是否覆盖它?

The current htaccess is: 目前的htaccess是:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

And the URL I'm trying to rewrite is: 我正在尝试重写的URL是:

http://domain.com/deal-info/?id=87&post_name=testdealtitle

Desired URL: 所需网址:

http://domain.com/deal-info/87/testdealtitle

I've tried adding: 我试过添加:

RewriteRule ^([^/]*)/([^/]*)$ /deal-info/?id=$1&post_name=$2 [L]

to no avail. 无济于事。 Any ideas appreciated! 任何想法赞赏!

When you do this: 当你这样做:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

This means: "if it's not a file, and if it's not a dir, redirect all incoming URLs to index.php . 这意味着:“如果它不是文件,如果它不是dir,则将所有传入的URL重定向到index.php

Your rewriterule should work... unless you've put it after the previous rewriterule. 您的重写规则应该工作...除非你把它前面的重写规则之后 So, in short, are you sure your .htaccess it like that: 所以,简而言之,你确定你的.htaccess是这样的:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^([^/]*)/([^/]*)$ /deal-info/?id=$1&post_name=$2 [L]
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

Please tell me if it works. 请告诉我它是否有效。


Moreover if you only want to redirect only deal-info, you should do something like (not tested): 此外,如果你只想要重定向成交-信息,你应该像做(未测试):

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^(/)?deal-info/([^/]*)/([^/]*)$ /deal-info/?id=$2&post_name=$3 [L]
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

Two hints: 两个提示:


Please try to use the RewriteLog directive: it helps you to track down such problems: 请尝试使用RewriteLog指令:它可以帮助您跟踪此类问题:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

My favorite tool to check for regexp: 我最喜欢检查regexp的工具:

http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!) http://www.quanetic.com/Regex (别忘了选择ereg(POSIX)而不是preg(PCRE)!)

May I ask you to add the rewritelog in your question? 我可以请你在你的问题中添加重写吗?

After much confusion I found an answer here: https://wordpress.stackexchange.com/questions/5413/need-help-with-add-rewrite-rule 经过多次困惑后,我在这里找到了答案: https//wordpress.stackexchange.com/questions/5413/need-help-with-add-rewrite-rule

Though I think the proper way to do it is by: http://codex.wordpress.org/Rewrite_API/add_rewrite_rule 虽然我认为正确的方法是: http//codex.wordpress.org/Rewrite_API/add_rewrite_rule

I'm not sure if you can do this directly in htaccess, or I was having a conflict problem with another plugin I was using. 我不确定你是否可以直接在htaccess中执行此操作,或者我在使用另一个插件时遇到冲突问题。

Thanks again to Olivier for his extensive answer! 再次感谢Olivier的广泛回答!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM