Search code examples
php.htaccesscodeigniterhttp-redirecthttp-status-code-301

Redirect /index.php?/bitcoin to /bitcoin - Codeigniter 301 redirect issue


I've been struggling with some htaccess redirects. I just spent some time reading and searching on stack and couldn't get an answer that works with my scenario.

I'm in the process of making the 301 redirect for an old client website to a new one.

This is old page URL

https://digitalcoinprice.com/index.php?/first-bitcoin

to

https://digitalcoinprice.com/first-bitcoin

I have multiple pages to do, here's a couple example:

index.php?/msd
index.php?/bitcoin
index.php?/ripple

Which all link to different new pages.

Here's what I tried:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

I get redirected correctly, but the URL keeps the query string like this :

https://digitalcoinprice.com/?/first-bitcoin

It didn't work (Still have the query string)

Thanks!


Solution

  • At the the top of your htaccess, put the following rule :

    RewriteEngine on
    
    
    RewriteCond %{THE_REQUEST} /index\.php\?/(.+)\sHTTP  [NC]
    RewriteRule ^.*$ https://digitalcoinprice.com/%1? [L,R]
    

    This will redirect https://digitalcoinprice.com/index.php?/any-value to https://digitalcoinprice.com/any-value .