【问题标题】:URL RewriteRule on CodeIgniter .htaccesCodeIgniter .htaccess 上的 URL 重写规则
【发布时间】:2015-02-15 05:15:06
【问题描述】:

我有以下网址:

http://<site>/assets-teste/...

我想把它改写成:

http://<site>/application/views/ambiente_teste/assets-teste/...

我在.htaccess 上尝试了以下操作:

ReweiteRule ^assets-teste/?$ /application/views/ambiente_teste/assets-teste [NC,L]

【问题讨论】:

  • 所以你想把它改写成/assets-teste/application/views/ambiente_teste/?还是要彻底删除/application/views/ambiente_teste/
  • 嘿 kkkk。确切地说,我想将/application/views/ambiente_teste/assets-teste/ 重写为简单的/assets-teste/。或者只是删除/application/views/ambiente_teste/

标签: php regex .htaccess codeigniter url-rewriting


【解决方案1】:

在我看来,您正在使用 CodeIgniter。他们的系统中内置了路由,我建议使用它来更改您的 URL 方案。

CodeIgniter URI Routing

【讨论】:

  • 哦,确实是这样,但是我不明白如何使用routes.php来添加一个直接路由到这个文件夹,所以你能帮帮我吗?
  • 在 applicaiton/config/routes.php 下,添加以下内容:$route['assets-teste/'] = 'controller_name/method'; 基本上,您使用 $route 数组,并添加一个用作您想要的 URI 的键,然后该值设置为用于呈现该页面的方法。这是一个关于 CI 路由的好视频:CodeIgniter Routing: YouTube
【解决方案2】:

首先,你(我)应该使用下划线_作为空格,而不是-

RewriteRule 的第一部分表示在 URL 中找到时要重写的内容。

在 URL 中本地化:

^\/assets-teste\/(.*)

编写新的网址:

/application/views/ambiente_teste/assets-teste/$1

.htaccess 看起来像:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^\/assets-teste\/(.*) /application/views/ambiente_teste/assets-teste/$1 [L]

补充说明:

将启用重写引擎:

RewriteEngine On

重写的基本目录将是 /:

RewriteBase /

如果请求匹配一个不存在的文件,继续:

RewriteCond %{REQUEST_FILENAME} !-f

如果请求匹配一个不存在的目录,继续:

RewriteCond %{REQUEST_FILENAME} !-d

[L]立即停止重写过程,不再应用任何规则。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-06
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多