How to change the url in codelginter

2019-08-02 09:11发布

问题:

My url is http://mydomain.com/controllername/method/ .

Now I need the site url like. http://mydomain.com/method.

mydomain name wants to access as a controller name . for example. url like www.testing.com/pages. 'testing' act as a controller in codeigniter.

Explain how to get the controller name from the domain name. and also how to remove the controller name(www.mydomain.com/controllername/method) from the url.

I need the url like www.controllername.com/method

回答1:

In application/config/routes.php use this code

$route['method'] = 'controllername/method';

for routing.



回答2:

Use Routes. Routes in codeigniter



回答3:

You have to define routes in

application/config/routes.php.

http://ellislab.com/codeigniter/user-guide/general/routing.html

This may help.



回答4:

IMO this would best be done with your .htaccess file instead of CI routes. I am not the best with these rules, so typo may exist, this is the general idea:

// If HOST contains www, strip www.
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

// If URI contains DOMAIN.COM, it was probably just redirected, dont do it again!
RewriteCond %{REQUEST_URI} !^%{HTTP_HOST}
// Re-write http://domain.com/method to http://domain.com/domain.com/method
RewriteRule (.*) http://%{HTTP_HOST}/%{HTTP_HOST}/$1 [L]