普通网友 2010-10-09 20:07
浏览 20
已采纳

帮助.htaccess文件。 mod_rewrite用于自定义网址?

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?

  • 写回答

2条回答 默认 最新

  • dtncv04228 2010-10-09 20:13
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器