Greetings.

You know how some sites provide custom URL paths like www.myspace.com/fred or www.flubber.com/sally.

The "fred" and the "sally" text appears as a Path in the URL, but I doubt it exists as a real folder in their file structure.

My uninformed guess is that the URL "path" is passed to the host, where it is parsed in some way, where it in turn is used as a variable that points to the right database record.

Anyway...

I heat up my coffee and start looking for how to do something like this.

I look at the PHP getenv, parse_url and other commands. I try out some ideas on my own. All without any luck or good results to report.

So, now I post this as a question to the group.

I desire the user to type in a very simple URL such as www.mysite.com/fred. There is actually no "/fred" path in the host file structure, but the text "fred" is instead passed to PHP as a variable.

Of course, I could easily implement something like having the user type in www.mysite.com?name=fred, but you see, my users (and I) are lazy and forgetfull, and being able to type a simple www.mysite.com/fred would be desired.

There are two aspects of this issue:

Issue#1) Since there is actually no file path ".../fred" on the server, how do I prevent a "page can not be found". Hmmmm.. perhaps I can use htaccess and redirect bad addresses to a common page?

Issue#2) Passing the trailing URL path (eg: "fred") to PHP.

Any thoughts, suggestions or pointers?

    I also found an alternative to Apache mod_rewrite at:

    http://www.tutorio.com/tutorial/php-alternative-to-mod-rewrite-for-se-friendly-urls

    That page includes some camparison scoop as shown below. Any comments, opinion or suggestions out there?

    Flexibility
    Mod_rewrite is much more flexible than the php method, but whats fexible to some is complicated to others

    SEO
    I think that both are pretty much equal. The only problem with the php method is the urls sometimes become very long. Still this is more of a user friendliesness problem than a SEO problem

    Sever Resources
    This is where the php method really shines, specially for larger sites. Because doing mod_rewrite on sites with a lot of pages takes up a large amount of resources. This is also the reason why tutorio.com uses the php method for our urls.

      The idea in the first response was good, however I do not have direct access to the apache server itself.

      The alternate idea in the second response sounds good on the surface, however it does not seem to work (at least my poor old fumble fingers can't make it work).

      Here is my current status:

      Recall, I desire to implement a friendly, elegant URL address solution like www.mysite.com/fred to pass a parameter, instead of an unfriendly URL address like www.mysite.com/name.php?name=fred

      So, following the alternate method, I create a .htaccess file:

      <FilesMatch "something$">
      ForceType application/x-httpd-php
      </FilesMatch>

      I also create a test file named something.php file as shown:

      <?
      $data = explode("/",$HTTP_SERVER_VARS['PATH_INFO']);
      $page = $data[1];
      $sub1 = $data[2];
      $sub2 = $data[3];
      $sub3 = $data[4];
      $sub4 = $data[5];
      echo '<br>page = ', $page;
      echo '<br>sub1 = ', $sub1;
      echo '<br>sub2 = ', $sub2;
      echo '<br>sub3 = ', $sub3;
      echo '<br>sub4 = ', $sub4;
      ?>

      PS... my other htaccess files I've used in the past for other purposes have worked just fine, so I think my web host does indeed allow this.

      I expected the URL www.mysite.com/something/fred to echo something like:

      page = fred

      But all I get is that annoying, common "page not found" error.

      FYI when I type a more full URL www.mysite.com/something.php/fred , I do indeed get:

      page = fred

      Ooooooof. I will continue to poke around.

      Meanwhile... if anyone has similar experiences, good ideas or other suggestions, I would greatly appreciate it.

        Go with mod_rewrite (that's what it's there for). If your web server is unable to tell from the URL what it's supposed to use to handle the request, then it's going to have to handle every request in the same way - PHP, whether it's needed or not.

        Anyway, here's a bit in the tutorial you linked to

        If you look closely you will spot something strange with the resultant url
        [url]http://www.mod_rewrite.dom/something.php/page/12/category/hosting[/url]

        The .php extension right in the middle of the url is a problem, some search engines may recognize this as improper format and wont index your site. And if you leave it out the server wont recognize that it is a php file, that is ofcourse unless you tell the server to treat the file as a .php file even if its missing the extension. So you want the url to look like this
        [url]http://www.mod_rewrite.dom/something/page/12/category/hosting[/url]

        To do this you need to create a .htaccess file (blank text file named .htaccess). And insert the following code

        <FilesMatch "^something$"> 
          <  ForceType application/x-httpd-php >
        </FilesMatch> 
        

        First the files match checks for the word 'something' in the url, then the force type directive tells Apache to recognize 'something' as a php file even though there is no extension in the url.

        In short, "oops, this method isn't good enough to do a decent job without using the web server to make the decisions anyway".

        Because doing mod_rewrite on sites with a lot of pages takes up a large amount of resources.

        Somehow I suspect that PHP is more resource-intensive than mod_rewrite. But I don't have the numbers (and tellingly, I don't see any here).

        The fact that I can use mod_rewrite to resolve requests for resources that don't require PHP is just a bonus.

          5 days later

          Here is my status so far...

          I agree that the mod_rewrite is the correct solution.
          While I pursue that, here is what I'm doing.

          Recall, my desire is to be able to use a friendly URL such as
          www.domain.com/fred
          and have it rewritten to something like
          www.domain.com/index.php?person=fred

          By the way, this started out as a PHP Coding issue, but has migrated away from that, so it might belong in another catagory. But I will continue.

          In the .htaccess file, I use many variants of the following:

          # Example for a rewrite rule:
          # (turns http://domain.com/xyz.html into http://domain.com/index.php?xyz)
          RewriteEngine on
          RewriteBase /
          RewriteRule ^([a-z]+)\.html$ /index.php?person=$1 [R,L]

          ... but my poor old brain just can't come up with the correct syntax to do what I'm looking for.

          I'm sure there are some good suggestions out there. Any ideas?

            Hi yaea04,

            I was pondering about almost the same issue and thought I would post a question here. Then I noticed your recent post so I'm going to join in!

            I think you are on the right path in getting it to work. I have myself been able to do it...but I'm wondering about a slightly different issue.

            Well, first to see if I can help you!

            I think your first problem relates to the URL you are trying to access (sorry my tech talk is a little limited).

            Instead of ([a-z]+).html$ you might try just ([a-zA-Z]+)$ as you are not trying to translate a html file when entering yourdomain.com/fred

            The other problem might be that you have already specified RewriteBase as "/" and then telling Apache that your source file for "/index.php?person=$1" also begins with a forward slash "/".

            Those are my thoughts anyways...This is my first day with mod_rewrite.

            TO MY QUESTION:

            I have dynamically generated content and would also like to have a first level category rewritten/translated. I've seen many people do rewrites such as:

            /modules/content/about/14/ and then they use say "14" to pull the info out of the database which isn't that difficult...and fairly easy to match...

            BUT say my index.php?catid=14 is located in modules/content/ and I want to rewrite the URL as mydomain.com/about

            How do I regex that without having to include a number in the URL such as:

            mydomain.com/about14

            Of course I could change the index.php?catid=14 to query on title like:

            index.php?cattitle=about

            which would give me the right content.

            But there are so many possibilities to regex for if I make a rule for any alphabetic characters directly in the root.

            So my rewrite rule would look something like???:

            RewriteRule ([a-z]+)$ modules/content/index.php?cattitle=$1 [L]

            I also thought about adding each such rule to .htaccess with php fwrite...

            What do you think?

              10 days later

              Here is my status so far. Thanks to the previous poster for the tip.

              My htaccess now file looks like:

              # turns http://www.mydomain.com/fred  into  http://www.mydomain.com/person.php?uid=fred
              RewriteEngine on
              RewriteRule ^([a-zA-Z]+)$ /person.php?uid=$1 [R,L]

              This seems to work, although more testing on my part is in order.

              The other question about parsing:

              I'm also looking at something similar, and found this reference about exploding the url into components: http://www.tutorio.com/tutorial/php-alternative-to-mod-rewrite-for-se-friendly-urls

              http://www.tutorio.com/tutorial/php-alternative-to-mod-rewrite-for-se-friendly-urls

              That might or might not be what you were talking about.

              Meanwhile, I continue, and will report back later.

                Write a Reply...