【问题标题】:.htaccess RewriteCond %{REQUEST_FILENAME} !-d not working.htaccess RewriteCond %{REQUEST_FILENAME} !-d 不工作
【发布时间】:2014-10-10 07:25:37
【问题描述】:

.htaccess 代码是,

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^p/([A-Za-z0-9-_]+)/?$ redirect.php?url=$1  [NC,L] #
RewriteRule ^[A-Za-z0-9-_]+/([A-Za-z0-9-_]+)/?$ product.php?seo_id=$1 [NC,L] #
RewriteRule ^([A-Za-z0-9-_]+)/?$ catalog.php?seo_id=$1 [NC,L] #

我正在尝试访问服务器中已经存在的http://example.com/directory/,但我无法访问它。没有发生任何错误,但如果请求已经存在的目录,则不应应用RewriteCond %{REQUEST_FILENAME} !-d 之后的规则。

但在这种情况下,适用 3 条规则之一。

RewriteCond %{REQUEST_FILENAME} !-d 似乎不起作用。

【问题讨论】:

  • 不工作是什么意思?您要访问哪个 URL?
  • 请查看我编辑的答案。
  • 错误是什么?是否使用了 3 个 rewriteRule 之一?
  • 是的,已使用。但是如果请求已经存在的目录,则不应应用RewriteCond %{REQUEST_FILENAME} !-d 之后的规则。我说的对吗?
  • 没有任何错误,但应用了不应该出现的以下 3 条规则之一。

标签: php .htaccess url-rewriting


【解决方案1】:

RewriteCond 仅适用于下一个 RewriteRule,而不适用于所有。然后你应该像这样复制它们:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/([A-Za-z0-9-_]+)/?$ redirect.php?url=$1  [NC,L] #
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[A-Za-z0-9-_]+/([A-Za-z0-9-_]+)/?$ product.php?seo_id=$1 [NC,L] #
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-_]+)/?$ catalog.php?seo_id=$1 [NC,L] #

另一种解决方案包括使用 S 标志(跳过)并反转 RewriteCond:Apache doc for S flagexamples(法语文章,但 htaccess sn-p 不需要翻译 :))


使用另一种解决方案进行编辑(未经测试):

# if it's a file or a folder
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [S=3] # then skip the next 3 rules
RewriteRule ^p/([A-Za-z0-9-_]+)/?$ redirect.php?url=$1  [NC,L]
RewriteRule ^[A-Za-z0-9-_]+/([A-Za-z0-9-_]+)/?$ product.php?seo_id=$1 [NC,L]
RewriteRule ^([A-Za-z0-9-_]+)/?$ catalog.php?seo_id=$1 [NC,L]

【讨论】:

  • 如果我只想写一次而不是在所有规则之前到处写(到处都是蛮力),有什么解决方案
猜你喜欢
  • 2013-04-06
  • 1970-01-01
  • 1970-01-01
  • 2012-11-02
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-25
相关资源
最近更新 更多