How does wordpress do it?

14 replies
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
#wordpress
  • Profile picture of the author Abledragon
    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.
    Signature
    WealthyDragon - Earning My Living Online
    {{ DiscussionBoard.errors[2752440].message }}
    • Profile picture of the author Stephen Dean
      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
      Signature
      Free Coaching WSO: How to finish all your 2013 "Goals" in JANUARY with my proven productivity secrets - taken from 9 years working as a freelance copywriter. Click Here

      Occupation: Best Copywriter Ever.
      Clients:
      Matt Bacak, Jim Edwards, Ryan Deiss and more.
      {{ DiscussionBoard.errors[2752483].message }}
  • Profile picture of the author chapdaddy
    do some googling on 'php mod_rewrite'
    {{ DiscussionBoard.errors[2753062].message }}
  • Profile picture of the author theIMgeek
    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
    Signature
    FREE WSO: Protect and Automatically Deliver Your Digital Products

    Ask the Internet Marketing Geek
    <-- Happy to help with technical challenges
    MiniSiteMaker.org <-- Free software to make your mini-sites fast and easy
    {{ DiscussionBoard.errors[2753069].message }}
  • Profile picture of the author Stephen Dean
    Perfect guys, exactly what I needed. Will probably take me a bit but I'll figure it out.

    Thanks!

    Stephen
    Signature
    Free Coaching WSO: How to finish all your 2013 "Goals" in JANUARY with my proven productivity secrets - taken from 9 years working as a freelance copywriter. Click Here

    Occupation: Best Copywriter Ever.
    Clients:
    Matt Bacak, Jim Edwards, Ryan Deiss and more.
    {{ DiscussionBoard.errors[2753242].message }}
    • Profile picture of the author NetMan
      Originally Posted by Stephen Dean View Post

      Perfect guys, exactly what I needed. Will probably take me a bit but I'll figure it out.

      Thanks!

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

      Best regards,

      Andre Foisy
      {{ DiscussionBoard.errors[2755182].message }}
      • Profile picture of the author Stephen Dean
        Thanks guys.

        I do need it to be my own script as it's not a CMS.

        My server must be APACHE as I have gotten the mod-rewrite to work, just not quite perfectly yet.

        And for Regular Expressions, I think what I'm doing is simple enough that it'll be easy.

        I'm just trying to make this url

        http://blog.com/example

        equal to

        http://blog.com/?e=example

        In my tests last night it looked like I could just use

        RewriteRule ^(.+)$ index.php?e=$1

        That didn't quite work for me, as when I tried to GET the value of "e" all that outputted was "index.php" Not sure why...

        Thanks again,
        Stephen Dean
        Signature
        Free Coaching WSO: How to finish all your 2013 "Goals" in JANUARY with my proven productivity secrets - taken from 9 years working as a freelance copywriter. Click Here

        Occupation: Best Copywriter Ever.
        Clients:
        Matt Bacak, Jim Edwards, Ryan Deiss and more.
        {{ DiscussionBoard.errors[2755383].message }}
        • Profile picture of the author NetMan
          Originally Posted by Stephen Dean View Post


          http://blog.com/example

          equal to

          http://blog.com/?e=example

          In my tests last night it looked like I could just use

          RewriteRule ^(.+)$ index.php?e=$1

          That didn't quite work for me, as when I tried to GET the value of "e" all that outputted was "index.php" Not sure why...

          Thanks again,
          Stephen Dean
          Well that is very likely because the original link structure in your PHP program MUST be based on index.php?e=$1 URLs must result with string range... if not it will not work of course.

          You should probably need to define
          RewriteEngine On
          RewriteBase ()
          RewriteCond ()

          As well

          Andre
          {{ DiscussionBoard.errors[2755787].message }}
          • Profile picture of the author Stephen Dean
            Thanks Ryan, tried it and it hasn't fixed it yet.

            And I'm not sure what you mean Andre. But I've set up a test to make it as simple as possible to figure out. Here's what I'm doing.

            I created a directory "a" so I'm working within blog.com/a/

            Inside "a" I've just put two files. .htaccess and index.php

            .htaccess contains only this right now:

            Code:
            RewriteEngine On
            RewriteRule ^(.+)$ index.php?e=$1 [NC,L]
            and index.php simply uses GET to assign "e" to variable $e. And then it uses echo to print $e. I tried copying and pasting the code in to this post but it wouldn't accept it.

            I assume when I get it to work that typing in this url

            will result in a page that simply outputs

            "success"
            Right now all that outputs is "index.php"

            Cheers!
            Stephen Dean
            Signature
            Free Coaching WSO: How to finish all your 2013 "Goals" in JANUARY with my proven productivity secrets - taken from 9 years working as a freelance copywriter. Click Here

            Occupation: Best Copywriter Ever.
            Clients:
            Matt Bacak, Jim Edwards, Ryan Deiss and more.
            {{ DiscussionBoard.errors[2755905].message }}
  • Profile picture of the author SteveJohnson
    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.
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[2754859].message }}
  • Profile picture of the author sparckyz
    With a few clicks wordpress is nice enough to do it all for you
    {{ DiscussionBoard.errors[2755157].message }}
  • Profile picture of the author theIMgeek
    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
    Signature
    FREE WSO: Protect and Automatically Deliver Your Digital Products

    Ask the Internet Marketing Geek
    <-- Happy to help with technical challenges
    MiniSiteMaker.org <-- Free software to make your mini-sites fast and easy
    {{ DiscussionBoard.errors[2755525].message }}
  • Profile picture of the author theIMgeek
    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
    Signature
    FREE WSO: Protect and Automatically Deliver Your Digital Products

    Ask the Internet Marketing Geek
    <-- Happy to help with technical challenges
    MiniSiteMaker.org <-- Free software to make your mini-sites fast and easy
    {{ DiscussionBoard.errors[2755974].message }}
  • Profile picture of the author Stephen Dean
    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
    Signature
    Free Coaching WSO: How to finish all your 2013 "Goals" in JANUARY with my proven productivity secrets - taken from 9 years working as a freelance copywriter. Click Here

    Occupation: Best Copywriter Ever.
    Clients:
    Matt Bacak, Jim Edwards, Ryan Deiss and more.
    {{ DiscussionBoard.errors[2756085].message }}

Trending Topics