.htaccess - 重定向所有 URL,但有一个例外

Cra*_*ray 2 mod-rewrite .htaccess redirect sitemap 301-redirect

我想将所有 URL 从一个域重定向到另一个域。一些旧的 URL 有新的对应项,其中包含要重定向到的特定页面。所有其他 URL 应重定向到新域的主页。

但我不想重定向sitemap.xml. 所以我做了这样的例外(来自这里):

RewriteCond %{REQUEST_URI} !^/sitemap.xml?$
Run Code Online (Sandbox Code Playgroud)

但这不起作用。

这是我的完整代码:

RewriteEngine on

# exception for the sitemap:
RewriteCond %{REQUEST_URI} !^/sitemap.xml?$

# specific redirects:
Redirect 301 /old-page  https://www.new-domain.com/

# catch the rest:
RedirectMatch 301 ^/ https://www.new-domain.com/
Run Code Online (Sandbox Code Playgroud)

有什么不对?

Esa*_*nen 5

由于推荐使用 mod_alias 进行简单重定向,因此以下是mod_alias的解决方案:

# Specific redirects:
Redirect 301 /old-prefix https://www.example.com/
Redirect 301 /another-old-prefix/  https://www.example.com/path/

# Catch the rest, except the sitemap:
RedirectMatch 301 ^/(?!sitemap\.xml$).* https://www.example.com/
Run Code Online (Sandbox Code Playgroud)

主要的功能区别Redirect是它重定向具有相同前缀的所有内容。

那么任何以 URL-path 开头的请求都会向目标 URL 位置处的客户端返回重定向请求。匹配的 URL 路径之外的其他路径信息将附加到目标 URL。