Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have the following modrewrite rule:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/([^/]*)/([^/]*)/$ /blog.php?date=$1&title=$2 [L]

Which is pretty straightforward. My question is, how do I get this rewrite to direct to blog.php if no variables are listed?

blog.php is written to both produce the article listing if no variables are present, and also to create the individual listing if one is present.

Edit: blog.php works fine without mod rewrite. If I go to http://host/blog.php it lists all my articles. If I go to http://host/blog/2010-10-05/blog-title/ it shows my single entry. Similarly, if I go to http://host/blog.php?date=2010-10-05&title=blog-title I get a single entry. However, I want http://host/blog/ to list all my entries as well.

I guess the question is: Does this require a second rewrite rule? And if so, how do I get the two to play nice.

share|improve this question
    
Which is pretty straightforward. My question is, how do I get this rewrite to direct to blog.php if no variables are listed? care to make an example ? not entirelly sure i understood your point... From what i understood it seems more like a programming issue then a rewrite rule issue. It would be a blog.php programming thing to instead of trying to show the article to show something else when there is no available article on the given variables. –  Prix Oct 11 '10 at 3:21
1  
Or are you saying you just want to redirect blog when people type blog like: RewriteRule ^(blog|blog/)$ /blog.php [L] which would come before your other rule. –  Prix Oct 11 '10 at 3:25
    
Perfect, make it an answer and I'll accept. Thank you. –  Chris Sobolewski Oct 11 '10 at 3:30

1 Answer 1

up vote 1 down vote accepted

From the comments talk, there you go ;)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(blog|blog/)$ /blog.php [L]
RewriteRule ^blog/([^/]*)/([^/]*)/$ /blog.php?date=$1&title=$2 [L]

The blog alone comes first so if the user have typed blog or blog/ it will go to blog.php and stop there, if that requirement does not meet it will move to the second rule.

(blog|blog/)$

The $ is used to prevent that blog matchs blog.html or anything else.

The | is use as an operator OR so if it is blog OR blog/

[L] means LAST so if the rule is meet no other rule will be take into consideration after it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.