Mod Rewrite Anyone?

Fleetwood Mac

macrumors 65816
Original poster
Apr 27, 2006
1,265
0
Canada
I'm attempting to use mod rewrite to make URLs like http://mysite.com/index.php?page=imapyschokiller available at http://mysite.com/imapyschokiller. Ideally, I'd also like page such as http://mysite.com/index.php?page=go/with/the/flow to be available at http://mysite.com/go/with/the/flow too. Is this possible?

The best way to help me here would be to post the exact code I need to put in .htaccess. I'd love to learn, but I just can't see myself ever understanding this stuff. Too bad I can't do it in CSS. :D

I know, hopeless designer's question of the day™™, but just maybe someone can help me. :eek:
 

zimv20

macrumors 601
Jul 18, 2002
4,388
7
toronto
do you want *all* pages with that pattern to be re-directed? if so, something like:
Code:
RewriteEngine on
RewriteRule ([^/\.]+)/?$ index.php?page=$1 [L]
i'm no rewrite expert, but i think that should do the trick. there's a very similar example on this page.

if you want to do only certain pages, then you'll have to specify each one:
Code:
RewriteRule ^imapyschokiller$ index.php?page=imapyschokiller [L]
RewriteRule ^go/with/the/flow$ index.php?page=go/with/the/flow [L]
 

Fleetwood Mac

macrumors 65816
Original poster
Apr 27, 2006
1,265
0
Canada
I am trying to redirect all pages.

With one modification (adding a ^ after RewriteRule), the code provided worked for top level pages.
Code:
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
So; http://mysite.com/index.php?page=ducklings is now accessible at http://mysite.com/ducklings/. However, urls contained below the domains root (such as http://mysite.com/index.php?page=clownfish/nemo) don't seem to be affected by the rule.

In a perfect world, the pages would also be accessible without the trailing slash, but that isn't a matter of huge importance at this point.
 

zimv20

macrumors 601
Jul 18, 2002
4,388
7
toronto
it's all trial and error for me at this point, this is more than i've done before. however, give each of these a try (one at a time):
Code:
RewriteRule ^([^\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^([^\.]+)$ index.php?page=$1 [L]
edit: i just tested it out, and the first one is working for me, with and without the trailing slash. here's my index.php:
Code:
<?php
echo $QUERY_STRING
?>
and when i hit http://www.mysite.com/foo/bar/baz, i get:
Code:
page=foo/bar/baz
and when i hit http://www.mysite.com/foo/bar/baz/, i get:
Code:
page=foo/bar/baz/
 

elppa

macrumors 68040
Nov 26, 2003
3,229
106
This is what wordpress does:

Code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
Then everything gets sent to index.php.

You can then use $_SERVER['REQUEST_URI'] you can get a address divvy it up from there.

If no page exists you can send a: header("HTTP/1.0 404 Not Found"); and redirect to a 404 page.

Probably not the best/most efficient way to do it, but it works for me. MOD REWRITE looks so complicated from trying to read the manual so I kind of gave up.
 

Fleetwood Mac

macrumors 65816
Original poster
Apr 27, 2006
1,265
0
Canada
Its working. Here's my .htaccess:
Code:
RewriteEngine On
RewriteBase /
RewriteRule ^([^\.]+)/?$ index.php?page=$1 [L]
Thank you so much, zimv20 and elppa.
 

CoreWeb

macrumors 6502
Mar 2, 2007
456
0
Edge of reason
Keep in mind, though, that you might not want to redirect all content. I don't redirect the /templates folder on my website. Everything else is redirected. This allows me to statically store the CSS, Javascript, etc. for better performance, as the PHP-based system which parses the URL combined with the exceptionally slow server can make page generation times be somewhat slow (best case scenario, ~.03 seconds, worst case, with everything being generated from scratch, no cache or anything, 100% of CPU already being used by other processes, ~25 seconds... OUCH!)

Apache's file-system based path interpretation is much, much faster than PHP. It will most likely take Apache < .00001 seconds, where as PHP will take significantly longer.
 

Knox

Administrator
Staff member
Jul 1, 2002
1,259
1
UK
Keep in mind, though, that you might not want to redirect all content. I don't redirect the /templates folder on my website. Everything else is redirected. This allows me to statically store the CSS, Javascript, etc. for better performance, as the PHP-based system which parses the URL combined with the exceptionally slow server can make page generation times be somewhat slow (best case scenario, ~.03 seconds, worst case, with everything being generated from scratch, no cache or anything, 100% of CPU already being used by other processes, ~25 seconds... OUCH!)

Apache's file-system based path interpretation is much, much faster than PHP. It will most likely take Apache < .00001 seconds, where as PHP will take significantly longer.
I suspect that's why the regex is exempting URLs with a period in them, so that things like style.css, image.jpg, javascript.js etc are ignored. However, good point in general - do need to be careful with rewrite rules, sometimes they have unexpected consequences.