0

My blog's .htaccess is setup in such a way that one page is accessed through multiple URLs, and displays different content depending on which URL is visited.

http://kn3rdmeister.com/category/blog/
http://kn3rdmeister.com/2012/
http://kn3rdmeister.com/2012/07/

all are actually using http://kn3rdmeister.com/blog.php.

The .htaccess file is very handy in the sense that I only need to redirect to one page (pretty much ever) just with different query strings. After a lot messing around with 'em, all of my rules finally work, and I'm dang glad that they do. Well, almost all of them work. The last one does not.

the .htaccess:

RewriteEngine On

RewriteRule ^blog\.php$ /category/blog/ [R=301,L]
RewriteRule ^category/blog/?$ blog.php [L]

RewriteRule ^category/blog/page/?$ /category/blog/ [R=301,L]
RewriteRule ^category/blog/page/([0-9]*)/?$ /category/blog/?pagenum=$1 [L]

RewriteRule ^([0-9]{4})/?$ /category/blog/?year=$1 [L]
RewriteRule ^([0-9]{4})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2 [L] 
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2&day=$3 [L] 
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(^/]+)/?$ /category/blog/?url=http://kn3rdmeister.com/$1/$2/$3/$4/ [L]

The last rule is supposed to redirect to the "permanent link" page for each blog post. Being that each URL is unique, I'm using the post URLs as the unique identifier. Essentially, it is supposed to pass the "url" query string through "blog.php". The PHP script takes over, sees that the "url" query string is set, and then loads the only post with that exact URL in it's row.

The script works, but the redirect doesn't. Going directly to

http://kn3rdmeister.com/blog.php?url=http://kn3rdmeister.com/2012/07/04/amsterdam-ave/

will load the right content. However, going to

http://kn3rdmeister.com/2012/07/04/amsterdam-ave/

doesn't.

mcneiljm
  • 77
  • 1
  • 8
  • Hi, your website is vulnerable to SQL and/or XSS/CSRF exploit. Please, don't use `mysql_*` functions to write new code. They are no longer maintained and the community has begun [deprecation process](http://goo.gl/KJveJ). See the [*red box*](http://goo.gl/GPmFd)? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide which, [this article](http://goo.gl/3gqF9) will help you. If you pick PDO, [here is good tutorial](http://goo.gl/vFWnC). – Dejan Marjanović Jul 16 '12 at 06:18
  • Sorry, missed yr comment so my A was too late. Never mind. Why not see http://stackoverflow.com/a/9261963/1142045 for a simple way of avoiding doing your head in :-) – TerryE Jul 16 '12 at 07:39
  • Thank ya very much TerryE, I'll be visiting that page :P Most often when I'm posting on here it's around 2AM and I'm not quite 100% there – mcneiljm Jul 16 '12 at 08:08
  • When you're adding a comment and replying to someone who is not the poster of the Q or the A, then stick an @ in front of their username (tab completion works) and this trigger a message in their inbox. :-) – TerryE Jul 16 '12 at 10:04

2 Answers2

0

Try adding QSA (Query String Append). Also, invert rules so that "deeper" links go on top.

RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(^/]+)/?$ /category/blog/?url=http://kn3rdmeister.com/$1/$2/$3/$4/ [QSA,L]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2&day=$3 [QSA,L] 
RewriteRule ^([0-9]{4})/([0-9]{2})/?$ /category/blog/?year=$1&month=$2 [QSA,L] 
RewriteRule ^([0-9]{4})/?$ /category/blog/?year=$1 [QSA,L]

But, you can't use rewritten links in other rules. So wherever you have category/blog/ replace it with blog.php.

Dejan Marjanović
  • 19,244
  • 7
  • 52
  • 66
  • I tried that, but I'm still getting a 404 error. I'm going to creating a separate page for the "permalink" pages, I believe I'm starting to overuse blog.php to the point where I don't know what the heck is going on :) – mcneiljm Jul 16 '12 at 07:08
  • kn3rdmeister, you've made a fool of yourself once more. Careful inspection reveals that I forgot a left bracket, "[", in the rule that would not work. Good day, gents. – mcneiljm Jul 16 '12 at 07:25
0

Whilst webarto comments are good advice, your problem is a missing [:

^([0-9]{4})/([0-9]{2})/([0-9]{2})/([^/]+)/?$

not

^([0-9]{4})/([0-9]{2})/([0-9]{2})/(^/]+)/?$
TerryE
  • 10,724
  • 5
  • 26
  • 48