普通网友 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 SQL Server下载
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求