Hi Ben,
Sorry, I was not picking on you. I do get frustrated with members asking the same questions day after day, week after week, month after month and year after year. I got so tired of answering the same questions all the time that I wrote the mod_rewrite tutorial linked in my signature (and copied to the stickies you bypassed on entry) and it does no good. No, it’s not you, it’s everyone. Okay, I give up and ask, too, when I can’t find something I am looking for right away - this IS the age of instant gratification!
Okay, now on to comment on your post.
Just because something is used by someone else (or in your CMS) doesn’t mean it’s a good thing. The CMS people (okay, WordPress in particular) have written their mod_rewrite to prevent 500 errors (when mod_rewrite isn’t enabled) and it assumes that it must capture EVERYTHING and redirect to index.php. Well, it works for their system but … the idea that I try to impress on members is that you DON’T want to use code just because it’s available. PLEASE understand the code you’re using!
Options +FollowSymLinks should already be in the httpd.conf. If it’s not, it’s fine to add it.
The Last flag is a (minor) pet peeve as noobies tend to ignore it and don’t understand why the second RewriteRule (and subsequent RewriteRules) in a series is never matched and redirected. It’s seemingly minor but IS a big deal.
The start and end anchors can probably be omitted in certain circumstances (because (.*) is greedy).
To understand the (.*), you must understand
-
That it will match NOTHING or EVERYTHING
-
mod_rewrite loops back through making successive passes through the set of mod_rewrite statements until no match/redirection is made. That causes it to re-process a RewriteRule with (.*) … so there MUST be a way to escape the match/redirection! (Apache 1.x was very unforgiving on this point as it would continue to loop until it had to be shut down and restarted (that’s why most hosts refused to allow you to write to .htaccess or failed to enable mod_rewrite). Apache 2.x will only allow 10 loops before stopping mod_rewrite.) You can avoid these problems by:
a. Replacing the (.*) with regex code which will match what you want to redirect (see below)
OR
b. Using a RewriteCond to prevent a match of the redirection’s {REQUEST_URI}.
<IfModule> is the CMS protecting noobie webmasters from 500 errors. It’s a silly noobie mistake to leave it in but understandable if the noob doesn’t know what it’s there for. Chalk that up to a learning experience. :tup:
MultiViews is another noobie buggaboo as it will match just the filename (without an extension) in the path and redirect to that file … before getting to mod_rewrite! IMHO, it’s a major PITA and should be avoided. However, for completeness, I mentioned that because of your redirection (index.php/{whatever}). It’s a personal thing that, because I can hide the redirection with mod_rewrite, I prefer to use query strings which are easier for my PHP scripts to parse (they don’t have to parse the {REQUEST_URI} string, the key/value pairs are in the $_GET [and $_REQUEST] array).
RewriteBase is designed to “undo” the effect of a mod_alias redirect. If you have none of those (that you need to “undo”), why use the thing? Wait until you have a RewriteBase /path statement to wrap your head around while you’re struggling with why a RewriteRule won’t work.
The users/home/whatever URI is the sign of a corrupt Apache - it needs to be restarted as should NEVER display the physical path to your files. It should NEVER display anything in the path outside your webspace!
Actually, I jumped out of the PHP forum (which is why I’d joined years ago) for some help with mod_rewrite. I quickly learned from the master (pippo) and actually asked why one of his recommendations was wrong (I do that, too, on occasion). When pippo went on holiday, I stayed around to answer mod_rewrite questions and … well, I’m still here. Yes, I get tired (ergo the perceived “attitude”) of answering the same questions (as above) but try to convert that into a learning experience (I do get annoyed at the “script kiddies” that want me to code something for them) for all members.
The regex used by mod_rewrite is a subset of Apache’s regex engine - primarily because mod_rewrite is a very simple language. If you’ve every parsed an online page to pick out pieces of the page’s text (to pop into a scroller, for instance), then you know that you can’t merely find the start <p> and (.) then specify an end point as </p> as the (.) will grab everything on the page from the first <p> to the last </p>. The “trick” is to use regex to specify exactly what you will accept rather than use the “lazy regex” of the (.), e.g., ‘~<p id=“some_id”>([a-zA-Z .]+)</p>~’. Even (.?) is better than (.*) … but you already knew that.
And the answer is yes. However, I take the time to understand the code that others are using (CMS, the other posts here, etc.) as you know. Asking questions about the meaning of what’s not understood is my way of learning. My way of teaching is to make members think so they’ll remember the process the next time. No offense was intended and I am sorry that offense was taken.
If you have any questions about the tutorial - or anything else - please fire away. I’m here as a volunteer to help members learn so they don’t have to ask/pay for someone else to do it for them.
Regards,
DK