Htaccess:重写规则问题

时间:2013-03-03 11:35:11

标签: .htaccess mod-rewrite

我在.htaccess文件中跟踪代码。该文件位于子文件夹中,例如/ content /.

   RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d 
    RewriteRule ^([^/]+)(/([^/]+))?(/(edit)+)(/([^/]+))?/?$ edit.php?category=$1&slug=$3&edit=$5&password=$7 [L]
    RewriteRule ^([^/]+)(/([^/]+)?/?)$ content.php?category=$1&slug=$3 [L]

我期待上面的代码会重写,

  1. mydomain.com/content/text1/text2/edit/my_password to mydomain.com/content/edit.php?category=text1&slug=text2&password=my_password
  2. mydomain.com/content/text1/text2到mydomain.com/content/content.php?category=text1&slug=text2
  3. mydomain.com/content/text1至mydomain.com/content/content.php?category=text1
  4. mydomain.com/content/到mydomain.com/content/content.php
  5. 代码正确重写,但它打破了资产网址。 例如它将http://mydomain.com/content/js/tms.js重写为其他内容,对于css文件也是如此。我的资源文件夹是/ img,/ css,/ js。

    如果我评论了RewriteRule ^([^/]+)(/([^/]+))?(/(edit)+)(/([^/]+))?/?$ edit.php?category=$1&slug=$3&edit=$5&password=$7 [L]行,那么它适用于资产

    任何想法如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

条件RewriteCond %{REQUEST_FILENAME} -d表示“请求是针对现有目录”。如果您在!前面添加-d,则会将其更改为“请求不适用于现有目录”。

尝试:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)(/edit)+/([^/]+?)/?$ edit.php?category=$1&slug=$2&password=$4 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+?)/?$ content.php?category=$1&slug=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+?)/?$ content.php?category=$1 [L]