URL 仅重写主请求并忽略子文件夹

URL Rewrite only main request and ignore subfolders

我已将我的论坛从 vb4(使用 vbseo)移动到 vb5...

我的旧 vb4 网址是:

example.com/123-threadtitle

新的 vb5 是:

example.com/category/subcategory/123-threadtitle 

123是线程的id。

我的尝试:

RewriteRule ([0-9]+)+-[^*]+ hxxps://www.example.com/showthread.php?t= [L,R=301]

它有效,但他再次重写了 vb5 请求,我们遇到了问题。

如何将其设置为忽略文件夹中的请求?

RewriteCond %{REQUEST_FILENAME} !-d

不工作:-(

RewriteRule ([0-9]+)+-[^*]+ hxxps://www.example.com/showthread.php?t= [L,R=301]

正则表达式 (RewriteRule pattern) 不是 anchored 所以匹配请求中的任何地方 URL-小路。正则表达式不排除斜杠(即路径段定界符),因此将消耗整个 URL-path.

如果您的旧 URL 格式为 /123-threadtitle 那么您似乎只需要匹配第一个路径段,这自然应该排除“子文件夹”(我认为您指的是新 URL 格式中的附加 path-segments,而不是物理目录,这是您发布支票的 condition对于).

请尝试以下操作:

# Redirect old URLs to "/showthread.php?t=NNNN"
RewriteRule ^(\d+)-[^/]+$ https://www.example.com/showthread.php?t= [L,R=301]

这匹配 /123-threadtitle,但不匹配 /category/subcategory/123-threadtitle 并且 外部重定向 请求到 /showthread.php?t=123

这需要在任何现有重写之前接近 .htaccess 文件的顶部。首先使用 302(临时)重定向进行测试以避免潜在的缓存问题。


但是,出于 SEO 目的,最好在内部将请求重写为 /showthread.php?t=123,然后触发外部重定向到新的 URL(即 /category/subcategory/123-threadtitle)您的 PHP 代码。

# Internally rewrite old URLs to "/showthread.php?t=NNNN"
RewriteRule ^(\d+)-[^/]+$ showthread.php?t= [L]