通过mod_rewrite处理链接

时间:2016-06-09 18:05:51

标签: php apache .htaccess mod-rewrite

我想通过 mod_rewrite 来缩短我的网址。

我的网站上有两种类型的网址:一次带参数且没有参数。

  1. example.com/devel/pages/event.php?event=MYEVENTNAME
  2. example.com/devel/pages/image.php?event=MYEVENTNAME
  3. example.com/devel/pages/location.php?name=MYLOCATION
  4. example.com/devel/pages/home.php
  5. example.com/devel/pages/news.php
  6. 我想将这些网站称为:

    1. example.com/devel/events/MYEVENTNAME
    2. example.com/devel/images/MYEVENTNAME
    3. example.com/devel/locations/MYLOCATION
    4. example.com/devel/home
    5. example.com/devel/news
    6. 这就是我现在写的......

      e.g。

      RewriteRule devel/images/(.*)?$ devel/pages/images.php?event=$1
      

      如何以正确的方式做到这一点?

1 个答案:

答案 0 :(得分:2)

您可以在站点根目录中使用这两个通用规则.htaccess:

RewriteEngine On

RewriteRule ^devel/([\w-]+)/([\w-]+)/?$ devel/pages/$1.php?event=$2 [L,QSA,NC]

RewriteRule ^devel/([\w-]+)/?$ devel/pages/$1.php [L,NC]