Apache 使用htaccess规则将查询字符串替换为文件扩展名

Apache 使用htaccess规则将查询字符串替换为文件扩展名,apache,.htaccess,mod-rewrite,url-rewriting,Apache,.htaccess,Mod Rewrite,Url Rewriting,我有这个链接https://career.guru99.com/top-50-c-sharp-interview-questions-answers/?format=pdf 我想将其重定向到https://www.guru99.com/pdf/c-sharp-interview-questions.pdf 我创建了以下htaccess规则 RewriteCond %{QUERY_STRING} format=pdf [NC] RewriteRule ^c-sharp-interview-ques

我有这个链接
https://career.guru99.com/top-50-c-sharp-interview-questions-answers/?format=pdf

我想将其重定向到
https://www.guru99.com/pdf/c-sharp-interview-questions.pdf

我创建了以下htaccess规则

RewriteCond %{QUERY_STRING} format=pdf [NC]
RewriteRule ^c-sharp-interview-questions.html  /pdf/c-sharp-interview-questions.pdf? [R=301,L]
但挑战是我有100多个链接,我将不得不在htacess中手动添加这么多条目,这也会降低网站的速度。是否有一些正则表达式可以帮助实现这一点

我希望将
/?format=pdf
替换为
.pdf

第一个解决方案:如果您点击
http://localhost:80/top-50-c-sharp-interview-questions-answers/?format=pdf
在浏览器中。如果您想在浏览器中重定向URL,请将
[NC,L]
更改为
[R=301,NC,L]

RewriteEngine ON
RewriteCond %{HTTP_HOST} ^career\.guru99\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

RewriteCond %{QUERY_STRING} ^format=(.*) [NC]
RewriteRule ^top-50-(.*)/?$ pdf/c-sharp-$1.%1 [NC,L]


第二种解决方案:您是否可以尝试以下基于所示示例编写的解决方案(考虑到您想点击
http://localhost:80/pdf/c-尖锐的面试问题。pdf
在您的浏览器中)

注意:请一次使用第1种或第2种溶液。请确保在测试URL之前清除浏览器缓存

我希望将
/?format=pdf
替换为
.pdf

