Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Cabbit

macrumors 68020
Original poster
Jan 30, 2006
2,128
1
Scotland
Is it possible to have php set up my
HTML:
## PHP REWRITES ##
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)$ html/index.php?page=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ html/index.php?page=$1

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ html/index.php?page=$1&process=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ html/index.php?page=$1&process=$2
Instead of having to manually input different ones into the .htaccess file i would rather able to sent it one from my php file, perhaps making a function so i could tell it were having the "page/date/process" instead of just the "page/date".
 
The only thing I've seen like that is what WordPress does, but it's hard to follow their code for quick guidance on it. If you have it installed check the file /includes/rewrite.php. The issue is that in order for PHP to handle a case, that PHP has to be accessed, which means the visitor has to start somewhere. I don't think any PHP solution you find will be as good as the Apache one.

Here's some simplification of your rewrites though,
Code:
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)/?$ html/index.php?page=$1

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ html/index.php?page=$1&process=$2
You could probably even simplify things more using,
Code:
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/?$ html/index.php?page=$1

RewriteRule ^([^/]+)/([^/]+)/?$ html/index.php?page=$1&process=$2
though you'd want to test that one out first. I'm mostly sure about it though.
 
For your case it is appropriate or sensible to redirect all requests to a single php file which then serves up the appropriate content based on url.
 
The only thing I've seen like that is what WordPress does, but it's hard to follow their code for quick guidance on it. If you have it installed check the file /includes/rewrite.php. The issue is that in order for PHP to handle a case, that PHP has to be accessed, which means the visitor has to start somewhere. I don't think any PHP solution you find will be as good as the Apache one.

Here's some simplification of your rewrites though,
Code:
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)/?$ html/index.php?page=$1

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ html/index.php?page=$1&process=$2
You could probably even simplify things more using,
Code:
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/?$ html/index.php?page=$1

RewriteRule ^([^/]+)/([^/]+)/?$ html/index.php?page=$1&process=$2
though you'd want to test that one out first. I'm mostly sure about it though.

With this in mind how do i tell the server to rewrite

/html/index.php?page=blog&id=12&date=13112009&title=First_Post&location=readpost

to

/blog/13/11/2009/First_Post/readpost

or for another module

html/index.php?page=forum&location=showthread&id=13&title=First_Thread&page=3

to

/forum/showthread/13/First_Thread/page_3

or
html/index.php?location=edit/id=12

to
/edit/12
 
One thing i have just thought of is the possibility of using an array.

Code:
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/?$ html/index.php?location=$1

PHP:
$data[0] = $page
$data[1] = $id
$data[2] = $date
$data[3] = $location

Which i would assume would mean that i could have a "complex" navigation structure with very little code compared to a manual method.

Any thoughts on this method?
 
I don't currently have the time to work through those rewrites, but here's a decent write up that may help you. You also do searches on "apache rewrite pretty urls" to find other tutorials. I've seen what you want to do done before.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.