Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
$50
Need WP Rewrite Rules working code example
A rewrite rule that will parse the requested page URL and do the following with it:
given URL format: site_url()/post-name/some/additional/bunch/of/stuff/
The keyword in this case would be "stuff" and should be assigned to a constant 'KEYWORD'.
The actual post to rewrite to, would be site_url()/post-name/ but it is vital that the URL be rewritten, NOT redirected. The address bar must reflect the full requested URL, complete with any custom query string.
So in the address bar I would see something possibly like:
www.domain.com/blog/post-name/some/additional/bunch/of/stuff/?var1=red&var2=blue
but internally WP would be pulling content from the DB associated with 'post-name'.
Oh forgot one important point, it actually should work with whatever the current permalink rules are, so really the url format would be:
site_url()/%permalink_structure%/some/additional/bunch/of/stuff/
Thanks everyone!
EDIT:
This article seems to be talking about something similar to what I need:
http://betterwp.net/98-wordpress-create-fake-pages/
Jonathan van Clute | 10/09/11 at 10:42pm
Edit
Previous versions of this question:
10/10/11 at 1:54am
| 10/10/11 at 1:54am
| 10/10/11 at 3:33am
| 10/10/11 at 3:59am
| 10/10/11 at 12:01pm
(5) Possible Answers Submitted...
Note: Jonathan van Clute felt their question was unanswered, so we granted them a refund.
Note: Jonathan van Clute requested a refund. They offered this explanation:
"While I found the ensuing discussion interesting, I came to wpquestions.com specifically for the statement made by the founder:"When I am facing a tight deadline, I do not want to spend 4 hours looking up information on Google - I'd rather just pay someone to tell me the answer."
The discussion provided was not much different than what I get at Stackexchange... statements that I shouldn't do what I want the way I want to, and rough code snippets that don't do what I need but instead send me off to do precisely what this site is meant to avoid - googling for hours.
If the award offered is not enough for an expert to feel like writing the code, then I feel they should either not say anything (why waste your valuable time?!) or let the asker know what would be an acceptable price. Had anyone done this, I most likely would have paid it to get what I needed quickly. Instead I wasted several days and frankly, got better non-specific information from SE without any possible expectation of pay.
I like the idea behind this site, but when someone specifically states their needs are working code that solves their problem, I feel experts should refrain from adding to the discussion if they are not going to ultimately provide what was requested.
Perhaps I'll have "better luck next time.""
If no one challenges a refund request, then they are automatically granted and proccessed after 48 hours. Admins of this site only review refund requests if someone challenges the request. If you are curious about how we handled previous refund requests, you may read over all refund requests and their challenges.
See a chronological view of answers?
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
-

Last edited:
10/10/11
12:18amDylan Kuhn says:The parse_query action is the earliest action I know of after is_archive is set for the global query. pre_get_posts is a popular one too, still early enough to redirect if you need to. At init, since the query hasn't been parsed yet, is_archive() will always return false.
- 10/10/11 1:09am
Jonathan van Clute says:Thanks, I wish it were that easy. I have a seeming paradox. I need to manipulate the SERVER_URI before WP has done all its internal rewriting and such, and it looks like that means I can't do my manipulations any later than 'init'. But I also need to detect if the visitor is on an archive page BEFORE I finish my manipulations, and is_archive can't work until after init. Catch-22.
Any other ideas?
- 10/10/11 1:09am
-

Last edited:
10/10/11
2:27amBob Keen says:Jonathan,
You can check if a URI is associated with a specific post/page as early as "setup_theme".
The key is to use the WP function "url_to_postid()", which will check a URL and return a post->ID if it matches a permalink.
More on this here: http://codex.wordpress.org/Function_Reference/url_to_postid
If the URL doesn't resolve to a page/post, you'll get a 0 back.
If you get a valid post->ID back (non-zero), it will be a page/post.
If you get a 0, however, you'll want to begin a short process of elimination, such as URI which contain ".php" (i.e. wp-login.php) as well as any specialties on your, and arrive at the determination as to whether you're dealing with an is_archive() or not.
Best,
Bob- 10/10/11 2:42am
Jonathan van Clute says:Thanks Bob, that's basically what I'm already doing. The problem is my plugin is rather unusual, and does a whole bunch of custom rewriting so that I can insert any keyword I want at the end of the URL and various things can then be done with it.
So if I have an initial permalink that looks like this:
www.domain.com/blog/my-post/
it might look like this after adding the keyword "dogs":
www.domain.com/blog/my-post/dogs/
Of course, there is no such page no matter what, as no DB entry is ever created. So I am already looping through and parsing down the requested URI piece by piece, checking url_to_postid along the way to get to the base or "real" post, and then changing SERVER_URI to be that URI.
All that works fine... but the big issue is categories or archives. For instance a category URL might look like this:
www.domain.com/blog/category/
But how can I tell if this is a category request, or if "category" is the keyword chosen for the blog root page? I must positively identify /category/ as a category page (using is_archive() ideally) in order to not treat it as a keyword.
So I can't go off of url_to_postid values, because in this situation both the keyword page AND a category page will always return 0 for the postid anyway.
Hope you see my dilemma and perhaps have some more ideas.
Thanks again! - 10/10/11 3:17am
Bob Keen says:I don't know how you're doing you rewrites, but you can try to hook into "sanitize_comment_cookie" - which is before $wp_rewrite has been initiated. You may get some ideas moving from there, based on the data you're getting.
You can also look for clues through the categories in the taxonomy table and see if you get hits on relevant portions of the URI that would indicate that you're dealing with a parent or child category.
- 10/10/11 2:42am
-

