Can Mod_Rewrite do this?

Ive been trying to make this happen and I think mod_rewrite might be able to do this, but I’m not sure how to dynamically change all .php requests. Nor do I know if this is a good idea.

Basically it would be really tedius to make rules for every single page, every time I add a controller.


But it really loads

Yet stays on the <pagename>.php url

I don’t know if this works

Yes, that’s exactly what mod_rewrite does.

But might I suggest adding a front controller so that you can add more logic in the future if necessary? Say you want to handle pagename.rss in the future to call a different controller that generates RSS feeds.

Rewrite all requests that aren’t to actual files to index.php (or any name), and index.php looks at the REQUEST_URI and includes system/applicaton/controllers/pagename.php

Rewrite rule:

RewriteCond &#37;{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

basic index.php:

$pagename = substr($_SERVER['REQUEST_URI'], 1, strlen($_SERVER['REQUEST_URI']) -
include("system/application/controllers/" . $pagename . ".php");

The URL in the address bar remains pagename.php

Wow that is very clever, I like that with the index php file.
Thanks for the fast reply I wasnt prepared for that on this topic :smiley:

Awesome Ill play around with this for a while, thanks!


Dan is correct, that’s exactly what mod_rewrite does.

However, what he showed you is the WP code to force the index.php script to handle EVERYTHING. That is NOT mod_rewrite but an effective use of PHP in the “404’d” request (WP’s code, which Dan showed you, is quite effective as a 404 handler without using Apache’s inbuilt ErrorDocument 404 {path/file}.

Unfortunately, it’s NOT what you were asking for: A simple mod_rewrite to perform redirections.

First, you should NOT be using mod_rewrite for that (as a general rule) because mod_alias’s Redirect is designed to do that even faster.

Second, I use something similar to what you are asking for (in the DocumentRoot, though) at my website (for extensionless URIs) with:

RewriteEngine on
RewriteRule ^([a-z]+)$ $1.php [L]

All you would need to do is stick your system/application/controllers/ in front of the $1 in the redirection to have the same thing. Just be sure that the script knows it’s three levels deeper into your website as relative links will be predicated on the DocumentRoot level of the {REQUEST_URI}.



Hey Thanks, Ill try that too, Ive been pickling with it a bit.Im getting this error: Forbidden
You don’t have permission to access /fk2/system/application/controllers/index.php on this server.

My .htaccess
RewriteEngine On
RewriteRule ^([a-z]+)$ system/application/controllers/$1.php [L]

I don’t have a .htaccess inside the controllers area, and there is a index.php file.
But I don’t think that should matter, ill try it anyways though

### EDIT ###
When I do this it allows it to work, is this how i should keep it?

.htaccess in /controllers/
Order Allow,Deny
Allow from All

[COLOR=Black]I don’t want to have to edit apache.conf (i think thats the file) since this will be distributable and it’s gotta stay simple :stuck_out_tongue:

For some reason I have a hard time with the apache stuff, I still haven’t really GOT IT yet haha, even after playing with it quite a bit in the past.

How come this doesn’t change when submit is clicked?
<form action=“login.php” method=“post”>
“The requested URL /fk2/login.php was not found on this server.”

I am only guessing i need a %REQUEST type of thing in the htaccess, I tried this without any luck

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]+)$ system/application/controllers/$1.php [L]

Okay this works better for me :slight_smile:

[B]RewriteRule ^([a-z.-]+)$ system/application/controllers/$1 [L]

[/B]Then I can do – page.php?query=val&string=X
which is how i prefer :stuck_out_tongue:


If there is a directory protection in any of the directories in the path, you SHOULD receive that message!

As for your order code, that’s associated with a <Directory> or <File> block, it should NOT be standalone!

Your login.php relative link will be to the same directory level as indicated by the browser’s location, i.e., fk2. That’s where your .htaccess is located, isn’t it? Your “how come” is that login (NOT login.php) would have been redirected to system/application/controllers. When you go play with directory levels, you’ve GOT to remember what you’re doing with these funky links! The same thing goes with your page.php in the third post.

Finally, yes, the -d and -f tests are advisable - if you can have your “application” serving files you’re not in control of (in other people’s file systems). I.e., not advised for your case.



THanks a lot!
Haha Im having a good old time tweaking all this stuff I almost got it, now its up to some PHP stuff only :stuck_out_tongue: