【问题标题】:redirect with variable using .htaccess使用 .htaccess 使用变量重定向
【发布时间】:2014-07-23 12:38:34
【问题描述】:

我需要重定向自:

http://my-site.com/index.php?ean={any_number}

例如:http://my-site.com/index.php?ean=123

到:

http://my-site.com/b={any_number}

例如:http://my-site.com/b=123

请注意,我需要使用REDIRECT 函数,而不是RewriteRule

有什么想法吗? 非常感谢!

【问题讨论】:

  • 是使用 PHP 在 index.php 中重定向选项还是必须通过 .htaccess
  • 你能告诉我们你自己尝试了什么吗?你.htaccess的内容是什么?
  • 你有什么尝试?为什么只有RESIRECT
  • index.php选项中使用PHP重定向,但我更喜欢.htaccess选项。
  • 我试过RewriteRule选项,但我需要REDIRECT函数。

标签: php html .htaccess redirect


【解决方案1】:

试试这个

 RewriteCond %{REQUEST_URI}  ^index.php$
 RewriteCond %{QUERY_STRING} ^ean=([0-9]+)$
 RewriteRule ^(.*)$ http://my-site.com/b=%1 [R,L]

【讨论】:

  • 它是重定向到任何东西还是根本不重定向
  • 我需要REDIRECT 函数,而您使用RewriteRule 函数。无论如何,它不起作用。不过谢谢!
  • @ins0,我可以在我的.htaccess 中使用它吗?似乎应该通过 apache 设置进行配置,但我无权访问这些设置。
  • 你设置了 RewriteEngine 吗?
  • 您确定您的目录中忽略了.htaccess 文件吗?尝试在您的公共文件夹中放置一个带有随机单词的.htaccess 文件,如果您收到Internal Server Error 消息,您可以使用.htaccess 文件,否则这种广泛的重定向真的很难实现
【解决方案2】:

使用 PHP,您可以使用 header() 执行重定向。


<?php 
//--- index.php snippet ---

if (isset($_GET['ean'])) {
  header("Location: http://yoursite.com/b=".intval(trim($_GET['ean'])));
  exit(0);
}

如果您基于 URI 而不是参数进行重定向,则可以使用 RedirectMatch.htaccess 中执行重定向(即 yoursite.com/ean/12345)。

.htaccess 的代码如下所示:

RedirectMatch 301 /ean/(.*) /b=$1

【讨论】:

    【解决方案3】:

    所以,最后我决定使用PHP 中的header(Location: www.your-site.com) 函数进行重定向。谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多