【问题标题】:Url hide using codeigniter使用 codeigniter 隐藏网址
【发布时间】:2023-03-06 03:51:01
【问题描述】:

我正在使用 codeigniter...我想要一个来自以下 URL 的干净 URL

  http://localhost:8080/rtvnews/index.php/home/videonews?id=67598/newstitle 

here home => 控制器,videonews => 函数和 ?id=6586565 是一个 url 字符串。

我想删除 /index.php/home/videonews?id=67598 并替换为 /news/

下面我需要获取的最终网址

  http://localhost:8080/rtvnews/news/newstitle 

【问题讨论】:

  • 无论您做什么,都必须通过 ID。但是传递方式可以改变
  • 您应该尝试阅读清楚解释路由和 URL 格式的文档。我也不明白当 CodeIgniter 的默认值相反时,你怎么能有一个带有查询字符串的 URL。

标签: php .htaccess codeigniter


【解决方案1】:

按照以下步骤操作

第 1 步:.htaccess(位于根文件夹的那个)

Options All -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

第二步:routes.php 在下面添加代码

$route['rtvnews/news/newstitle'] = 'Your controller/method']; //Just a syntax to change the route in codeigniter. You can change the url as per you want.

【讨论】:

    【解决方案2】:

    要从 url 中隐藏 index.php,请使用以下 htaccess

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    

    More on htaccess

    URI Routing

    认为新闻标题是独一无二的:

    转到您的 route.php 文件 (application/config/routes.php) 。添新 路由规则如下

    $route['rtvnews/news/(:any)'] = 'home/videonews/$1';
    

    在您的视图文件中

    <a href="<?php echo base_url()."rtvnews/new/".$newstitle; ?>" > News Title</a>
    

    所以你的网址如下

    http://localhost:8080/rtvnews/news/uniquenewstitle  
    

    您的请求转到 home/vidonews 功能,您可以在其中获取您的 newstitle 作为参数。

    在你的controller.php函数中会是这样的

    function vidonews($newsTitle){}
    

    认为 id 是唯一的:

    如下添加新的路由规则

    $route['rtvnews/news/(:num)'] = 'home/videonews/$1';
    

    在view.php文件中

    <a href="<?php echo base_url()."rtvnews/new/".$newsId; ?>" > News Title</a>
    

    所以你的网址如下

    http://localhost:8080/rtvnews/news/newsId
    

    现在您的请求转到 home/vidonews 功能,您可以在其中获取您的 newsId 作为参数。在你的controller.php 函数中会是这样的

    function vidonews($newsId){}
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多