SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Understanding how my Mod_Rewrite works

    I have a Mod_Rewrite that I created this past Fall, and I want to make sure that I understand how things actually work?!

    Here is my code...
    Code:
    RewriteEngine on
    
    #PRETTY:		articles/postage-meters-can-save-you-money
    #UGLY:			article.php?title=postage-meters-can-save-you-money
    
    RewriteRule articles/([a-zA-Z0-9_-]+)$ article.php?title=$1

    Let me explain how I *think* things work...

    1.) A user clicks on a link http://www.MySite.com/articles/posta...save-you-money

    2.) The Mod_Rewrite uses this Regular Expression...
    Code:
    RewriteRule articles/([a-zA-Z0-9_-]+)$ article.php?title=$1
    ...to convert the URL above into this format...
    Code:
    www.MySite.com/article.php?title=postage-meters-can-save-you-money
    The key point being that the user always sees the "Pretty URL" but my browser and PHP script will actually be working with the "Ugly URL".

    Is that correct??


    Debbie

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    DD,

    You've pretty much got it but please allow me to make a few comments for others:

    1. Which - in [a-zA-Z0-9_-] represents a hyphen and which one(s) don't? Because - is a metacharacter within a character range definition, it's supposed to be place first so there will be no confusion ... but last, as you have it, works, too.

    2. Since you didn't show anything else in your .htaccess file, I'll assume that this is it ... nothing more. If this is not the case, then the lack of a Last flag will force mod_rewrite to AND any subsequent RewriteRule (blocks) with this one - or this one with preceding blocks. Please learn to use the [L] as you would ; or } in PHP and/or JavaScript to avoid problems.

    3. Because your redirection was not "absolute," it was not redirected to www.MySite.com/yadda-yadda, merely an internal redirection to the RELATIVE link (article.php in the same directory as the .htaccess file). This is a minor point ... unless you have an atypical setup.

    4. As for your final point, without an external absolute redirection or a 301 redirection, you are correct that the original request will continue to be displayed. If you change directory levels with your redirection, though, this will be problematic as the browsers will believe they're in the original directory (i.e., wrong depth so relative links will be offset). Because you're using articles as a pseudo directory, this has likely been the cause of missing support files (unless you use absolute links or the HTML <base> tag). This is discussed in more detail in my signature's tutorial.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    DD,

    You've pretty much got it but please allow me to make a few comments for others:

    2. Since you didn't show anything else in your .htaccess file, I'll assume that this is it ... nothing more. If this is not the case, then the lack of a Last flag will force mod_rewrite to AND any subsequent RewriteRule (blocks) with this one - or this one with preceding blocks. Please learn to use the [L] as you would ; or } in PHP and/or JavaScript to avoid problems.
    Think you are missing some words there.

    So I need a Last Flag?

    How should that be written?

    And since I don't have one, what are the ramifications?


    3. Because your redirection was not "absolute," it was not redirected to www.MySite.com/yadda-yadda, merely an internal redirection to the RELATIVE link (article.php in the same directory as the .htaccess file). This is a minor point ... unless you have an atypical setup.
    Again, you are dropping words, so I don't understand...

    Yes, I used a relative reference. (I thought that was better?!) What are you saying I should or should not do?


    4. As for your final point, without an external absolute redirection or a 301 redirection, you are correct that the original request will continue to be displayed. If you change directory levels with your redirection, though, this will be problematic as the browsers will believe they're in the original directory (i.e., wrong depth so relative links will be offset). Because you're using articles as a pseudo directory, this has likely been the cause of missing support files (unless you use absolute links or the HTML <base> tag). This is discussed in more detail in my signature's tutorial.

    Regards,

    DK
    Not sure if I follow you.

    I have a "Config" file which defines my DOCUMENT_ROOT and BASE_URL, so that should take care of any confusions as t where things point/exist. I also usually try and use absolute references in HTML so that links/pictures/etc don't get messed up.

    If I follow you, then I should be okay there.

    Thanks,


    Debbie

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    So I need a Last Flag?
    YES! That's both good programming practice or REQUIRED if this is not the only directive in your .htaccess.
    How should that be written?
    [L]
    And since I don't have one, what are the ramifications?
    As above, any mod_rewrite code following the directive you showed would be dependent upon this directive matching BECAUSE any following code is automatically ANDed with it unless you use the [OR] flag (which is not appropriate) or the Last flag ([L]) which is appropriate.
    Again, you are dropping words, so I don't understand...
    You stated that the full URL was the redirection of your code. It is not. Your code makes an internal redirection which is what you want (an external redirection would force the new URL to be displayed).
    Yes, I used a relative reference. (I thought that was better?!) What are you saying I should or should not do?
    I was saying that you should use (internal) absolute links to your supporting files, not relative links, e.g., /css/mycss.css where the leading / makes it absolute (with respect to your DocumentRoot - external absolute would be http://www.example.com/css/mycss.css which is okay but too much typing for me).
    Not sure if I follow you.
    Your question was whether or not the NEW URI would be shown with your code - it would not because (1) you did not use an external absolute redirection (the http:// bit) AND you did not use R=301 as a flag which would display the new URI/URL. That was a confirmation of your interpretation that, with your code, the new URL would not be displayed.
    I have a "Config" file which defines my DOCUMENT_ROOT and BASE_URL, so that should take care of any confusions as t where things point/exist. I also usually try and use absolute references in HTML so that links/pictures/etc don't get messed up.
    Both are irrelevant to the mod_rewrite questions - except that a RewriteBase directive is designed ONLY to undo a mod_alias redirection (i.e., Redirect {code} {target} {absolute redirection}). If you don't have anything like that to undo, you do not have any use for RewriteBase and, therefore, should not be using that code.
    If I follow you, then I should be okay there.
    Only if you follow these clarifications.
    Thanks,

    Debbie
    You're welcome. That was a good chance for me to explain some mod_rewrite intricacies for everyone.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •