Welcome Guest, Not a member yet? Register   Sign In
RewriteRule for different controllers
#1

[eluser]catojul[/eluser]
Hello everyone,


I have a RewriteRule which hides the <b>"index.php"</b> plus the main controller (<b>"pages"</b>).

Code:
RewriteEngine on

RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/pages/$1 [QSA]

When a link is followed (e.g. "About Me"), the anchor is something like this

http://www.mywebpage.com/about_me


The RewriteRule that I use allows the directly usage of methods without explicitly mention of the <b>"pages"</b> controller. So far so good.

The problem appears when I want to access a different method.

http://www.mywebpage.com/admin/


How can I write the RewriteRule in order to stop using the <b>"pages"</b> controller when I want to use the <b>"admin"</b> controller?

I know that I could use different folders and write a <b>".htaccess"</b> file for every folder, but is there any other way?
#2

[eluser]rogierb[/eluser]
you could add something like
Code:
RewriteCond %{REQUEST_URI} ^admin.*
RewriteRule ^(.*)$ /index.php/admin/$1 [L]

RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/pages/$1 [QSA]

Which would means admin gets processed before everythin else.
#3

[eluser]cahva[/eluser]
You should use URI routing in CI instead of using .htaccess
#4

[eluser]catojul[/eluser]
@rogierb:

Unfortunately it doesn't work. CI still says "<b>404 Page Not Found</b>".

@cahva:

It doesn't work considering the fact that I have to have the RewriteRule ^(.*)$ ./index.php/pages/$1 [QSA] rule in the .htaccess file (that's for hiding the <b>"index.php"</b>)
#5

[eluser]rogierb[/eluser]
Could you try
Code:
RewriteCond %{REQUEST_URI} ^.*/admin.*$
#6

[eluser]catojul[/eluser]
@rogierb: Well, sir, you are a genius! It worked marvelous and I thank you so much!
#7

[eluser]cahva[/eluser]
You have misunderstood how routing works. Take out the pages/ from .htaccess and just define route so that different pages use your Pages controller. Check out the userguide and you'll see. Routing can be done in various ways. By extending controllers / routing library it would also be possible to make the whole routing dynamic(if the site is large, this is preferred way as you probably dont want to assing all pages one by one to routes).
#8

[eluser]catojul[/eluser]
Thank you <b>cahva</b>! I will definitely look into it!
#9

[eluser]Shay Falador[/eluser]
If you still want to use only HTACCESS you can use a code similar to this:
Code:
RewriteEngine on

RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]*)$ ./index.php/pages/$1 [QSA]
RewriteRule ^(.*)$ ./index.php/$1 [QSA]




Theme © iAndrew 2016 - Forum software by © MyBB