Last edited:
10/10/11
3:10amJohn Cotton says:I would have thought that you are on very dodgy ground changing the SERVER_URI value. Clearly I don't know your plugin, but I can't think of any reason why you'd want to do it other than to fool WordPress into thinking that a different page had been requested.
If that's what you're trying to do, then you are doing it the wrong way.
You should being adding custom rewrite rules (using the 'rewrite_rules_array' action), probably creating your own query vars (using the 'query_vars' action) in order to neatly pass through to the destination page or template redirect the extra info you need to define the page switch and then, finally, using the 'template_redirect' action (if necessary) as the correct place to redirect WordPress to the desired end point.
I say 'if necessary' since you may well be able to create a rewrite rule that takes you there directly.
Your problem of "how can I tell if it's a category or a keyword" is the age old one of uniqueness and, if you don't allow yourself unque URLs you're on a hiding to nothing.
Why not change your url to www.domain.com/blog/my-post/keywords/dogs/ where keywords (or whatever) is reliably unique allowing you to construct a decent regex e.g.
www.domain.com/(.*)/keywords/(.*)
JC
- 10/10/11 3:26am
Jonathan van Clute says:You're exactly right that I'm trying to "fool" WP into loading a different page than what's seen in the URL. It was a far simpler approach than getting into rewrite rules - at least until archives came into play.
I don't want to designate a "delimiter" word in my URLs, it's important to me that they simply follow the format of /post-name/some/possible/keywords/ where "keywords" is the desired target keyword, since it is the last item in the url.
I'm still quite new (a few months) to developing on WP, so all the various hooks, filters, actions, etc. tend to throw me for a loop. Could you possibly supply some sample code for creating a rewrite to accomplish the above? It sounds like this would give me more flexibility in terms of what point in the logic to execute things, which would enable me to use more conditionals such as is_archive and others.
I'm going to increase the award for this question and update it with my additional requirements. Thanks! - 10/10/11 3:51am
John Cotton says:This the basic structure:
function template_redirect() {
if( get_query_var( 'something_special' ) == 1 ) {
// Do whatever you want!
// Test get_query_var( 'lead' ) and get_query_var( 'follow' ) to find your matches and then off you go
exit();
}
}
add_action('template_redirect','eg_template_redirect');
function eg_add_rewrite_rules( $rules ) {
$newrules = array();
$newrules['^(.*)/marker/(.*)$'] = 'index.php?something_special=1&lead=$matches[1]&follow=$matches[2]';
return $newrules + $rules;
}
add_action('rewrite_rules_array', 'eg_add_rewrite_rules');
function eg_add_query_vars( $vars ) {
array_push($vars, 'something_special', 'lead', 'follow');
return $vars;
}
add_action('query_vars', 'eg_add_query_vars');
but it's crucial that you have some marker in the url otherwise it's impossible to write a rule that won't match every (and thus stop WordPress doing it's work).
What reason don't you want a marker word - SEO? - 10/10/11 4:05am
Jonathan van Clute says:Yes, SEO is a primary reason, but also so that datafeed (csv data usually) driven sites can be easily created simply by adding the product keyword one wishes to pull a page for, right in the URL. For instance:
www.domain.com/blog/some-post/sports/athletic-wear/golf-shoes/
This would match to my datafeed file for the term "golf shoes" and all the other stuff is merely there to give the appearance of a careful heirarchy. I already have this code working provided I feed it a keyword. In this way I can quickly construct URLs for all manner of things without having to do anything more than add something to the URL.
Looking over the code you posted, I have to admit to not following it really at all. It looks as if it parses the URL for something (the marker in your example) and then rewrites behind the scenes to some query string variables? Is that right? - 10/10/11 4:39am
John Cotton says:The search engines aren't going to fuss about an extra entry in here, trust me:
www.domain.com/blog/some-post/--/sports/athletic-wear/golf-shoes/
As you can see, it can be whatever you want, it just needs to be unique.
The code works from the bottom up:
eg_add_query_vars: WordPress needs to know about your new query variables so that it doesn't return a 404 (it will do that in the header even if you successfully return content!)
eg_add_rewrite_rules: We need to add some rules for your special urls. These get added to the top of the list of rules that WP checks so it's important that there is no chance of them matching an existing rule. So, for your standard /%category%/%postname%/ permalink, the rule is this:
$newrules["(.+?)/([^/]+)$"]= "index.php?category_name=$matches[1]&name=$matches[2]";
Unfortunately, that would also match your sample link which is why you need to add a marker....
$newrules["(.+?)/([^/]+)/k/([^/]+)$"]= "index.php?category_name=$matches[1]&name=$matches[2]&keywords=$matches[3]";
Can adjust your rewrites based on the current permalink? Absolutely, the global $wp_rewrite->permalink_structure tells you want the shape is and you can then adjust accordingly.
eg_template_redirect: (beware, the function is my first response is wrongly name!!) This is your chance to tell WordPress to go somewhere other than where it expects. So, for your CSV output, you could have this:
function eg_template_redirect() {
if( !empty( get_query_var('keywords')) {
include('template-csv-output.php');
exit();
}
}
add_action('template_redirect', 'eg_template_redirect');
which would send WP to a file called template-csv-output.php where you do whatever it is you do to output CSV.
Does that help?
- 10/10/11 11:19am
Jonathan van Clute says:I'm afraid the requirement of not having a delimiter is absolute. It's not really about the search engines, it's simply how I need it to function.
Could perhaps the post name itself be a delimiter?
Also redirecting WP to a file to manage certain output is not acceptable. The URL format must be as I outlined, and not redirect to something like 'template-csv-output.php' in order to process a csv.
Thanks anyway, but unless you can think of a way to do exactly as I need, I'm afraid this won't do. - 10/10/11 11:27am
John Cotton says:It's not really about the search engines, it's simply how I need it to function.
Could perhaps the post name itself be a delimiter?
You can try.....I'm not clear about why you don't want something else in the URL. If I was I might be able to suggest something.
For example if "sports" is always there, then that will do. Equally, if it's sports or games or toys (ie a defined list) then that could be made to work too.
Also redirecting WP to a file to manage certain output is not acceptable.
You've misunderstood. The URL stays the same, it's WordPress that redirects itself.
It always does that, redirecting to page.php, or single.php or archive.php depending on the page requested and the files in your theme. In this case, you can have it nicely sent to a clean PHP file to output the feed with no header or footer calls.
- 10/10/11 11:46am
Jonathan van Clute says:OK so it's not a redirection it's a rewrite. When I saw "redirect" in the code, I assumed it was a real redirect.
What I'm doing is converting an existing non-WP application, to a WP plugin. This means that It must function the same way the old one did, I can't suddenly change the basic way the URLs are constructed by requiring a delimiter. There will never be anything that is guaranteed to always be present in the URL.
If it helps, here is the htaccess rewrite rules I've been using for years (on the non_WP version):
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [S=44]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [S=44]
RewriteRule ^(.*)$ index.php?k=$1&%{QUERY_STRING}
So this is the behavior I need to recreate within WP. - 10/10/11 12:01pm
Jonathan van Clute says:This article seems to be talking about something similar to what I need:
http://betterwp.net/98-wordpress-create-fake-pages/ - 10/10/11 4:03pm
John Cotton says:This article seems to be talking about something similar to what I need:
It does...because it's talking about doing exactly what I have described to you ;)
RewriteRule ^(.*)$ index.php?k=$1&%{QUERY_STRING}
The above line is what's making your previous code work and there's nothing to stop you having an identical rule in your custom rewrites.
You can then process the url in template redirect as I described.
However, if you do IT WILL PICK UP JUST ABOUT EVERY SINGLE URL.
So you will have to do all the legwork that WordPress does for you already because none of the wp_query settings will be set (since your rewrite rule will have come in before any of the others).
To quote Luis:
you are trying to solve the problem the hardest possible way.
Is your list of keywords (or at least the first one of them at the "sports" position") really that infinite that you can't predict it? I can't understand how you can have a page where anything can be passed in...
Even it's 50, or 100 or 250 long, you can still create restricted, identifiable regexs.
- 10/10/11 4:12pm
Jonathan van Clute says:The "sports" example I gave was just a completely arbitrary random example.
The way my non-WP version works is that it DOES in fact have a delimiter of sorts, like this:
www.domain.com/script/template-name/some/list/of/keywords/
so "template-name" becomes the delimiter. This is why I asked about using the post-name itself as the delimiter, as it would accomplish the identical result I believe.
This script is intended to be used for affiliate and other marketing purposes, where there could be an absolutely limitless possible range of keywords, products, virtual page structures, etc. in use so I mustn't place any restrictions on how people structure their URLs, other than minimally so I can identify their "template" or post, as described above.
I can tell these suggestions are on the right track, I just haven't been able to make them actually work in practice. What I'm looking for from this question and for the award, is working code that will accomplish what I need.
Thanks! - 10/10/11 4:18pm
Jonathan van Clute says:Sorry, it wouldn't be using post-name as the delimiter, it would be using %/permalink%/ since the permalinks could be something other than %postname%
- 10/10/11 4:56pm
John Cotton says:Can you guarantee that category name will always be a single folder (ie not hierarchical)?
- 10/10/11 5:21pm
Jonathan van Clute says:Unfortunately no, I can't ever guarantee what permalink structure might be in use, or how the categories of a specific site might be set up.
- 10/10/11 5:26pm
John Cotton says:In which case, it'll cost more than $50 for me to write the code.
What you want needs some careful thought as, in essence, a whole series of scenarios have to be catered for in the code and that isn't a 30 minute job. There's potential for a performance nightmare as the url needs stepping along, directory by directory, finding the category. - 10/10/11 6:00pm
Jonathan van Clute says:Hmmm... if I'm understanding you, you're suggesting that stepping through the URL to find the active category is a problem?
Unless I'm missing something, that's probably one of the easier parts and I've already done it in my previous approach. I did a simple backwards for loop, examining each part of the URL as it went... something like this:
$uriparts = array_filter(explode("/",URI_PATH));
for($i=count(array_filter($uriparts)+1;$i>0;$i--){
foreach($uriparts as $part){
$permalink .= '/' . $part;
}
echo( "\n<br>this postid = ".url_to_postid($permalink));
}
Haven't seen any performance problems at all. But maybe I'm not interpreting your concern correctly? Would be slightly different of course as this is only checking url_to_postid and not looking for a category, but the same principal holds I would think...? - 10/10/11 6:08pm
John Cotton says:You can't use url_to_postid...it checks the value against the rewrite rules!
That's what I was trying to tell you - by using a rewrite rule that matches most URLs, you have to stop using wordpress to do the work and do it yourself. Basically, it's a customised version of url_to_postid that needs writing. - 10/10/11 7:07pm
Jonathan van Clute says:I wasn't suggesting using url_to_posid, was merely illustrating a simple way to loop through the URL elements. What to compare them against is another issue obviously.
A "custom version of url_to_postid" would do what I need. url_to_archive() would have solved my whole problem... if it could be done.
- 10/11/11 6:15am
John Cotton says:url_to_archive() would have solved my whole problem... if it could be done
Anything can be done, it's just a question of cost - both of time to do it and resources used to run it on a request.
As I've said, I'm happy to code something up but it's more than $50. However, I've given you all the code structure, you just need to expand on what you've got from your last version - as you say, there are a series of tests that you can apply to each url and you will be able to determine what page is being requests and what the keywords are with a set of conditional statements.
- 10/11/11 11:33am
Jonathan van Clute says:OK then, what am I looking at then in terms of cost for you to code this? That's what I'm looking for. Your commentary has been interesting and appreciated, but that's not why I'm here... I can get that at Stackexchange or plenty of other places. I'm here because I hoped to be provided with some working code for a reasonable fee. At this point I'm tired of messing with it, and am willing to pay for the code to be written if it's within my budget.
- 10/10/11 3:26am
-

Last edited:
10/10/11
9:22amAbdessamad Idrissi says:Did u have a look at Some background on rewrite rules?
Use this regex to rewrite the URL
.*?/(\w+)/.*?/(\w+)/$
Input :
www.example.com/first_folder/some/additional/bunch/of/folders/last_folder/
Output :
$1 prints first_folder
$2 prints last_folderPrevious versions of this answer: 10/10/11 at 9:22am
- 10/10/11 11:48am
Jonathan van Clute says:Thanks for the link, I read that article but was not able to follow it. Rewriting and regex makes my head spin. I copy/pasted the sample code (as well as other examples I've found in my researching) and tried working with it, but couldn't even replicate their results before altering it to suit my needs.
- 10/10/11 11:48am
-

Last edited:
10/10/11
12:05pmLuis Cordova says:I honestly think you are trying to solve the problem the hardest possible way. If your routing problem gets overly complex and you really need flexibility to debug, troubleshoot, and modify it over time, I fully recommend integrating it with a routing component like symfony2's routing component which works standalone as well. This is a complex task at first but it will pay off. It will definitely solve many of your problems and let you hook a great framework into your app. I could be wrong though but better than all if this mess keeps going on and on.
Previous versions of this answer: 10/10/11 at 12:05pm
This question has expired.
Current status of this question: Refunded
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
