【问题标题】:Redirect url with question mark and other characters使用问号和其他字符重定向 url
【发布时间】:2014-06-02 08:45:47
【问题描述】:

如何使用 htaccess 文件将此网址重定向到 www.example.com。

used-machinery/en/other/used-machinery-item?000172:compost-turner

这行得通吗?

RewriteRule ^used-machinery/en/other/used-machinery-item?000172:compost-turner?$ http://www.example.com/ [R=301,L]

RewriteRule ^(.*)$ index.php/$1 [L]

会吗?导致旧网址出现问题?

【问题讨论】:

  • 您想使用此规则重写哪些示例 URL?您还希望在浏览器中更改 URL 还是只想静默重写。
  • 如果更容易理解,我已经更新了我的问题

标签: .htaccess redirect


【解决方案1】:

问号应该代表一个查询字符串,因此您必须编写一个条件来检测查询字符串。这应该工作

RewriteEngine On
ReWriteCond %{REQUEST_URI} ^/used-machinery/en/other/used-machinery-item$
ReWriteCond %{QUERY_STRING} ^000172:compost-turner$
RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]

RewriteRule ^(.*)$ index.php/$1 [L]

编辑:

对于一大堆或 URL,您可以使用“OR”标志,甚至可以在服务器端尝试一些东西。这会很痛苦,但我不知道任何其他具有大量链接的解决方案。

RewriteEngine On

# %{THE_REQUEST} format as follows
# GET /used-machinery/en/other/used-machinery-item?000172:compost-turner HTTP/1.1
# important notes: 
#     escape ? with( \?)
#     space after url (\ )
#     each line has [NC,OR] flags for case insensitive and checks this condition or the next
#     last Cond line does not have the OR flag

ReWriteCond %{THE_REQUEST} ^.+\ /used-machinery/en/other/used-machinery-item\?000172:compost-turner\ .*$ [NC,OR]
ReWriteCond %{THE_REQUEST} ^.+\ /anoterURL\?querystring\ .*$ [NC,OR]
ReWriteCond %{THE_REQUEST} ^.+\ /lastURL\?with-no-OR-at-end\ .*$ [NC]
RewriteRule ^(.*)$ http://www.example.com/ [R=301,L]

RewriteRule ^(.*)$ index.php/$1 [L]

如果你想批量编辑,我建议使用Sublime Text

  • 粘贴所有链接,每行一个链接
  • 关闭自动换行
  • 选择一个?在网址中
  • 在菜单中,查找 > 快速查找全部(选择页面上的所有 ?s)
  • 按左光标箭头
    • 输入\
  • 按 End 键转到所有行的末尾
    • 输入 \ .*$ [NC,OR]
  • 按 Home 键转到所有行的开头
    • 键入 ReWriteCond %{THE_REQUEST} ^.+\
  • 编辑最后一行以删除 ,OR

【讨论】:

  • 如果我有多个该类型 url 的实例,我是否必须为每个实例创建一个查询字符串?看起来工作量很大?
  • 归结为您要匹配的模式。如果查询字符串不重要,那么您可以省略查看查询字符串的 ReWriteCond 行。如果需要,那么我们必须看看它应该匹配的模式是什么。
  • 我已经在link 中放入了我的整个重写网址列表。抱歉,如果只是想向您展示我正在处理的事情!
  • 检查上面的编辑。我认为没有简单的方法
  • 如果我需要每个 url 重定向到不同的 url 而不仅仅是 example.com。例如 /used-machinery/en/other/used-machinery-item\?000172:compost-turner\ 重定向到 example.com/machines,/news/news-item?open-days-a-huge-success\ 重定向到 @ 987654325@等
【解决方案2】:

您可以在DOCUMENT_ROOT/.htaccess 文件中使用此代码:

RewriteEngine On

RewriteCond %{QUERY_STRING} 000172:compost-turner [NC]
RewriteRule ^used-machinery/en/other/used-machinery-item/?$ /? [R=301,L]

RewriteRule ^((?!index\.php/).*)$ index.php/$1 [L,NC]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-19
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 2014-01-16
    • 2012-11-09
    相关资源
    最近更新 更多