Reputation: 53
I have just now upgraded one of my static website to a codeIgniter enabled website. I need to redirect the exact domain names of the old website to the new ones to avoid the 404 error.
http://example.com/pagename.php
http://example.com/index.php/home/category_images/9(cat_id)
I do not a very high no. of pages so hard coding all the pages directly will not be a problem. I have tried :
RewriteEngine On
RewriteBase /
RewriteRule ^http://www.example.com/pagename.php$ http://example.com/index.php/home/category_images/9 [L,R=301]
Not working, I have no idea why. mod_rewrite
is enable on my apache server.
Also, just for the confirmation please also tell me in which folder I should put this file, I am confused between root dir or Application folder.
Thank you.
Version 2 : Now after suggestion from Mr. Michael, I tried to implement it using the redirect function like this :
Default Controller function : 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);
}
}
}
And my .htaccess looks like :
RewriteEngine On
RewriteBase /
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
PS : I have removed index.php from my domain name structure. It looks like : www.example.com/controller/function/uri/uri
Now I am not getting an error page from my host, but I am getting an error page from the CodeIgniter.
Please help.
Upvotes: 1
Views: 5333
Reputation: 9007
Considering that your Hypertext Access file should be routing everything through index.php
anyway, it is best to handle your redirects using CodeIgniter's redirect()
function.
An example is provided below, based on what I have in my Page controller, in my own framework extensions (it can be used anywhere where the redirect()
function is available, as well as $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);
}
}
}
If you were to pass a query to your pagename.php
file in your framework, you could consider the following (note that I do not know what the intention of your pages are - this is a generic solution):
$redirects = Array(
'pagename.php?q=(:any)' => 'home/category_images/$1'
);
For example:
http://example.com/pagename.php?q=9
would redirect you to:
http://example.com/home/category_images/9
This solution works for me quite well, and is very efficient when it comes to cross-browser compatibility.
Note that your RewriteRules should look like this, or something similar to it:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (xml|txt|css|js|jpg|png|gif)$ - [L]
RewriteRule .* index.php [L,QSA]
Edit: The above has been changed to accommodate for assets. Basically, it tells Apache to route everything through
index.php
, unless it is a file that exists AND has the extensions specified. (This is good practice for security purposes.)Edit: Note that you should not include
index.php
in your URL as it is being stripped by the.htaccess
file anyway.
Note that I use PHP 5.3 - though this solution should work for you. Please let me know if there are any problems.
Upvotes: 3
Reputation: 7762
you can open you xampp/apache/conf/httpd.conf
After then check below line
#LoadModule rewrite_module modules/mod_rewrite.so
if Hash (#) exist in above the line. then remove it.
LoadModule rewrite_module modules/mod_rewrite.so
and restart your apache server and check you site.
After then site not working please let me know.
Upvotes: 0
Reputation: 71414
You don't use the whole domain name in the match portion of the rewrite rule. Try this:
RewriteRule ^pagename.php$ /index.php/home/category_images/9 [L,R=301]
Of course you will likely need other rules for the server to understand that index.php is the file that is to be called, not something at /image.php/home/category_images/9
So perhaps a second rule like
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^index.php/(.*) /index.php?q=$1 [L]
I just noticed that in your updated answer you are trying to redirect from www.example.com to example.com. If you do want to force one domain, you should probably add another rule in your .htaccess before either of the other rules mentioned like this:
RewriteCond %{HTTP_HOST} ^www.example.com$
Rewrite Rule ^(.*)$ http://example.com/$1 [L,R=301]
Upvotes: 0