1

I have a problem with the Apache URL rewriting, I want when the user tapes an url like this : 1 : www.site.com/country/city/smthg

or 2 : www.site.com/country/city/smthg/fct

or 3 : www.site.com/country/city/smthg/fct/value

I want to transform the url to this:

1 : www.site.com/index.php/smthg/index/country/city

2 : www.site.com/index.php/smthg/fct/country/city

3 : www.site.com/index.php/smthg/fct/value/country/city

I am using PHP Codeigniter framework,

I tried this but it is not working :

Options -Indexes

RewriteEngine on

RewriteCond $1 !^(index\.php|assets/|robots\.txt)

RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php/$3/$1/$2 [L]

RewriteCond $1 !^(index\.php|assets/|robots\.txt)

RewriteRule ^(.*)$ index.php/$1 [L]

2 Answers 2

3

Use this in .htaccess:

RewriteEngine on
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

and in application->config->routes.php add routes like this

$route['country/(:any)/(:any)/(:any)/(:any)'] = 'ControllerName/functionName/$1/$2/$3';

Replace 'ControllerName' and 'functionName' with the actual names you have. $1, $2 and $3 becomes function parameters.

For details CodeIgniter URI Routing

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

3 Comments

For me Country and City are variables, and i have more than one controller
this worked for me: $route['(:any)/(:any)/(:any)'] = '$3/index/$1/$2'; $route['(:any)/(:any)/(:any)/(:any)'] = '$3/$4/$1/$2'; $route['(:any)/(:any)/(:any)/(:any)/(:any)'] = '$3/$4/$5/$1/$2'; Thx ^^
We can use any number of routes. I always use explicit routes. If I have 3 controllers named a, b, c and want to hit the index function with 2 variables; one string and second integer. I will use: $route['a/index/(:any)/(:num)'] = 'a/index/$1/$2'; $route['b/index/(:any)/(:num)'] = 'b/index/$1/$2'; $route['c/index/(:any)/(:num)'] = 'c/index/$1/$2';
0

Try :

Options -Indexes

RewriteEngine on

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php/$3/index/$1/$2 [NC,L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php/$3/$4/$1/$2 [NC,L]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php/$3/$4/$5/$1/$2 [NC,L]

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.