Htaccess mod rewrite help

Hey everyone,
I have this in my htaccess file. It helps me convert my pages from page.php?id=4 to page.php/page4

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ./home/path/mysite.com/template.php [L]

It works fine,

But when i need this to work in other directories it doesn’t.
like:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ./home/path/mysite.com/folder1/folder2/template.php [L]

Any help would be great. Thanks!

I got this to accomplish what I want, I would love some feed back though as to way the first code I posted didn’t work.

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ template.php [L,QSA]

In your first post, your rewrite rules… don’t have any rules. Just urls. ??

In your second post, you’re sending everything to template.php. That makes no sense. How can every single page on your site be “template.php?”?

And yes, you’re sending EVERYTHING to template.php with the (.*) (means everything and nothing). Prolly looks like it works because beforehand you have the rewriteCond checking for not file or directory.

Give us a full url (with example.com for domain name) of what you want the links to look like
and another full url of the actual, real url of the file you want users to reach.

You want your rewrite rules to be as specific as possible and still work. Yours now is much too general and in danger of looping.

^(.*)$ matched “template.php” you know.

But I’ll give it a try because I’m still so fresh and green that it’s still thrilling to try : )
If it was only
page.php?id=4 to page.php/page4
then it would be
RewriteRule ^page\.php\/page([\d]+)$ page.php?id=$1 [L]

I think.

Sp,

Correct - no regex in the RewriteRules AND the links were to PHYSICAL files (they MUST be to website files, i.e., accessible via http protocol)!

Sending EVERYTHING (except files and directories which exist) to ONE script is EXACTLY what WordPress does. In other words, not only does it make sense (with the file and directory exclusions), it WORKS! That’s why, with the file exclusion, it’s okay for (.*) to match template.php as the NOT file exclusion is already false (prevents looping).

A couple of problems with this:

  1. For the OP to use a filename in place of a directory, Options MultiViews must be enabled. That’s an UGLY thing (and makes the redirect unnecessary if page.php can read the “filename” part of the URI - which it can).

  2. Do NOT escape the / in the regex.

Otherwise, fine. Personally, I’d use page/{pageid} to keep to what the OP asked for OR merely use {page_title} and redirect that to the page script changing the id to the title key (and search the db for title rather than id) - more about that in my signature’s tutorial with an example at http://wilderness-wally.com where almost ALL his links are handled by a script which fetches the articles by title.

js,

Your first code didn’t work for the reasons specified above (no regex and physical links vs http links).

Options +FollowSymLinks 
RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ template.php [L,QSA]

Well, that’s the typical WP mod_rewrite except for the useless QSA (the query string is not affected by the redirection so the QSA flag is not needed). However, please note that it’s left to template.php to parse the {REQUEST_FILENAME} to determine how to handle the request.

A pedantic complaint of mine, though, is that, if you’re NOT going to use $1, DON’T create it with the EVERYTHING atom when .? will work just as well (and faster).

RewriteRule .? template.php [L,QSA]

Regards,

DK

Sending EVERYTHING (except files and directories which exist) to ONE script is EXACTLY what WordPress does. In other words, not only does it make sense (with the file and directory exclusions), it WORKS! That’s why, with the file exclusion, it’s okay for (.*) to match template.php as the NOT file exclusion is already false (prevents looping).

Is this because of the particular script in the back of WordPress or can anyone use this to send anything not a file or dir to a script with .?/.* without worrying about looping?

Also I’m confused about multiviews– if the file you’re looking for doesn’t exist, apache looks for anything starting with the name you asked for… (though I remember by husband saying he used it to ask for different language-versions of pages)

For the OP to use a filename in place of a directory, Options MultiViews must be enabled.

do you mean because if page.php is a file then page.php/page4 is something Apache has to figure out because page.php isn’t a dir?

Sp,

Regards,

DK