mcneiljm
mcneiljm

Reputation: 77

mod_rewrite with multiple query strings?

I'm trying to cleanup some URLs on my blog, so I've decided to look into mod_rewrite. I haven't a clue what I'm doing though, so I was hoping I could get some help :P I have links like http://kn3rdmeister.com/blog/post.php?y=2012&m=07&d=04&id=4. Although it works, and people still get the content I want them to have, I don't like them having to look at all the query strings. I want to turn the above link into http://kn3rdmeister.com/blog/2012/07/04/4.php.

This is what my .htaccess looks like right now.

RewriteEngine On
RewriteCond %{QUERY_STRING} ^y=([0-9){4})&m=([0-9]{2})&d=([0-9]{2})&id=([0-9]*)$
RewriteRule ^/blog/post\.php$ http://kn3rdmeister.com/blog/%1/%2/%3/%4.php? [L]

Like I said, I'm absolutely clueless :D

Upvotes: 3

Views: 8661

Answers (2)

Eddy Freddy
Eddy Freddy

Reputation: 1816

At first you should define your URLs!!!

Like:

/blog shows front page

/blog/1234 shows post 1234

/blog/date/2012 shows posts by year

/blog/date/2012/06 shows posts by year and month

/blog/date/2012/06/01 shows posts by year and month and day

and so on...

First option is to rewrite each of your defined URLs to index.php. Your index.php has only to handle the submitted GET parameters.

### Do only if rewrite is installed
<IfModule mod_rewrite.c>

### Start rewrite and set basedir
RewriteEngine on
RewriteBase /

### Rewrite only if no file link or dir exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

### Rewrite frontpage
RewriteRule ^blog$ /index.php?action=showfront [L,QSA]

### Rewrite post
RewriteRule ^blog/([0-9]+)$ /index.php?action=showpost_by_id&id=$1 [L,QSA]

### Rewrite posts by date
RewriteRule ^blog/date/([0-9]{4})$ /index.php?action=showposts_by_date&year=$1 [L,QSA]
RewriteRule ^blog/date/([0-9]{4})/([0-9]{2})$ /index.php?action=showposts_by_date&year=$1&month=$2 [L,QSA]
RewriteRule ^blog/date/([0-9]{4})/([0-9]{2})/([0-9]{2})$ /index.php?action=showposts_by_date&year=$1&month=$2&day=$3 [L,QSA]

### Rewrite posts by tag
RewriteRule ^blog/tag/([a-zA-Z0-9_-]+)$ /index.php?action=showposts_by_tag&tag=$1 [L,QSA]

</IfModule>

Test in index.php with: print_r($_GET); print_r($_POST);

The second option is to rewrite all URLs and your index.php needs to handle all possible URLs. So at first it needs something like a router that splits the incoming URL in parts and then send the requested page or an error-page. I would try this at first as the bloody school.

<IfModule mod_rewrite.c>

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^ index.php%{REQUEST_URI} [L]

</IfModule>

Test in index.php with:

print_r(explode('/', ltrim($_SERVER['PATH_INFO'], '/')));
print_r($_GET);
print_r($_POST);

The third option is to use a PHP framework. A framework may help you to write your code quite fast. It delivers you many base-classes like a router. (f.e. ZendFramework, Flow3, Kohana, Symfony, CodeIgniter, CakePHP, yii and others). This will make you more advanced.

The fourth and laziest option is to use a ready made software like Wordpress.

Upvotes: 0

Jon Lin
Jon Lin

Reputation: 143846

If you're using apache 2.0 or higher, you're going to need to remove the leading slash (the prefix) if these rules are in an .htaccess file, so that your regular expression looks like this:

# also note this needs to be a "]"--v
RewriteCond %{QUERY_STRING} ^y=([0-9]{4})&m=([0-9]{2})&d=([0-9]{2})&id=([0-9]*)$
RewriteRule ^blog/post\.php$ http://kn3rdmeister.com/blog/%1/%2/%3/%4.php? [L]

This is going to make it so when someone puts http://kn3rdmeister.com/blog/post.php?y=2012&m=07&d=04&id=4 in their browser's URL address bar, their browser will get redirected to http://kn3rdmeister.com/blog/2012/07/04/4.php and the new URL will appear in their address bar.

I assume you've got something setup on your server to handle a request like blog/2012/07/04/4.php.

Upvotes: 3

Related Questions