Howzit Howzit - 3 months ago 8
PHP Question

.htaccess Rewrite to Codeigniter system in its own folder

I built an open-source system without a MVC framework.
It became time to upgrade the system and I rebuilt it into the Codeigniter MVC framework.

The system is located in the /ci folder.
For technical reason it should stay in this folder.

I have set up the correct .htaccess in route folder to make this work:

RewriteEngine on
# Change example.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/ci/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /ci/$1
# Change example.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ ci/index.php [L]
RewriteRule ^/member$ /member [L]
RewriteRule ^/ad_images$ /ad_images [L]
RewriteRule ^/ad_images/130$ /ad_images/130 [L]
RewriteRule ^/ad_images/116$ /ad_images/116 [L]


I am bypassing the member folder as Its still in use and also allow the system to make use of the different ad_images folders.

My problem is this. The old site will have a link that looks like


www.domain.com/vehicles-for-sale


In the new system this link is actually


www.domain.com/categories/1-0-0-0


I will need to do a permanently move the old url to the new.

Why would adding this to the .htaccess file not work and how should I write it do have the desired effect?

RewriteRule ^/vehicles-for-sale$ /ci/categories/index/3-0-0-0 [L]

Answer

You should omit the first slash. And if you want to do a permanent redirect, use the R=301 flag. Furthermore, you are rewriting to ci/categories/index/3-0-0-0 now, instead of the desired categories/1-0-0-0.

This should work for you:

RewriteRule ^vehicles-for-sale$ /categories/1-0-0-0 [L,R=301]
Comments