问题
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]
回答1:
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
回答2:
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]
来源:https://stackoverflow.com/questions/32532856/htaccess-url-rewriting-not-working