【问题标题】:Redirect to Dynamic URL only if that dynamic URL path name file not exist PHP仅当动态 URL 路径名文件不存在 PHP 时才重定向到动态 URL
【发布时间】:2021-06-05 02:29:05
【问题描述】:

仅当我的文件夹中不存在该路径名文件时,我才想重定向到动态 URL。我如何使用 .htaccess RewriteRule 来做到这一点

RewriteRule ^(.*)/(.*)/(.*)/?$ product.php [NC,L]
RewriteRule ^(.*)/(.*)/?$ results.php [NC,L]
ErrorDocument 404   http://example.com/404.php

此规则有效,但我的图像、CSS 和 JS 文件无法加载。

仅当“abc/xyz”和“abc/xyz/tuv”不作为我的文件夹中的文件存在时,我想将这些动态 URL(如 http://example.com/abc/xyz 重定向到 results.php 和 http://example.com/abc/xyz/tuv 到 product.php)。

我们如何做到这一点。

谢谢

【问题讨论】:

  • 这能回答你的问题吗? rewritecond if file does not exist
  • 旁白:您的正则表达式相当模棱两可。 /abc/def/ghi/jkl/mno/pqr 形式的请求会发生什么情况?那么/abc/def/呢?
  • 您的“图片、CSS 和 JS”网址采用什么形式?

标签: apache .htaccess mod-rewrite url-rewriting friendly-url


【解决方案1】:

对于您显示的示例,您能否尝试以下操作。这会查找您提到的文件夹名称是否作为实际目录/文件夹存在于系统中,然后在后端将它们重写为 product.php(对于 abc/xyz/tuv)和 results.php(对于 abc/xyz)文件。

请确保在测试您的 URL 之前清除您的浏览器缓存。

RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^abc/xyz/tuv/?$ product.php [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^abc/xyz/?$ results.php [NC,L]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 2013-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多