0

I'd like to know how to do the URL rewriting using .htaccess file. My web application is based on PHP CodeIgniter and I am going to implement the SEF function.

Current URL is

http://localhost/index.php/sale/list

In this URL, "sale" is a controller name and "list" is a method name. I'd like to replace the URL with http://localhost/index.php/product-sales/list

I would think this can be done easily with .htaccess file if you know .htaccess well.

Thanks for your attention and time. Regards,

6
  • Why not change your routing in the routes file. codeigniter.com/user_guide/general/routing.html Commented Sep 15, 2019 at 16:24
  • Hi Daniel, I know I can change it in routing. But there are already many old urls on db. So I'd like to solve this with .htacess file. Commented Sep 15, 2019 at 16:27
  • Do you want to redirect only 1 link or multiple based on some parameters? Commented Sep 15, 2019 at 16:57
  • Hi Mikeyhun, I'd like to change the controller's part. I think it is 1 link. e.g. sale/list, sale/update, sale/search?order_by=xxx, etc. The above urls should be replaced with product-sales/list, product-sales/udpate and product-sales/search?order_by. Commented Sep 15, 2019 at 17:04
  • 1
    There are many tutorials on this: wpscholar.com/blog/simple-redirects-with-htaccess ... But I wouldn't go in that direction but start typing away in your routing system. You only put more dirt in your app if you don't fix the thing where it should be fixed. Commented Sep 15, 2019 at 17:05

2 Answers 2

1

Put this into your .htaccess file and will rewrite everything that's index.php/sale/... to index.php/product-sales/...

RewriteEngine on

RewriteRule ^index\.php/sale/(.+)$ index.php/product-sales/$1 [R=301,L]

BUT I agree with @Daniel that it might be better to fix it in system rather than in htaccess

Edit: if i understand it right now, you just want to have a new url for the same controller so after you added the upper rule to your htaccess (so you redirect the old url's to the new one) you still need to add the new routes to the routes file like:

$route['product-sales/(:any)'] = 'sale/$1';

It should work now, if that's not the desired effect then explain more pls...

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your reply. But I couldn't get it solved. <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index.php/orders/(.+)$ index.php/product-sales/$1 [R=301,L] RewriteCond %{HTTP_HOST} ^demo RewriteCond %{HTTP_HOST} phppointofsale.com RewriteRule ^robots\.txt$ robots_allow.txt </IfModule>
Additionally, I am running the Codeigniter on the local virtual host.@Mikeyhun
Wait! You still using the 'sale' controller and want to redirect everything from /sale/ and /product-sales/ to the 'sale' controller or you made a new controller? I'm a bit confused now (also that you wrote /orders/ to rewrite to 'product-sales'
sorry for confusing. But I think there is no difference between controller names orders and sale on this topic.
0

All routing is defined in route.php file.

Navigate to your project. Open application/config/routes.php file.

$route['sale/list'] = 'product-sales/list/';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.