.htaccess Htaccess规则使URL像这样?page=1看起来像Codeigniter中的/page/1
以下是我的URL当前的外观:.htaccess Htaccess规则使URL像这样?page=1看起来像Codeigniter中的/page/1,.htaccess,codeigniter,url,mod-rewrite,friendly-url,.htaccess,Codeigniter,Url,Mod Rewrite,Friendly Url,以下是我的URL当前的外观: http://mysite.com/?page=1 我怎样才能做到这一点 http://mysite.com/page/1 有一个关于StackOverflow的问题问了同样的问题。但被接受的解决方案对我不起作用。因为我使用的是Codeigniter,我的页面结果是404,可能是因为CI站点的url模式是: 域/控制器/方法 系统假设我正在请求一个名为“page”的控制器和一个名为“1”的方法,这两者当然都不存在。或者可能是由于与我的htaccess文件中的其他
http://mysite.com/?page=1
我怎样才能做到这一点
http://mysite.com/page/1
有一个关于StackOverflow的问题问了同样的问题。但被接受的解决方案对我不起作用。因为我使用的是Codeigniter,我的页面结果是404,可能是因为CI站点的url模式是:
域/控制器/方法
系统假设我正在请求一个名为“page”的控制器和一个名为“1”的方法,这两者当然都不存在。或者可能是由于与我的htaccess文件中的其他代码冲突(我从CI wiki下载了该文件,它去掉了index.php并执行了一些安全操作)。这是我的整个htaccess文件:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users. Additionally this will allow you to create a System.php controller, 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder. This snippet prevents user access to the application folder. Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
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]
#Pretty urls for pagination links
RewriteRule (.*) index.php?page=$1
</IfModule>
htaccess规则将如何将其转换为
http://mysite.com/page/2
从
这将处理删除
index.php
,但之后会发生什么取决于CodeIgniter的查询字符串处理设置:可以将其配置为使用查询字符串而不是路径。有关更多详细信息,请参见链接。您应该在/application/config/routes.php
中进行此配置(并且让.htaccess
只需像您已经做的那样隐藏index.php)
或者更好,比如@zaherg membered(确保只有数字可以匹配):
这样,所有对http://mysite.com/page/2
将在内部被视为http://mysite.com/home/index/2
等等
我建议你看一看,然后
祝你好运。CI不会在有人无意中发现正在执行的实际文件时执行301操作,对吗?但如果使用
(:num)
这将匹配只包含数字的段,效果会更好。因此,您的路由将是$route['page/(:num)]='home/index/$1'代码>
http://mysite.com/page/2
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
$route['page/(:any)'] = 'home/index/$1';
$route['page/(:num)'] = 'home/index/$1';