A couple of months ago, I built this lead dashboard (*) in pure PHP.
A key feature of my lead dashboard is that it's very flexible in its URL parsing. Parameters can be added to the end of the URL and the order or quantity really doesn't matter. See examples below.
I'm currently porting my dashboard to a Wordpress system and would like to maintain this same flexibility, but it's not really clear to me what would be a good strategy in Wordpress to implement this routing technique. Should I go for an .htaccess based solution? Should I add a filter? Should I add an action? And which filter or action would seem the most suitable?
Any suggestions on how to do this the right way?
Example URL 1 :
http://www.johnslegers.com/lead-dashboard/
Example URL 2 :
http://www.johnslegers.com/lead-dashboard/keyword:Stackoverflow
Example URL 3 :
http://www.johnslegers.com/lead-dashboard/keyword:Stackoverflow/language:English
Example URL 4 :
http://www.johnslegers.com/lead-dashboard/language:English,Dutch/keyword:Stackoverflow,problem
Example URL 5 :
http://www.johnslegers.com/lead-dashboard/value:3500/percentage:5,15,30,50,70/language:English,French,German/keyword:Stackoverflow,problem
Example URL 6 :
http://www.johnslegers.com/lead-dashboard/keyword:Stackoverflow,%20programming,%20code,%20Wordpress,%20problem/language:English/currency:US%20Dollars/percentage:5,10,20,50,85/cost:0.9,9,34,108/value:5400
EDIT :
(*) Google decided to impose an RMF policy that requires any dev to implement a long list of features of they want to use their Adwords API. Because my app uses only a few features of Adwords and thus doesn't comply with this policy, Google no longer allows me to access their API. This means that the tool no longer functions correctly and cannot be fixed unless Google decides to change their policy. Because the tool no longer functions correctly, I removed the link.
I ended up solving this issue myself.
To fix my problem, I just had to add the following action function to the functions.php file of my theme :
function simulationpage_init() {
// Remember to flush the rules once manually after you added this code!
add_rewrite_rule(
// The regex to match the incoming URL
'simulatie/.*',
// The resulting internal URL
'index.php?pagename=simulatie&data=$matches[1]',
// This is a rather specific URL, so we add it to the top of the list
'top');
flush_rewrite_rules();
}
add_action('init', 'simulationpage_init');
That achieved exactly what I wanted!
Related
Some time ago I needed to create an AMP version in a website. The website has no the default Wordpress structure. Had many custom posts, many custom taxonomies, custom archives and, to create an AMP endpoint was very hard. But, it worked.
Now, I have to do the same thing, but in 2 other websites. First in an existent wordpress website and other with a custom CMS.
I did not want to change the URL structure because the two sites have a large structure.
The most easy way is to add a query string parameter.
Instead of writing the URL this way: www.website.com/page-permalink-here/amp, I would like to writing this way: www.website.com/page-permalink-here/?amp=1
In Wordpress case, I just have to identify the query string.
If has $_GET['amp'] && $_GET['amp'] == 1, I load the AMP theme version. If false, loads the regular theme folder.
Wordpress has the function wp_is_mobile(). If wp_is_mobile() == true and $_GET['amp'] && $_GET['amp'] == 1 returns false, I can redirect the current URL, adding the ?amp=1 in the URL.
I can do this in reverse as well. If the URL has amp=1 but wp_is_mobile() == false, I can redirect the URL without the ?amp=1.
In the other website (custom CMS), I would like to do the same.
But, all references suggested to use the subfolder /amp/ as AMP endpoint. Is wrong OR is not a good practice to use the endpoint as query string?
I appreciate all thoughts! Tks!
I had the same question and now I use with parameter.
My thought was about a litte from SEO view.
With "/amp" is a deeper subfolder
incoming links that stripped parameter goes to one site (see tip down)
social links are goes to the site without the parameter, to the cannonical url.
Inside my CMS I have one document for both and I can check easy Querystring for stuff that need difference for AMP
But both with paramter or /amp both works fine. Look what is easy to handle for you.
Tip: If you use parameter, don't forget to setup the parameter in the search console (Webmaster tools)
We want to have as much simple as possible referral links to our site for users. The best to have something like: example.com/username
But in this case we need to invent some way to make all other links working in WordPress. The idea how to implement it using mod_rewrite should look like:
If no any parameters provided - rewrite to / and process index.php
If parameter provided represented in site structure with file or directory - rewrite to example.com/parameter and show the content from site.
If link contain /s/ (example.com/s/something) then it's our post, which we should translate to something which allow us to see a post.
If one parameter provided - rewrite to example.com/parameter and process example.com/index.php?ref=parameter
Question is: Will it work for all plugins (at least woocommerce and some other) or is it possible to implement at all ? I really don't know how permalink and other wordpress part is working.
Thank you for your ideas.
Our current website is built using AngularJS and as a result has URLs with /#/in them such as http://www.website.com/#/termsofuse
Short-sightedly, the URL for terms or use has been hard coded into a mobile application and although this has been changed, some users still have older versions.
We're moving to a WordPress site (running on Azure) and the new URL for terms of use is http://www.website.com/termsofuse
The issue is that I want to redirect to the new URL if the old URL is used (from the app where it is hard coded in older versions) but I can't find a way to do this with the /#/ in the URL (otherwise I could use HTML/JS at the old URL to redirect).
I have tried searching for solutions on Google and here but although I'm sure someone has had this problem, I'm finding it hard to define the search terms in order to get valuable results.
I also considered posting this on WordPress stackexchange but it is not really a WordPress question, I'm assuming I'll need to use some other method.
Appreciate any ideas or advice. Thanks in advance.
So far I have learnt from responses that I probably need a JS solution and based on that I have found the below which looks similar (at least shows me how to isolate the fragment after the URL). Since my issue is very specific, and I only need to look for the specific fragment #/termsofuse could I use this code (with midifications) to look for that string and redirect based on that?
Checking URL fragment for a keyword
Sadly, this is not possible as everything after the # doesn't get sent to the server.
What many people do in this sort of situation is to use a javascript/ajax solution to load the page.
By your description, as your new web site is built on WordPress without AngularJs.
So it hardly could approach your need in a traditional way. As hashtag # is a client symbol which is never passed on serve, so IIS on Azure will not get the portion after # of the URL, also URL rewrite module won’t see it too.
So if possible to modify home page of your WordPress site, here is a workaround with using JavaScript get the portion after # of the URL and redirect to the right URL.
Here is the code snippet:
(function () {
console.log(window.location.href);
var url = window.location.href;
params = url.split('#');
console.log(params);
if(params[1]){
window.location.href = params[1];
}
})();
I have a perfectly good (so far) setup of wordpress of for a new website. The pretty urls are working as expected.
I have 1 dynamic page that loads content depending on the querystring:
/dynamic/?loc=england&code=uk
I wish to make this URL "pretty" as well but every-time I modify the .htaccess no changes are made. Tried everything and googled everything else - last resort. If I could get the following to work then I would be happy.
I need to make the URL look like.
/dynamic/location/england/code/uk/
Adding this to any .htaccess breaks the whole website.
RewriteRule /dynamic/(.*)/(.*)/(.*)/(.*)/$ /dynamic?$1=$2&$3=$4
What am i missing.
Thanks in advance
N
Don't add the rewrite rule to your .htaccess file. WordPress manages that file for you, so try to use built-in features whenever you can.
WordPress actually has a somewhat advanced rewrite engine that ships standard - and it's pluggable just like the rest of the platform.
The trick, though, is working with it. You'll need to register your RegEx so WordPress knows what kinds of strings to match (i.e dynamic/location/(.*)/code/(.*) => /dynamic?$loc=$1&code=$2). Then you'll need to set up the page and script on the back end to handle the submission.
For a similar example, look at the answer I received to parsing custom URLs with a custom post type over on WordPress Answers. Extending this, you'll need to set up code similar to the following (note: untested!!!):
<?php
add_action('init', 'add_my_rewrite');
function add_my_rewrite() {
global $wp_rewrite;
$wp_rewrite->add_rule('location/([^/]+)/code/([^/]+)','index.php?loc=$matches[1]&code=$matches[2]','top');
$wp_rewrite->flush_rules(false); // This should really be done in a plugin activation
}
This should add the rewrite structure to your .htaccess file automatically.
I’m a recent user of Codeigniter and am developing a simple backend CMS to manage pages.
Based on a URL (in this example I have hidden “index.php”) : mysite.com/pagename
I would like the system to detect if there is a value of “pagename” in my database, if there is, I need the system to re-route to a custom controller (eg: Pagemaker) and if there is no record called pagename, just do it’s normal thing (i.e. find a controller called pagename)
Currently I have:
$route['(:any)'] = "pagemaker/create/$1";
whereby all requests are forwarded to my custom function.
However I want to change this structure so that if the page does NOT exist in the db, the traditional codeigniter request process is followed.
Can anyone offer any advice about how to complete this? Or any advice about routing custom CMS’s in codeigniter in general?
The best solution is to upgrade to CI 2.0 because it's stable enough and it gives you plenty of useful features.
In your case, set the following route:
$route['404_override'] = 'pagemaker';
If the router doesn't know where to go it just goes to pagemaker controller. This can then check if the first uri segment exists and if not you create a custom 404 page instead of the crappy default one.
And I don't want to hear any of this "Oh but it's not released yet" crap, I've been using it CI 2.0 for almost a year. ;-)
I can think of two possibilities:
1) Edit your custom function to let it redirect your client when page's not in the db
pseudo code:
if($dbresult == null){
redirect("http://yoursite.com/"+$this->uri->segment(3));
}
2) Edit the router class of CI so it will first check if the page's in the db and if not, just continues. This may be somewhat messier as you need a db connection in your Router.php