Wordpress custom Querystrings & Pretty URL's - How? - php

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.

Related

Whats the best way to change the content urls in the WordPress Rest API results?

Say I have api.wordpresssite.com where I will be entering data, uploading images and so on. Then saw that I want to consume the WordPress API on another site like mysite.com.
WordPress will then assume that every link in content will be api.wordpresssite.com It will also embed images with the same URL because the links and media are absolute.
Am I supposed to process the content on mysite.com looking for links but ignoring media or is there a plugin or function that I can add to my theme to do this?
I have tried changing the base URL and while it works for links, it breaks media uploads.
There are multiple endpoints so I am thinking that even if there was a function to add, it would be too far down the line of execution to do anything.
For Example, there is the WP API, as well as JetPack, and Yoast that I am using.
There are tonnes of articles on "How to use WordPress in Laravel" but not a single article has talked about how to "normalize" the content for the site that is consuming it.
The API feels like it's only true out of the box use is to be used with some kind Javascript based front end.
What I would like is a headless WordPress API with relative URLs for content links.
I was looking for the same answer and solved it this way:
What you would need to do is set the site URL to www.remote-domain.com.
You can do this by going to WP Admin > Settings > General
Screenshot
Then on your functions.php file or somewhere you can add a filter (plugin etc.) add this filter in.
add_filter( 'rest_url', 'fix_rest_url');
function fix_rest_url( $url ) {
return $url;
}
Got the answer from: https://core.trac.wordpress.org/ticket/49146
My suspicion that no one really uses the WordPress API seems to be confirmed. Mainly on an external site.
I had to create some render methods that looked for HREF links and remap them on the content, and menus.
It still seems silly and not very polished.

Is it possible to make referrals like example.com/username on WordPress?

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.

Angular routing under wordpress site

I have a WordPress site where one of the pages that WordPress serve is an SPA based on AngularJS. The Angular-app uses html5mode so that I can browse to http://localhost/79133/71 and I'm still showing index.html. Now I want to deploy this to my WordPress site so that one of the pages under WordPress (the main page) is my SPA. However, if I browse to http://example.com/79133/71 WordPress will try to find the page with the permalink 79133/71 and won't understand that the main page should be served. How can i configure WordPress so that these new "routes" will be pointing to the first page?
I'm not an expert on WordPress, but in order to get html5mode to work in angular:
Any route starting with /your_spa needs to serve the main page (index.html) of your app.
You must ensure that any resources referenced in your spa (for example, /assets/themes/theme.css) needs to point to the correct location, even if your location url is your_spa/your/spa/page. This means no relative paths unless you use the base tag.
Here's a tutorial on how to configure the router in wordpress. I don't use wordpress so I'm sorry that I
It seems like a lot of trouble, and I would just use the default hash routes if possible.
I had the same basic issue, and solved it. Your situation is slightly different but I think you need something like:
function spa_rewrite() {
add_rewrite_rule('79133/71', '/', 'top');
}
add_action('init', 'spa_rewrite');
In practise you'll need to use some regex to make this more flexible

ExpressionEngine custom url routing

From what I understand, Expression Engine url parsing works like this:
site/template/
I want to deeplink straight off the regular site url, like this:
site/special-deep-link
and I have javascript in place to carry out specific actions.
But EE is just assuming this is a template, and giving me a 404 saying it does not exist (which is true). So naturally the js does not fire and nothing works.
Is there any way to stop EE from trying to parse it as a template without using .htaccess or other server side config settings?
Unfortunately the old codeigniter way of rule routing doesn't seem to be working.
I tried:
$route['site/special-deep-link'] ='';
$route['/special-deep-link'] ='';
$route['index/special-deep-link'] ='';
But sadly none of those seem to work. Any thoughts on this?
Figured it out, using this plugin:
http://devot-ee.com/add-ons/freebie
I was able to have EE ignore that url string.

joomla fully customize component link

I made a component and now I want to customize the link for it.
I already wrote the router.php.
The link to the component view (if I use the JRoute function of course) now looks good, except for the first part, which looks like :
http://www.example.com/en/component/componentname/...
don't mind the "en", it's there cause I use JomFish to manage more languages on my site.
I want to transform "/component/componentname/" to "/myString/" for example.
I can't use the menu for this, cause my site displays info from a database, linking this many sites to my menu is impossible.
The only solution I found requires the changing of the Joomla JRoute function (I found only a suggestion, not how it's done :( ).
What about using mod_rewrite to rewrite your url?
Basically you put the rewrite rules into your .htaccess file and Apache will handle the rest.
There is a great guide that teaches you all about it on http://www.htmlist.com/how-to/a-simplemod_rewrite-tutorial/

Categories