How WordPress permalink system works? - wordpress

I am a php developer and i am currently developing a CMS/blog system. i want to add a permalink system like WordPress. i am curious about how WordPress parse permalink. For example, how to get data like id and post name from:
example.com/id/123/post/example/
In short, I want a system to get id and post name from the url. I want to enable users to change the permalink structure like WordPress using tags like:
id/%postid%/post/%postname%/
How do I get data in variables like $id and $post_name where values will 123 and example? Please help me.
Thanks in advance.

The commonly available apache module mod_rewrite can help you out with this. What you do is write rewrite rules inside an .htaccess file, and through the rewrite, fancy structures that would have normally resembled a file system get sent to a PHP file as $_GET parameters.
For example, if you wanted to replace something like: ?reactor=13 into /reactor/13/
Write this rewrite rule in the .htaccess file:
RewriteEngine on
RewriteRule reactor/([0-9]+)/$ index.php?id=$1
Now, if instead of index.php you pull up /reactor/13/, your index.php file should see this in $_GET:
Array
(
[id] => 13
)
Now, for your HTML code, it's up to you to craft URLs and obey your thought-out structure.
These rewrite rules can be confusing, but they follow a logical regex pattern.
WordPress takes a stronger approach than inserting these editing .htaccess files, to where they send everything to WP, and then WP solves / routes the rest through internal rules.

This depends on what server software you're using, but if your planning on using your scripts on Apache it, usually, involves using something called mod_rewrite and specifically a .htaccess file.
I would recommend starting by reading a tutorial on this subject. As usual, Google is your friend.

Related

Wordpress "permalinks" under the hood

this may be too general question to ask, but how does wordpress permalink work?
clearly they dont work with htacces only what it seems?!
there is that page where a "template tree" is listed
"single.php"-url is like that: "https://example.com/thisisanarticle"
"page.php"-url is like that: "https://example.com/apage"
so how does wordpress know what to call? both urls share the same structure after all.
Under the hood, WordPress uses their WP_Rewrite mechanism to create/update entries in Apache's .htaccess file whenever permalinks are added/removed/updated.
The class and its implementation in the WordPress core use a set of rules to query the WordPress database and write the rules into .htaccess each time an update is determined to be required.

How do I remove a word from a url in Wordpress using .htaccess?

In the site I'm building there is a post-type called "Focus". Since they need to url to be like www.mysite.com/focus/page-post-name I have the post type slug as "focus".
Of course the archive page is www.mysite.com/focus this is causing an issue since I need a specific page to be an archive page. Metaboxes are used to build and add settings to pages. I can't just create a page called "Focus" since that is reserved by the post-type.
My idea is to create a page called "Our Focus" resulting a URL of www.mysite.com/our-focus.
I'd like to just remove the "our-" from that url to make it appear that the archive is being used.
I've tried setting has_archive to false but that just reverts to using the archive.php page.
I'm mobile and working without access to a test server, so forgive any errors.
It sounds like you need mod_rewrite, it should be packaged with apache, but may be off. Typically you'll have to check the mods_enabled directory in your apache conf directory, or search your conf file for rewrite.
You'll need two lines in your htaccess at least.
RewriteEngine on
RewriteRule ^/focus/?$ /our-focus/
First line is required to enable rewrite.
The second line is the rule declaration, a regex to match, and a destination. The regex should only match focus, and direct you accordingly. Wordpress may take issue, but I think this will work.
Docs for reference
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
You may need RewriteBase depending on setup. RewriteBase is typically used if there are virtualHosts or a different base url then the server expects.

wordpress permalinks with prefix for wildcard

I'm having trouble trying to program into Wordpress a wildcard for the permalink structure. So for example, I'm trying to setup a permalink where the link is like this: foo.example.com/company. At the moment, the wildcard is in place for the URL (foo.example.com), but the permalinks are still under the root (example.com/company). Is there a way to program it so that the permalink will be matched exactly to the URL that is written. So if I entered in dsdd.example.com, the permalinks will display dsdd.example.com/company, etcc...
This kind of thing is way over my head, so if someone could provide some code examples of how to accomplish this, it would be much appreciated. Thank you.
I'm not sure what your current setup is, but I can offer advice based on my prior experience with web hosts and WordPress.
If you want to have something where the subdomain (something.example.com) is a wildcard (thiscouldbeanything.example.com) and still point to your WordPress installation, you're going to have to get your host to set up something called a Wildcard DNS Record
From your description, you might have this set up already. If so, cool.
Using the wildcard is another story. Personally, I would go with a .htaccess file, and use mod_rewrite in order to have it use the wildcard subdomain to be used in the URL.
WordPress MU (Multi User) uses something along these lines to help you set up a subdomain network, and you can probably use their .htaccess file demonstrated here
Without REALLY specific information about your setup and goals, I couldn't be as much help as I wanted to be, but good luck!

How to tackle existing url's when moving from Joomla to Drupal site

I've a Joomla site but I want to move it to Drupal-6 site. How should I go about changing existing url's? Should I add the redirection in .htacces or use any redirect modules?
One main reason of moving is the forum module in Drupal which I'd like to use.
I'd be using nodealias & global redirect modules.
In my site I'm using SH404SEF to generate urls(with .html suffix).
You can absolutely place a redirect in the .htaccess file and reference the pathauto generated path.
http://drupal.org/project/pathauto generates URL aliases based on title etc. and then put something like:
RewriteRule ^misc/about.html http://www.example.com/aboutus/ [R=301,L]
for each URL. (This is better off in your apache httpd.conf file for performance reasons.)
Or there is a module:
http://drupal.org/project/path_redirect
Which looks like it does just what you want but I've not tried it.

How to rewrite part of the url with .htaccess?

I'm not that good with .htaccess, so I wonder, how can I rewrite default 'node' part of the url to an 'article' for example, so that server will output 'article/1' instead of 'node/1'. I need this to be done on a server side, so with the use of .htaccess only. Any guess?
The Pathauto module will allow you to set customised paths like this in Drupal. It's a must-have for Drupal (should be in Core, really). Just install and configure via the easy web-interface and you'll be all set.
You can even have different patterns for different node types. As an example, if you had 'page' and 'news' content types, your pages could have URLs like http://example.com/about-us while your news items could have urls like http://example.com/news/2009/05/28/news-post-title.
Well, the first problem is that you don't understand what mod_rewrite (which is what's doing the work you want, not .htaccess) does. It doesn't change the HTML you're outputting; it changes how the server interprets incoming URL requests.
So if your Drupal install understands node/1 and you want people to see article/1, first you need to modify the HTML output so it embeds links to article/1. Then you rewrite from article/1 to node/1 like so, in a .htaccess or Apache config file:
RewriteEngine on
RewriteRule article/ node/ [NS]

Resources