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…


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 www.MySite.com/articles/postage-meters-can-save-you-money

2.) The Mod_Rewrite uses this Regular Expression…

RewriteRule articles/([a-zA-Z0-9_-]+)$ article.php?title=$1

…to convert the URL above into this format…

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

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

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?

  1. 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?

  1. 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

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

Regards,

DK