Forum Moderators: phranque

Message Too Old, No Replies

Apache2.2 mod rewrite not rewriting

         

eZACKe

2:47 pm on Feb 7, 2012 (gmt 0)

10+ Year Member



Not sure why this is not working. I've checked in httpd.conf and I have this line:
LoadModule rewrite_module modules/mod_rewrite.so

And when I do phpinfo(), I see mod_rewrite in the loaded modules box.

My directory structure: I'm using Apache, so there's a htdocs directory. Inside the htdocs directory I have myProject directory. This is the root of my site. Within the my Project directory I have all the files and folders that make up my site. Sitting right in this "root" myProject directory I created a .htaccess file (making sure that it doesn't have a .txt type).

Here's the contents of this file:


RewriteEngine On


RewriteBase /

RewriteRule .* index2.php


And here's index2.php (which is located right in myProject):

<?php
echo "hiiii";
class Foo {
static public function test($classname)
{
if(preg_match('/\\\\/', $classname))
{
$path = str_replace('\\', '/', $classname);
}
else
{
$path = str_replace('_', '/', $classname);
}

if (is_readable($path.".php"))
{
require_once $path .".php";
}
}
}
spl_autoload_register('\Foo::test');

\controller\Controller::run();
?>



So no matter what I do, this index2.php file should be run, right? Well it's not. I know it's not because I'm getting no echo of hiiii unless I physically go to index2.php.

What could be going wrong?

Thanks!

EDIT:
Just want to make a note that I believe the problem is that the .htaccess file is not being read at all. Here's why I think that:


RewriteEngine On


RewriteBase /
This is garbage
RewriteRule .* index2.php



This should throw a 500 internal server error, should it not? Well it doesn't. When I go to this url:
[localhost...]

I am just getting a 404 Not Found Error screen.

g1smd

8:19 am on Feb 8, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Apart from the placement, or not, of MyProject/ within the patterns and targets the above code should now be working for you.

I've just realised where one of your initial problems might have been. I believe that you've got this .htaccess file in the /MyProject/ folder rather than in the site root. You are therefore now aware that "MyProject/" is removed from the path before the path is presented to RewriteRule for evaluation. In Apache speak 'the path is localised "per directory"'. Of course you have added that item back in within your redirect.

On a real site, you'd likely have this code in the root .htaccess and so all five of your RegEx patterns would include MyProject/ too.

If this code were to be moved to the root htaccess without adding MyProject/ to the rule patterns there would be an error. If I requested
http://example.com/foo/bar/quux/
with a trailing slash, I would be redirected to
http://example.com/MyProject/foo/bar/quux
- removing slash and adding MyProject/ and if I requested
http://example.com/MyProject/foo/bar/quux/
with a trailing slash, I would be redirected to
http://example.com/MyProject/MyProject/foo/bar/quux
- removing slash and adding MyProject/ again.


There's one more thing you would do in real life, on a server "out there" on the web.

After the final (of one) redirecting rule and before the first (of three) rewriting rule you would place a non-www to www redirecting rule using yet another RewriteCond and RewriteRule.

The first group of rules redirect the user to the right URL if they request the wrong one.
The final group of rules rewrite requests arriving in the right format to serve the actual content.

Always leave a blank line after every RewriteRule. This aids clarity of reading.
Add a
# comment
before each block of code explaining in plain English what the block of code does.

Finally, do you actually need the QSA flag? Are valid requested URLs ever in the form
http://example.com/foo/bar/quux?id=456&page=2
with parameters or are they just like
http://example.com/foo/bar/quux
without parameters?


While I see what you're saying and you're making it seem easy

Hopefully, you now understand your code a bit better, and it is your code not mine. You wrote it. The important stuff is the difference between URLs and filepaths and the difference between redirects and rewrites. Once you get that there's no looking back.

This thread also underlines the WebmasterWorld forum principle. We help you debug your code, code you wrote. We don't write your code. This way you learn much more. :)

lucy24

10:15 am on Feb 8, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've just realised where one of your initial problems might have been. I believe that you've got this .htaccess file in the /MyProject/ folder rather than in the site root. You are therefore now aware that "MyProject/" is removed from the path before the path is presented to RewriteRule for evaluation. In Apache speak 'the path is localised "per directory"'. Of course you have added that item back in within your redirect.

I think the important thing is to have your htaccess in the same relative location. That is, if your "real" htaccess is going to be in the same directory as index.html (the root) then that's where the mockup has to be. No matter what you do you'll have to change the exact wording of your redirect target-- from something containing "localhost" to something containing "example.com". But the important part is picking up the incoming requests. If the htaccess is in the right place, those won't need to be changed.

Hm, yes, the QSA... The good part is that if there are no incoming query strings, then this flag will have no effect.

eZACKe

2:34 pm on Feb 8, 2012 (gmt 0)

10+ Year Member



Yep lucy, just localhost. Not sure why there would be a difference from me to you, but that's interesting.


Finally, do you actually need the QSA flag? Are valid requested URLs ever in the form http://example.com/foo/bar/quux?id=456&page=2 with parameters or are they just like http://example.com/foo/bar/quux without parameters?

Hopefully, you now understand your code a bit better, and it is your code not mine. You wrote it. The important stuff is the difference between URLs and filepaths and the difference between redirects and rewrites. Once you get that there's no looking back.

This thread also underlines the WebmasterWorld forum principle. We help you debug your code, code you wrote. We don't write your code. This way you learn much more. :)


Actually, the way my Front Controller is working at the moment there could possibly be urls like that where I will need the query string. Maybe I will change it in the future, but for now I'll have to keep it.


Well glad I got it straightened out and thanks to you all for the help. Much appreciated!

g1smd

3:07 pm on Feb 8, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Don't forget to comment each block of code so that when you look at it in one week/month/year from now you avoid the problem of having no idea what it does.
This 34 message thread spans 2 pages: 34