3

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

4
  • It is much clearer if you only ask one question at a time. Please split one of them off into a separate post. Commented Jul 9, 2012 at 0:15
  • Regarding this question, what effect do these three lines in your .htaccess have on the example URL? Does it change at all? Commented Jul 9, 2012 at 0:22
  • Nothing changes. The .htaccess is in my website's root directory. Could it help if I make another .htaccess in the blog directory, and then edit the rule accordingly? Commented Jul 9, 2012 at 0:25
  • There is a typo in the pattern. The first ) should be a ]. Is that the post, or your actual .htaccess that has the typo? Commented Jul 9, 2012 at 1:06

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you @Jon, that worked fine. However, I don't have anything setup to handle the redirection :P where would you suggest starting for that? this perhaps needs its own separate post.
You could probably use mod_proxy with ProxyPass and ProxyPassReverse. Might be overkill, but it should do the rewriting in both directions. httpd.apache.org/docs/2.0/mod/mod_proxy.html#proxypass
That looks promising... buuut I'm a n00b at this. I'll start a new question, as to keep things clean :P thanks for the help
0

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.

2 Comments

Woah... That's a lot of info to look at :D I'll look into all of that at a later time (a few hours, likely). I think I may need to reconsider how I display and post my content, so that I can expand the range of things I can play with.
Your "first option" answer turns /blog/date/2012/07/04/4.php into index.php?action=showposts_by_date&year=1234&month=05&day=21 . But the questioner wants the opposite effect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.