【问题标题】:Htaccess Redirect an ID Query String URL to User Friendly URLHtaccess 将 ID 查询字符串 URL 重定向到用户友好 URL
【发布时间】:2017-06-08 14:00:43
【问题描述】:

我正在尝试将带有 ID 查询字符串的页面重定向到用户友好的 URL。我有多个这样的页面,我需要在 htaccess 文件中重定向。这是我尝试重定向的页面之一的示例:

旧网址: examplepage.com/about_news_info.aspx?id=275

重定向到

新网址: examplepage.com/news-events/

这是我没有运气使用的:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=275$
RewriteRule ^/about_news_info.aspx http://examplepage.com/news-events/test1 [L,R=301]

RewriteCond %{QUERY_STRING} ^cateid=373$
RewriteRule ^/about_news.aspx http://examplepage.com/news-events/test2 [L,R=301]

【问题讨论】:

    标签: .htaccess mod-rewrite redirect


    【解决方案1】:

    .htaccess 是 per directory 上下文与将其放在配置文件中,因此您的模式不匹配。

    来自文档:

    在 .htaccess 文件中使用重写引擎时,每个目录 前缀(对于特定目录始终相同)是 自动删除 RewriteRule 模式匹配和 在任何亲戚之后自动添加(不以斜杠或 协议名称)替换遇到规则集的结尾。见 RewriteBase 指令以获取有关将使用什么前缀的更多信息 被添加回相对替换。

    去掉的前缀总是以斜杠结尾,表示匹配 发生在一个没有前导斜杠的字符串上。 因此, 带有 ^/ 的模式在每个目录的上下文中从不匹配。

    所以你的规则应该看起来像这样没有 RewriteRule 中的前导/

    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^id=275$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^about_news_info.aspx http://examplepage.com/news-events/test1 [L,R=301]
    
    RewriteCond %{QUERY_STRING} ^cateid=373$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^about_news.aspx http://examplepage.com/news-events/test2 [L,R=301]
    

    不检查确实不需要的查询字符串。

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^about_news_info.aspx$ http://examplepage.com/news-events/test1? [L,R=301]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^about_news.aspx$ http://examplepage.com/news-events/test2? [L,R=301]
    

    【讨论】:

    • 我尝试了您提供的代码,但 URL 仍未重定向并转到 404 页面。
    • 我很好奇你有 aspx 页面的 .htaccess 代码在哪里?这通常在 IIS 服务器而不是 apache 上运行。
    • 此站点在 Windows 服务器上,但现在在 apache 服务器上,我希望所有旧 URL 都重定向到新页面,这样我们就不会从 Google 获得更多未找到的页面网站管理员工具。
    • 您无需担心查询字符串。只要确保文件匹配,无论查询字符串如何,它都会重定向。查看我更新的代码。
    • 每个 URL 有多个查询字符串。例如,examplepage.com/about_news_info.aspx?id=275 和 examplepage.com/about_news_info.aspx?id=325 分别转到单独的页面。所以我需要指定查询字符串。感谢您的帮助。
    【解决方案2】:
    RewriteEngine On
    
    RewriteCond %{QUERY_STRING} ^id=275$
    RewriteRule ^about_news_info.aspx$ http://examplepage.com/news-events/test1? [L,R=301]
    
    RewriteCond %{QUERY_STRING} ^cateid=373$
    RewriteRule ^about_news.aspx$ http://examplepage.com/news-events/test2? [L,R=301]
    

    【讨论】:

      猜你喜欢
      • 2011-08-13
      • 2014-12-03
      • 2014-02-24
      • 2022-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-03
      • 2019-10-31
      相关资源
      最近更新 更多