【问题标题】:Sending GET value to URL without parameters and extra characters?在没有参数和额外字符的情况下向 URL 发送 GET 值?
【发布时间】:2013-10-18 14:55:21
【问题描述】:

我目前正在开发一个 CodeIgniter 应用程序,但我需要搜索功能对人类和 CodeIgniter 都更具可读性。

这是我目前在提交表单时得到的 URL。

http://mydomain.com/search/?query=batman

这就是我希望得到的结果。

http://mydomain.com/search/batman

我不擅长重写模组,但我想这就是我需要走的路?

感谢您的帮助。

【问题讨论】:

  • @BeatAlex 这不是 +1 的用途,但是......
  • 我不会这样做,使用GET 字符串是进行搜索的正确方法。通过这种方式,您可以告诉搜索引擎和您的分析工具跟踪或忽略这些页面。

标签: php codeigniter mod-rewrite


【解决方案1】:

你应该使用 mod_rewrite,在你的脚本路径中创建一个脚本 .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ /index.php?query=$1 [L]
</IfModule>

查看 mod_rewrite 文档了解更多信息 http://httpd.apache.org/docs/current/mod/mod_rewrite.html

【讨论】:

  • 我喜欢这种方法的外观,说实话,我已经尝试了所有方法,但目前还没有任何效果。这是我的 .htaccess ,我已经删除了函数名。 RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php/$0 [PT,L] 我尝试将您的解决方案添加到当前 .htaccess 的底部,但没有任何反应。我注意到脚本中有 /index.php,但我的 URL 看起来像 http://localhost/dev/comiq/search/?search=batman。这是个问题吗?
【解决方案2】:

不是一个很好的方法,但是:

如果您将其设为POST 并将变量放入单独的控制器中会怎样。例如search($s)

public function search($s){
    header('Location:http://yoursite.com/whatever/search/'. $s);
}

这应该会给你你需要的结果。

【讨论】:

  • 这种方法可能不是最干净的,但它确实有效! :) 谢谢。
  • @LewisHill :你应该尝试过我的建议,我看到它不是一个干净的方法(只是一个建议)。我的过程相同,但更适合pagination
【解决方案3】:

首先将form method 更改为POST,然后在您的搜索功能中您可以执行以下操作:

function search(){
    if( $this->input->post(null) ){
        $keyword    = $this->input->post('keyword');
        #do you processing with the post
    }else{
        $keyword    = $this->uri->segment(3);
    }

    #for pagination
    $config['base_url']     = base_url()."controller/search/".$keyword."/page/";
    $config['uri_segment']  = 5;
}

【讨论】:

  • 这很好,但我认为他想坚持使用 GET + @Christopher 在上面指出的原因
【解决方案4】:

您可以使用 javascript 捕获 onsubmit() 事件,然后重定向到您想要的 url..

$('#myform').on('submit', function(e){
    e.preventDefault();
    var value = $('#input').val()
    window.location = 'http://mydomain.com/search/' + value;
})

当然,您的控制器必须如下所示:

class Search extends CI_Controller
{
   public function index($term)
   {
      // code code
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-07
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    相关资源
    最近更新 更多