普通网友 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 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!