2012-10-04 87 views
1

我一直在努力与我的.htaccess文件数周,我改变了很多次,但它不会工作。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 

,但它不工作。

回答

0

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

0
  1. 您的条件仅适用于第一条规则。每套RewriteCond只适用于紧接的RewriteRule。所以条件只适用于RewriteRule ^/([^./]+)\.html$ category.php?id=$1,最后2条规则完全没有条件。

  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 
    

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