开发者

Apache Mod-Rewrite Question

I have a PHP scripted named index.php inside a folder named blog. There are three different views.

  1. http://www.myDomain.com/blog/index.php
  2. http://www.myDomain.com/blog/index.php?tags=list of categories
  3. http://www.myDomain.com/blog/index.php?post=name of post

I would like to change the view based on the URL.

  1. /blog redirects to number 1 above
  2. /blog/name-of-category redirects to numbe 2 above
  3. /blog/name-of-category/name-of-post redirects to number 3 above.

Right now I have the following mod_rewrite rules.

RewriteRule ^blog$ blog/index.php [L]
RewriteRule ^blog/(.+)/(.+)$ blog/index.php?post=$2 [L]
RewriteRule ^blog/(.+)$ blog/index.php?tags=$1 [L]

This does not work, and I'm not sure why. Right now it always redirects to the last URL:

blog/index.php?tags=$1

And the GET data contains "index.php."

Also, if add a forward slash to the fina开发者_Python百科l rule like so:

RewriteRule ^blog/(.+)/$ blog/index.php?tags=$1 [L]

All redirects work fine. The problem is, I'm required to have a forward slash at the end of the URL if I want the category view.

Any ideas what's happening here? how I can fix this?


Thanks for the replies. I figured out that my problem was a side effect of having my scripts inside the folder named "blog". Here's what index.php looked like:

<?php

define ('BASE_PATH', "../blog/");

include_once(BASE_PATH . 'controller/Controller.php');

$controller = new Controller();
$controller->invoke();

See the problem? Because my script's base path was "blog", mod_rewrite was rewriting all my references inside the program. By renaming my script folder to blogScript, it fixed the problem.


In a regular expression, . matches any character (including a / character) so try doing ^blog/([^/]+)$ instead to match any character except a /.


You could write it as follows.

RewriteRule ^blog/?$ blog/index.php [L]
RewriteRule ^blog/(.+?)/(.+?)/?$ blog/index.php?post=$2 [L]
RewriteRule ^blog/(.+?)/?$ blog/index.php?tags=$1 [L]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