views:

27

answers:

2

Situation:

I have a few hundred posts each belonging to a particular category.

A] Now when the user visits the home page, the content is irrespective of the category sorted by date.

http://www.example.com

He can navigate through different pages like:

Type 1: http://www.example.com/3 which corresponds to http://www.example.com/index.php?page=3

I can probably do this in mod_rewrite

B] The user can then decide to view by category like:

Type 2: http://www.example.com/Football which will correspond to

http://www.example.com/index.php?page=1&category=Football

He can then navigate through pages like:

Type 3: http://www.example.com/Football/5 which => http://www.example.com/index.php?page=5&category=Football

C] I have a directory called View with index.php in it. It only shows individual posts like:

Type 4: http://www.example.com/View/1312 => http://www.example.com/View/index.php?id=1312

Here is the mod_rewrite I do:

RewriteEngine on RewriteRule ^View/([^/.]+)/?$ View/index.php?id=$1 [L]

Now here are the problems I have

In point C]: http://www.example.com/View/1312 works fine but http://www.example.com/1312/ (notice the trailing slash) breaks apart & gives weird results. Q1) So how do I maintain consistency here?

Q2) Ideally I would want http://www.example.com/View/1514 to show a 404 Error if there is no post with id 1514, but now I have to manually take care of that in PHP code.

What is the right way of dealing with such dynamic urls? especially if the url is wrong.

Q3) how do I ensure that http://www.example.com & http://www.example.com/ both redirect to http://www.example.com/index.php?page=1; (mod_rewrite code would be helpful)

Please Note that there are only two index.php files. One in the root directory which does everything apart from showing individual posts which is taken care by a index.php in View directory. Is this a logical way of developing a website?

+1  A: 

Try these rules:

RewriteRule ^$ index.php?page=1 [L]
RewriteRule ^([0-9]+)/?$ index.php?id=$1 [L]
RewriteRule ^View/([0-9]+)/?$ View/index.php?id=$1 [L]
RewriteRule ^([A-Za-z]+)/?$ index.php?category=$1&page=1 [L]
RewriteRule ^([A-Za-z]+)/([0-9]+)/?$ index.php?category=$1&page=$2 [L]

As for the other question: Yes, since Apache can map these requests to existing files, it responds with a success status code. Now if your application decides that the requested resource does not exist, you need to handle that within your application and send an appropriate status code.

Gumbo
Thanks will try that, can you clarify about 404 error and the trailing slash problem?
Gaurav
+1  A: 

To fix the trailing slash, Just put /? before the $ at the end in your pattern

JKirchartz
No not working, are you sure about this?
Gaurav
If this is your rule: RewriteRule ^View/([0-9]+)/?$ View/index.php?id=$1 [L]This is your pattern: ^View/([0-9]+)/?$
JKirchartz