Pregunta

I want to rewrite URL using .htaccess in codeigniter but it's not working. Can you please figure it out. Want to change URL from:

www.site.com/mauritius_holiday_rentals/apartment/anyname

to

www.site.com/mauritius_holiday_rentals/anyname

My current .htaccess file contains:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA] 
RewriteRule ^mauritius_holiday_rentals/([A-Za-z0-9-]+)/?$ mauritius_holiday_rentals/apartment/$1 [L,QSA]

First four line is for removing index.php from URL which is working fine.

¿Fue útil?

Solución

If routes file is set to access new url and you want to set redirection for old URLs then Use following code in .htaccess. otherwise let me know in detail what you want to do.

RewriteEngine on
RewriteBase /
RewriteRule /mauritius_holiday_rentals/apartment/$1 /mauritius_holiday_rentals/$1 [R=301,L]

Routes.php config file code

$route['mauritius_holiday_rentals/(:any)']="mauritius_holiday_rentals/apartment/$1";

Let me know if any problem.

Otros consejos

 RewriteEngine On    
 RewriteBase /yourProjectName/    
 RewriteCond $1 !^(images|stylesheets|javascript)    
 RewriteCond %{REQUEST_FILENAME} !-f    
 RewriteCond %{REQUEST_FILENAME} !-d    
 RewriteRule ^(.*)$ /yourProjectName/index.php?/$1 [L]

Try to use codeigniter routing system https://www.codeigniter.com/user_guide/general/routing.html

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top