我刚刚将我的一个静态网站升级为启用了codeIgniter的网站。我需要将旧网站的确切域名重定向到新网站,以避免404错误。
- 旧页面>
http://example.com/pagename.php
- 新建页面>
http://example.com/index.php/home/category_images/9(cat_id)
我的页数不是很高,所以直接对所有页面进行硬编码不会有问题。我试过:
RewriteEngine On
RewriteBase /
RewriteRule ^http://www.example.com/pagename.php$ http://example.com/index.php/home/category_images/9 [L,R=301]
不工作,我不知道为什么。在我的apache服务器上启用了mod_rewrite
。
此外,为了确认,还请告诉我应该把这个文件放在哪个文件夹中,我混淆了根目录和应用程序文件夹。
谢谢。
版本2:根据Michael先生的建议,我尝试使用如下重定向函数来实现它:
默认控制器功能:home.php
function __construct()
{
// Call the Model constructor
parent::__construct();
$this->load->model('common_model');
$this->handle_redirects();
}
function handle_redirects ()
{
$redirects = Array(
'gorgeous-girls.php' => 'home/category_images/9'
);
$uri = implode('/', $this->uri->segments);
foreach ($redirects as $from => $to)
{
$from = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $from));
if (preg_match("#^{$from}$#i", $uri))
{
if (strpos($to, '$') !== false and strpos($from, '(') !== false)
$to = preg_replace("#^{$from}$#i", $to, $uri);
redirect($to , 'location', 301);
}
}
}
我的.htaccess看起来像:
RewriteEngine On
RewriteBase /
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
附言:我已经从我的域名结构中删除了index.php。它看起来像:www.example.com/controller/function/uri/uri
现在,我没有从主机获得错误页面,但我从CodeIgniter获得了错误页面。
请帮忙。
考虑到您的超文本访问文件无论如何都应该通过index.php
路由所有内容,最好使用CodeIgniter的redirect()
函数来处理重定向。
下面提供了一个示例,基于我在Page控制器中的内容,在我自己的框架扩展中(它可以在redirect()
功能可用的任何地方使用,以及$this->uri->segments
function handle_redirects () {
$redirects = Array(
'pagename.php' => 'home/category_images/9'
);
$uri = implode('/', $this->uri->segments);
foreach ($redirects as $from => $to)
{
$from = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $from));
if (preg_match("#^{$from}$#i", $uri))
{
if (strpos($to, '$') !== false and strpos($from, '(') !== false)
$to = preg_replace("#^{$from}$#i", $to, $uri);
redirect($to , 'location', 301);
}
}
}
如果您要将查询传递到框架中的pagename.php
文件,您可以考虑以下内容(请注意,我不知道您的页面的意图是什么-这是一个通用解决方案):
$redirects = Array(
'pagename.php?q=(:any)' => 'home/category_images/$1'
);
例如:
http://example.com/pagename.php?q=9
会将您重定向到:
http://example.com/home/category_images/9
这个解决方案对我来说效果很好,而且在跨浏览器兼容性方面非常有效。
请注意,您的RewriteRules应该是这样的,或者类似的东西:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (xml|txt|css|js|jpg|png|gif)$ - [L]
RewriteRule .* index.php [L,QSA]
编辑:以上内容已更改以适应资产。基本上,它告诉Apache通过
index.php
路由所有内容,除非它是一个存在并且指定了扩展名的文件。(出于安全目的,这是一种良好的做法。)编辑:请注意,您不应在URL中包含
index.php
,因为它无论如何都会被.htaccess
文件剥离。
请注意,我使用的是PHP 5.3,不过这个解决方案应该适用于您。如果有任何问题,请告诉我。
在重写规则的匹配部分不使用整个域名。试试这个:
RewriteRule ^pagename.php$ /index.php/home/category_images/9 [L,R=301]
当然,您可能需要其他规则让服务器了解index.php是要调用的文件,而不是位于/image.php/home/category_images/9 的文件
所以也许还有第二条规则,比如
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^index.php/(.*) /index.php?q=$1 [L]
我刚刚注意到,在你更新的答案中,你正试图从www.example.com重定向到example.com。如果你确实想强制一个域,你可能应该在.htaccess中添加另一个规则,然后再添加其他规则:
RewriteCond %{HTTP_HOST} ^www.example.com$
Rewrite Rule ^(.*)$ http://example.com/$1 [L,R=301]
您可以打开examplep/apache/conf/httpd.conf
然后检查线下
#LoadModule rewrite_module modules/mod_rewrite.so
如果Hash(#)存在于行上方。然后将其移除。
LoadModule rewrite_module modules/mod_rewrite.so
重新启动您的apache服务器并检查您的站点。
之后网站不工作,请告诉我。