尽管在你的例子中,这并不是唯一改变的事情。您还需要执行以下操作:

  • 将主机名从
    career.guru99.com
    更改为
    www.guru99.com
  • 从URL路径的开头删除
    top-50-
  • 从URL路径的末尾删除
    -answers/
  • 请尝试以下方法:

    RewriteCond %{HTTP_HOST} ^career\.guru99\.com [NC]
    RewriteCond %{QUERY_STRING} ^format=(pdf)$ [NC]
    RewriteRule ^top-50-([\w-]+)-answers/?$ https://www.guru99.com/%1/$1.%1 [QSD,R=302,L]
    
    这将重定向表单的URL:

    https://career.guru99.com/top-50-something-here-answers/?format=pdf
    
    致:

    %1
    反向引用只是从查询字符串中捕获“pdf”字符串(避免重复),这在
    重写规则
    替换字符串中使用了两次

    $1
    反向引用在URL路径开始处的
    /top-50-
    之后和URL路径结束处的
    -answers/
    (尾部斜杠可选)之前捕获URL路径的部分

    QSD
    标志从重定向的URL中丢弃查询字符串

    在更改为301(永久)重定向之前,首先使用302(临时)重定向进行测试—如果这是有意的话—以避免潜在的缓存问题

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


    旁白:

    在您的示例中,
    .html
    来自哪里


    更新#1:如果最后一个URL路径段要按原样形成文件名,而不删除任何内容(正如您在注释中所暗示的那样),则可以简化上述规则:

    RewriteCond %{HTTP_HOST} ^career\.guru99\.com [NC]
    RewriteCond %{QUERY_STRING} ^format=(pdf)$ [NC]
    RewriteRule ^([\w-]+?)/?$ https://www.guru99.com/%1/$1.%1 [QSD,R=302,L]
    
    (然而,这确实与您在问题中发布的示例相矛盾?)


    更新#2:

    从这两个示例中,没有一个模式可以应用于将源URL路径映射到目标。例如,您将如何用自然语言描述如何从
    专家级qtp uft面试问题
    qtp
    以及从
    top-35-advanced-software-testing-questions
    测试

    正则表达式不是魔法。需要有一个从A映射到B的可识别的“模式”

    因此,您需要单独执行这些重定向。但是,这可以简化,以便不影响站点性能。(尽管在
    .htaccess
    中,即使有100个这样的重定向也不算太糟糕。)

    您可以在内部将与主机名(
    test8.guru99.com
    )和查询字符串(
    format=pdf
    )匹配的请求重写为处理请求并发出重定向的脚本。或者,您可以将请求重写到(私有)子目录,该子目录包含第二个
    .htaccess
    文件,该文件仅具有这些特定重定向

    例如:

    # Redirects
    RewriteRule ^expertadvance-level-(qtp)-uft-interview-questions$ %{ENV:TARGET_BASE}/$1.pdf [R=302,L]
    RewriteRule ^top-35-advanced-software-(testing)-questions$ %{ENV:TARGET_BASE}/$1.pdf [R=302,L]
    
    在root
    .htaccess
    文件中,如果子目录通过初步检查,则对其进行内部重写:

    RewriteCond %{HTTP_HOST} ^test8\.guru99\.com [NC]
    RewriteCond %{QUERY_STRING} ^format=pdf$ [NC]
    RewriteRule ^([\w-]+?)/?$ redirect-pdf/$1 [QSD,L]
    
    创建子目录
    /redirect pdf
    ,并在
    /redirect pdf/.htaccess
    的辅助
    .htaccess
    文件中创建特定的重定向。请注意,在上面的
    RewriteRule
    中,我从重写的URL中删除了(可选)尾随斜杠,因此在下面的重定向中不应选中该斜杠

    RewriteEngine On
    
    # Set an env var to the base target
    # (Just saves some bytes/repetition and easier to update)
    RewriteRule ^ - [E=TARGET_BASE:https://www.test2.demoguru99.com/pdf]
    
    # Redirects
    RewriteRule ^expertadvance-level-qtp-uft-interview-questions$ %{ENV:TARGET_BASE}/qtp.pdf [R=302,L]
    RewriteRule ^top-35-advanced-software-testing-questions$ %{ENV:TARGET_BASE}/testing.pdf [R=302,L]
    # etc.
    # :
    
    # If get to the end and nothing matched above then fail with a 404...?
    
    如果需要,可以使用反向引用(如果适用)来保存重复。例如:

    # Redirects
    RewriteRule ^expertadvance-level-(qtp)-uft-interview-questions$ %{ENV:TARGET_BASE}/$1.pdf [R=302,L]
    RewriteRule ^top-35-advanced-software-(testing)-questions$ %{ENV:TARGET_BASE}/$1.pdf [R=302,L]
    

    您的规则首先与此处请求的路径不匹配。(或者你的问题描述不准确。)我在以下链接上进行了重定向-但没有正确重定向,这是404错误。重定向正确的链接是-我使用了以下代码RewriteCond%{QUERY_STRING}format=pdf[NC]RewriteRule^c-sharp-interview-questions.html/pdf/c-sharp-interview-questions.pdf?[R=301,L]@anubhava,是的,主机名发生了变化。我尝试了以下解决方案,但无效。我有以下链接重定向-但它不是404错误重定向。重定向正确的链接是-@renishbhalodiya,如果有任何错误,请告诉我好吗?@renishbhalodiya,好的,请你在这里举一些URL的例子好吗?@RavinderSingh13,尽管在这个问题上他们说他们“有100多个链接”。这些大概只是那些“100+以上”中的两个@胡言乱语13是的,我在这里没有看到任何模式?!;)我们的Url没有固定在“前50名”的单词上。对于ex.-和100+链接,您如何从源URL中识别目标URL?在您在问题中发布的单个示例中,唯一明显的区别似乎是删除了
    top-50-
    -answers/
    ,正如我在上面的回答中所述。您现在是否建议删除不同的前缀?怎么
    RewriteCond %{HTTP_HOST} ^test8\.guru99\.com [NC]
    RewriteCond %{QUERY_STRING} ^format=pdf$ [NC]
    RewriteRule ^([\w-]+?)/?$ redirect-pdf/$1 [QSD,L]
    
    RewriteEngine On
    
    # Set an env var to the base target
    # (Just saves some bytes/repetition and easier to update)
    RewriteRule ^ - [E=TARGET_BASE:https://www.test2.demoguru99.com/pdf]
    
    # Redirects
    RewriteRule ^expertadvance-level-qtp-uft-interview-questions$ %{ENV:TARGET_BASE}/qtp.pdf [R=302,L]
    RewriteRule ^top-35-advanced-software-testing-questions$ %{ENV:TARGET_BASE}/testing.pdf [R=302,L]
    # etc.
    # :
    
    # If get to the end and nothing matched above then fail with a 404...?
    
    # Redirects
    RewriteRule ^expertadvance-level-(qtp)-uft-interview-questions$ %{ENV:TARGET_BASE}/$1.pdf [R=302,L]
    RewriteRule ^top-35-advanced-software-(testing)-questions$ %{ENV:TARGET_BASE}/$1.pdf [R=302,L]