.htaccess重写多个域

时间:2012-02-21 15:40:30

标签: php apache .htaccess

我有一个项目位于以下域:

http://www.example.com/PROJECT/

在项目中,有三个子页面,其页面结构如下:

http://www.example.com/PROJECT/first-subpage.php?Page_ID=12345 
http://www.example.com/PROJECT/second-subpage.php?Page_ID=12345
http://www.example.com/PROJECT/third-subpage.php?Page_ID=12345

我正在尝试为这些中的每一个编写多个Mod Rewrite规则,以便我可以像以下那样重定向以下样式的url:

http://www.example.com/PROJECT/first-section/12345/some-other-text-here
>>
http://www.example.com/PROJECT/first-subpage.php?Page_ID=12345

根据.htaccess tester

,这是我所拥有的,但是我无法让比赛得到。

这是我当前的.htaccess文件:

RewriteEngine On
RewriteBase /PROJECT/

<filesMatch "^(/first-section/.*)">
        Options +FollowSymlinks
        RewriteRule ^/(.*)$/(.*)$/(.*)$ /first-subpage.php?page_ID=$2
</filesMatch>

<filesMatch "^(/second-section/.*)">
        Options +FollowSymlinks
        RewriteRule ^/(.*)$/(.*)$/(.*)$ /second-subpage.php?page_ID=$2
</filesMatch>

<filesMatch "^(/third-section/.*)">
        Options +FollowSymlinks
        RewriteRule ^/(.*)$/(.*)$/(.*)$ /third-subpage.php?page_ID=$2
</filesMatch>

如何让这些规则正确匹配网址?

1 个答案:

答案 0 :(得分:2)

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^PROJECT/first-section/([^/]+)/([^/]+) /PROJECT/first-subpage.php?Page_ID=$1 [NC]
RewriteRule ^PROJECT/second-section/([^/]+)/([^/]+) /PROJECT/second-subpage.php?Page_ID=$1 [NC]
RewriteRule ^PROJECT/third-section/([^/]+)/([^/]+) /PROJECT/third-subpage.php?Page_ID=$1 [NC]

注意:请仔细检查var名称,因为它区分大小写,问题中的变量为Page_ID,而page_ID代码中的变量为.htaccess