How does wordpress do it?

by 14 replies
17
So I've got a problem I need to figure out. Hopefully the answer is in php.

You know how with Wordpress, posts can end up looking like this...

http://www.blog.com/post/

...even though "post/" isn't really a directory on blog.com?

How do they do that? I'd like to swap using "blog.com/?var=something" at the end of a url with "blog.com/something". Is that possible?

Thanks for any help I can get.

Cheers,
Stephen Dean
#programming #wordpress
  • Hi Stephen,

    The various permalink formats can be adjusted or customised in the Setting>Permalinks screen in your Admin area.

    If it's the trailing / you're wanting to remove I believe there's a plugin that can do this. However, the trailing / appears on all WordPress permalink formats and doesn't have any negative effect on SEO (if that's what you're concerned about).

    Cheers,

    Martin.
    • [1] reply
    • Thanks for your help Martin. Unfortunately my original post wasn't as clear as I meant it to be. I'm not trying to mess with Wordpress or permalinks, more I'm wondering how WP does it?

      How does wordpress show a page at "blog.com/post" when there is no such directory as post?

      I want to apply this to my own php script. It seems that data is being passed after the trailing / and I'd like to do the same thing with my script.

      Anyone know how wordpress does it?

      Thanks,
      Stephen Dean
  • do some googling on 'php mod_rewrite'
    • [ 1 ] Thanks
  • Stephen, the dirty little secret is called "Rewrite Rules".

    They are most commonly configured in a file named .htaccess (as in the case of WP) located in the root folder. Every URL request will first look for that file to see how it should proceed.

    Basically, it can turn a "pretty" URL like domain.com/folder/section/page into an actual request for domain.com/index.php?folder=folder&section=section&page=page (or really just about anything you want it to do)

    Now, setting up these rewrite rules can be a bit tricky, as you're dealing with "regular expressions" to essentially find and replace text. The formatting is a challenge, and even the slightest mistake will cause your whole site to be inaccessible until it gets fixed. So be careful if you're tinkering with a live site.

    -Ryan
    • [ 1 ] Thanks
  • Perfect guys, exactly what I needed. Will probably take me a bit but I'll figure it out.

    Thanks!

    Stephen
    • [1] reply
    • However, for that .htaccess rewrite rules option to work, web server (http server) MUST be Apache web server.

      Best regards,

      Andre Foisy
      • [1] reply
  • Banned
    [DELETED]
  • WordPress does all of its redirecting internally. The .htaccess rewrite rules only look for existing files or directories then, if none are found, redirects to the WP index file.

    If you can, take a look at the options table in a WordPress install, and look through the rewrite functions. WP maintains a 'rewrite_rules' option that contains the different rules. It's a pretty good example of how to use reg expressions in rewrites.
  • With a few clicks wordpress is nice enough to do it all for you
  • I'm just running out the door, Stephen, but here's a quick thought.

    Try this:

    Code:
    RewriteRule ^(.+)$ /index.php?e=$1 [NC,L]
    -Ryan
    • [ 1 ] Thanks
  • Ah-HA! (head slapping moment)
    The trouble is that the rewrite creates a bit of a loop, so it's sort of reading itself for the variable... hence "index.php".

    Adding some lines to your htaccess will prevent the rewrite rule from happening when there is an actual physical file that matches the URL. (this is code used by Wordpress as well)

    Code:
    RewriteEngine On
    
    # if the file exists, hop to it
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    
    # Send all "fake" URLs to the index.php for processing
    RewriteRule ^(.+)$ index.php?e=$1 [NC,L]
    -Ryan
    • [ 1 ] Thanks
  • Genius Ryan, that would have taken me ages to troubleshoot and I probably would have given up. Thanks for your help, works perfect now. I owe you big.

    Hope to see you at my blog and let me know if I can be of help with anything.

    Cheers,
    Stephen Dean

Next Topics on Trending Feed