【问题标题】:How do I redirect multiple urls to same url如何将多个网址重定向到同一个网址
【发布时间】:2022-01-19 04:12:07
【问题描述】:

如何将多个 url 重定向到另一个 url 我试过了,但它不起作用

RewriteCond  %{HTTP_HOST} ^sub.domain.to/q1$ [NC,OR]
RewriteCond  %{HTTP_HOST} ^sub.domain.to/q2$ [NC,OR]
RewriteCond  %{HTTP_HOST} ^sub.domain.to/q3$ [NC,OR]
RewriteCond  %{HTTP_HOST} ^sub.domain.to/q4$ [NC,OR]
RewriteCond  %{HTTP_HOST} ^sub.domain.to/q5$ [NC]
RewriteRule ^(.*)$ https://sub.domain.to/full-q [R=301,L]

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案1】:

    好的,当我添加%{REQUEST_URI}时它起作用了

    这样就成功了

    RewriteCond  %{HTTP_HOST}%{REQUEST_URI} ^sub\.domain\.to/q1$ [NC,OR]
    RewriteCond  %{HTTP_HOST}%{REQUEST_URI} ^sub\.domain\.to/q2$ [NC,OR]
    RewriteCond  %{HTTP_HOST}%{REQUEST_URI} ^sub\.domain\.to/q3$ [NC,OR]
    RewriteCond  %{HTTP_HOST}%{REQUEST_URI} ^sub\.domain\.to/q4$ [NC,OR]
    RewriteCond  %{HTTP_HOST}%{REQUEST_URI} ^sub\.domain\.to/q5$ [NC]
    RewriteRule ^(.*)$ https://sub.domain.to/full-q [R=301,L]
    

    【讨论】:

      【解决方案2】:

      HTTP_HOST 服务器变量包含Host HTTP 请求标头的值,即。仅主机名。这不包含 URL 路径(即您的示例中的 /q1/q2)。

      RewriteRule 模式与 URL 路径匹配。

      请尝试以下方法:

      RewriteCond %{HTTP_HOST} ^sub\.domain\.to [NC]
      RewriteRule ^(q1|q2|q3|q4|q5)$ https://%{HTTP_HOST}/full-q [NC,R=301,L]
      

      或者,在 条件 中使用REQUEST_URI 服务器变量,其中包含完整的 URL 路径。例如:

      RewriteCond %{HTTP_HOST} ^sub\.domain\.to [NC]
      RewriteCond %{REQUEST_URI} ^/q1$ [NC,OR]
      RewriteCond %{REQUEST_URI} ^/q2$ [NC,OR]
      RewriteCond %{REQUEST_URI} ^/q3$ [NC,OR]
      RewriteCond %{REQUEST_URI} ^/q4$ [NC,OR]
      RewriteCond %{REQUEST_URI} ^/q5$ [NC]
      RewriteRule ^ https://%{HTTP_HOST}/full-q [R=301,L]
      

      使用 302(临时)重定向进行测试以避免潜在的缓存问题,并且仅更改为 301(永久)重定向 - 如果这是有意的话 - 一旦您确认这按预期工作。

      您需要在测试前清除浏览器缓存。

      【讨论】:

        猜你喜欢
        • 2020-09-29
        • 1970-01-01
        • 1970-01-01
        • 2016-11-05
        • 2016-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多