【问题标题】:URL Rewriting in php CodeIgniter using .htaccess使用 .htaccess 在 php CodeIgniter 中重写 URL
【发布时间】:2015-06-17 02:25:11
【问题描述】:

我正在做一个 URL 重写的项目。这是旧uri的链接

http://www.mysite.pk/jobs/search/faisalabad/all-cats

但现在在新网址中我已将其更改为

http://www.mysite.pk/jobs/pakistan/faisalabad/all-cats

我想要做的是将所有具有http://www.mysite.pk/jobs/search/ 搜索后工作的 url 的重定向更改为

http://www.mysite.pk/jobs/pakistan/

这是我的路线代码

$route["jobs/pakistan"]="vacancies/search";

我也做过

$route["search/pakistan"]="vacancies/search";

我的 .htaccess 文件内容是

但它不起作用 请帮助我 提前致谢

【问题讨论】:

  • 有一个非常好的 htaccess 测试器可以帮助你在这里生成你的 htaccess htaccess.madewithlove.be
  • 你真的需要第一个重写条件和规则吗?有一种更直接的方法。
  • @berkyl 是的,我真的需要,我希望能解决
  • @Usama 据我解释,您使用它来提供一些文件夹的内容,这样它们就不会被重写为 index.php,对吗?
  • 我不知道详细信息,但我认为是的

标签: php apache .htaccess codeigniter mod-rewrite


【解决方案1】:

我认为问题在于缺少占位符。如果没有占位符,您声明的 URL

http://www.mysite.pk/jobs/search/faisalabad/all-cats

不匹配。

正确的路线应该是这样的:

$route["jobs/pakistan/(:any)"]="vacancies/search";
$route["search/pakistan/(:any)"]="vacancies/search";

这样,jobs/pakistan 和 search/pakistan 都可以正常工作。

编辑

这个 RewriteRule 在放入 .htaccess 文件时会产生同样的效果。

RewriteRule ^jobs/search/(.*)?$ http://www.mysite.pk/jobs/pakistan/$1 [L,R=301]

您的 .htaccess 文件应如下所示:

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /

    # Removes trailing slashes (prevents SEO duplicate content issues)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ $1 [L,R=301]

    ###

    RewriteRule ^jobs/search/(.*)?$ http://www.mysite.pk/jobs/pakistan/$1 [L,R=301]

    # Removes access to the system folder by users.
    # Additionally this will allow you to create a System.php controller,
    # previously this would not have been possible.
    # 'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php/$1 [L]

    # Checks to see if the user is attempting to access a valid file,
    # such as an image or css document, if this isn't true it sends the
    # request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]

</IfModule>

<IfModule !mod_rewrite.c>

    # Without mod_rewrite, route 404's to the front controller
    ErrorDocument 404 /index.php

</IfModule>

RewriteRule 有以下方案:

RewriteRule [输出] [原始]

在这种情况下,当“输出”(url)以jobs/search/ 开头(^ 标记开始,$ 结束)之后的所有内容都将被重定向到原始文件。 (.*) 标记任何内容的引用,并将插入到源中以代替 $1[L,R=301] 标记重定向的类型(HTTP 响应状态码 301 表示已永久移动),L 表示路由满足后,以下行将不被处理。

【讨论】:

  • 是他们的任何方式,我无法使用 .htaccess @berkul 重写它
  • @Usama 是的,您可以使用重写规则,但我会避免这样做,因为这样您必须查看多个位置以防出错,而不是检查 routes.php 并进行概述。
  • 请指导我如何使用 .htaccess 来做到这一点
  • 抱歉回复晚了,我因为个人原因离开了。我已经实现了它,但它不起作用,请你再看一遍。
  • @usama 你能提供你的.htaccess 的目录结构和内容吗?你在哪里添加了重写规则?
猜你喜欢
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 1970-01-01
  • 2019-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多