【问题标题】:CodeIgniter URL rewriting not workCodeIgniter URL 重写不起作用
【发布时间】:2016-07-05 11:24:33
【问题描述】:

我的系统被锁定了... 我将 config.php 文件中的设置更改为具有带参数的 URL。 突然我有了一个产品控制器,它显示我传递 URL 参数的 id。

http://localhost:8888/mywebsite/index.php?c=product&m=index&id_product=12

这个网址现在可以工作了我想得到一个类型的网址:

http://localhost:8888/mywebsite/product/my-product-12

所以我把它放在 .htaccess 文件中:

RewriteRule ^product/([a-zA-Z0-9\-]+)-([0-9]+).html$ index.php?c=product&m=index&id_product=$2 [L]

但页面显示:请求的 URL /index.php/product/my-product-12.html 在此服务器上未找到

我的 htaccess 文件:

#Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1         [L,QSA]

RewriteRule ^product/([a-zA-Z0-9\-]+)-([0-9]+).html$ index.php?c=product&m=index&id_product=$2 [L]

我的 config.php 文件:

<?php

$config['base_url'] = 'http://localhost:8888/mywebsite/';
$config['index_page'] = '';
$config['uri_protocol'] = 'QUERY_STRING';
$config['url_suffix'] = '';
$config['enable_hooks'] = TRUE;
$config['allow_get_array']      = TRUE;
$config['enable_query_strings'] = TRUE;
$config['controller_trigger']   = 'c';
$config['function_trigger']     = 'm';
$config['directory_trigger']    = 'd'; 
$config['rewrite_short_tags'] = FALSE;

?>

【问题讨论】:

    标签: php .htaccess codeigniter mod-rewrite


    【解决方案1】:

    将新的 RewriteRule 放在 RewriteBase 之后:

    RewriteEngine on
    RewriteBase /
    
    RewriteRule ^product/([a-zA-Z0-9\-]+)-([0-9]+)$ index.php?c=product&m=index&id_product=$2 [L]
    
    RewriteCond $1 !^(index\.php|public|\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1         [L,QSA]
    

    因为你必须先重写为index.php?c=product

    【讨论】:

    • 感谢您的回答!我这样做了,我的页面中有这个错误消息:未找到在此服务器上找不到请求的 URL /index.php。
    • 立即尝试不使用.html。那在您的代码中,但不在您的示例网址中。
    • 同样的结果...我不明白 :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    • 1970-01-01
    • 2016-12-18
    • 2023-03-24
    相关资源
    最近更新 更多