首页 > 解决方案 > 如何在这些情况下正确重写 htacess?

问题描述

我在 htaccess 中很新,我在这些情况下遇到了一些麻烦

  1. 我只需要对这个特定的 URL 进行 301 重定向:

example.com/jobs 到 example.com/browse-jobs

如果 url 是 example.com/jobs/job1 它不会重定向。

  1. 将所有具有路径 /product 的 URL 重定向到 /package。例如:example.com/product 和 example.com/product/1,所有这些 URL 都会重定向到 example.com/package。

  2. 重写网址:

    example.com/browse-jobs/?job_type=internship 重写为 example.com/browse-jobs/intership。

如果 URL example.com/browse-jobs/?posted=14days&job_type=internship 不要重写并保持不变。

我整天都在与这些案例作斗争,我检查了所有参考网站,但仍然不知道如何使它正确。如果有人知道这件事,请帮助我。谢谢你。

标签: .htaccessurl-rewriting

解决方案


如果您一直在尝试,我建议您清除缓存,也许重新启动 apache:

RewriteBase /
# product, products, product/abc, product/a/b/c to package
# If you don't want products to redirect, add $ after the ?
RewriteRule ^product(/.*)? package [R=301]

# Exactly jobs with or without / to browse-jobs
RewriteRule ^jobs/?$ browse-jobs [R=301]

# If query string only contains parameter job_type with value internship    
RewriteCond %{QUERY_STRING} ^job_type=(internship)$
# With or without slash    
RewriteRule ^(browse-jobs)/?$ $1/%1 [QSD,R=301]
# QSD = Discard the query string

推荐阅读