【问题标题】:htaccess Url rewriting not workinghtaccess Url重写不起作用
【发布时间】:2015-09-12 13:58:07
【问题描述】:

我遇到了 Apache URL 重写的问题, 当用户录制这样的网址时,我想要: 1:www.site.com/country/city/smthg

或 2:www.site.com/country/city/smthg/fct

或 3:www.site.com/country/city/smthg/fct/value

我想把网址改成这样:

1:www.site.com/index.php/smthg/index/country/city

2:www.site.com/index.php/smthg/fct/country/city

3 : www.site.com/index.php/smthg/fct/value/country/city

我正在使用 PHP Codeigniter 框架,

我试过了,但它不起作用:

Options -Indexes

RewriteEngine on

RewriteCond $1 !^(index\.php|assets/|robots\.txt)

RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php/$3/$1/$2 [L]

RewriteCond $1 !^(index\.php|assets/|robots\.txt)

RewriteRule ^(.*)$ index.php/$1 [L]

【问题讨论】:

    标签: php apache .htaccess codeigniter mod-rewrite


    【解决方案1】:

    在 .htaccess 中使用它:

    RewriteEngine on
    RewriteRule ^(application|system|\.svn) index.php/$1 [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [QSA,L]
    

    并在 application->config->routes.php 添加这样的路由

    $route['country/(:any)/(:any)/(:any)/(:any)'] = 'ControllerName/functionName/$1/$2/$3';
    

    将“ControllerName”和“functionName”替换为您拥有的实际名称。 $1、$2 和 $3 成为函数参数。

    详情CodeIgniter URI Routing

    【讨论】:

    • 对我来说国家和城市是变量,我有不止一个控制器
    • 这对我有用:$route['(:any)/(:any)/(:any)'] = '$3/index/$1/$2'; $route['(:any)/(:any)/(:any)/(:any)'] = '$3/$4/$1/$2'; $route['(:any)/(:any)/(:any)/(:any)/(:any)'] = '$3/$4/$5/$1/$2'; Thx ^^
    • 我们可以使用任意数量的路由。我总是使用显式路由。如果我有 3 个名为 a、b、c 的控制器,并且想用 2 个变量访问索引函数;一个字符串和第二个整数。我将使用:$route['a/index/(:any)/(:num)'] = 'a/index/$1/$2'; $route['b/index/(:any)/(:num)'] = 'b/index/$1/$2'; $route['c/index/(:any)/(:num)'] = 'c/index/$1/$2';
    【解决方案2】:

    试试:

    Options -Indexes
    
    RewriteEngine on
    
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php/$3/index/$1/$2 [NC,L]
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php/$3/$4/$1/$2 [NC,L]
    
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php/$3/$4/$5/$1/$2 [NC,L]
    

    【讨论】:

      猜你喜欢
      • 2015-02-03
      • 2014-08-08
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多