普通网友 2015-06-17 02:25
浏览 224
已采纳

使用.htaccess在PHP CodeIgniter中重写URL

I am working on a project in which url Rewriting was done. Here is the link of the old uri

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

But now in the new url I have changed it to

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

what I want to do is to change the redirect all the url's whcih have http://www.mysite.pk/jobs/search/ search after job in them to the

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

Here is my code of the route

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

I have also done

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

My .htaccess file content is

but it is not working Kindly Helpe me Thanks in advance

  • 写回答

1条回答 默认 最新

  • doupijin0397 2015-06-17 11:55
    关注

    I think the problem are the missing placeholders. Without the placeholders your stated URL

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

    won't match.

    The correct route should look like this:

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

    This way both jobs/pakistan and search/pakistan are working.

    Edit

    This RewriteRule will have the same effect when put in the .htaccess file.

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

    Your .htaccess file then should look similar to this:

    <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>
    

    The RewriteRule has following scheme:

    RewriteRule [output] [original]

    In this case when the "output" (url) starts with jobs/search/ (the ^ marks the beginning and $ the end) everything after this will be redirected to the original. The (.*) marks a reference for any content and will be inserted in the origin in place of $1. The [L,R=301] marks the type of the redirect (the HTTP response status code 301 means Moved Permanently), the L means that after the route is met, the following lines won't be processed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动