htaccess不起作用重写规则

时间:2012-10-04 12:32:51

标签: .htaccess mod-rewrite

我已经在我的.htaccess文件中苦苦挣扎了好几个星期,我多次改变它但是它不起作用。

我在.htaccess文件中有这个:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^/([^./]+)\.html$ category.php?id=$1 
RewriteRule ^/([^./]+)\.html$ tag.php?id=$1 
RewriteRule ^/([^./]+)\.html$ play.php?id=$1 

但它不起作用。

2 个答案:

答案 0 :(得分:0)

您确定在Apache中打开了mod_rewrite吗?你有权访问httpd.conf吗?最好在那里进行重定向,而不是使用.htaccess文件。

答案 1 :(得分:0)

  1. 您的条件仅适用于第一条规则。每组RewriteCond仅适用于紧随其后的RewriteRule 。因此,条件仅适用于RewriteRule ^/([^./]+)\.html$ category.php?id=$1,最后两条规则根本没有条件。

  2. 您的条件是将存在重写为其他内容,这将导致重写循环。你可能想要:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
  3. 您的第二个和第三个规则将永远不会被应用,因为如果有人请求/some-page.html,则第一个规则的正则表达式将匹配它并将URI重写为/category.php?id=some-page,然后规则旁边的内容永远不会匹配,因为第一条规则已经将URI重写为category.php

  4. 您的正则表达式匹配一个前导斜杠,在htaccess文件中的重写规则中应用的URI会删除前导斜杠,所以您需要这样:

    RewriteRule ^([^./]+)\.html$ category.php?id=$1 
    
  5. 1,2和4很容易。 3,没那么多。您将不得不找出一种将html页面表示为类别,标记或播放的独特方式。你不能让所有3看起来都相同,没有办法告诉你想要哪一个。取:

    /something.html
    

    这应该是一个类别吗?标签?还是玩?谁知道,你的重写规则肯定不会。但如果你在前面加上一个关键字,那么你可以区分:

    /category/something.html
    /tag/something.html
    /play/something.html
    

    你的规则如下:

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^category/([^./]+)\.html$ category.php?id=$1 
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^tag/([^./]+)\.html$ tag.php?id=$1 
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^play/([^./]+)\.html$ play.php?id=$1 
    
相关问题