Welcome Guest, Not a member yet? Register   Sign In
HTACCESS - A Complex Problem
#1

[eluser]vanquish[/eluser]
Hello all,

I have been have some big struggles trying to exclude a particular URL from being rewritten by CodeIgniter.

The Situation:
I have almost all URL requests being routed through my "pages" controller, which looks up the URL segment in the database and returns a page if a match exists.

The Exception:
However, there are certain sections of the site that I do *not* want pushed through the pages router.

For example, my authentication controllers have to be separate from the pages.

Using the .htaccess file listed below, I cannot for the life of me figure out an exception for the auth controller.

I want to rewrite:
/auth -> index.php/auth/login (erroneously tries to reach index.php/pages/load_page/auth/login - causes 404!)
/auth -> index.php/auth/register (erroneously tries to reach index.php/pages/load_page/auth/register - causes 404!)

Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Removes access to the system folder by users.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php$1 [L]

    #Is the user accessing a valid file?
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    #Enables access to the images and css folders, etc
    RewriteCond $1 !^(index\.php|images|robots\.txt|css)
    #Rewrite the URL to the pages router
    RewriteRule ^(.*)$ index.php/pages/load_page/$1 [L]
    
</IfModule>

<IfModule !mod_rewrite.c>
    # Send to 404 if we don't have mod_rewrite
    ErrorDocument 404 /index.php
</IfModule>

If anyone has any insights, it would be greatly appreciated Smile
#2

[eluser]mddd[/eluser]
Why are you doing this in .htaccess? Just change this:
Code:
RewriteRule ^(.*)$ index.php/pages/load_page/$1 [L]
back into this:
Code:
RewriteRule ^(.*)$ index.php/$1 [L]

and do the routing in your routes.php:
Code:
$route['auth/(:any)'] = 'auth/$1'; // this doesn't change a thing for any url starting with auth/
$route['(:any)'] = 'pages/load_page/$1'; // all other pages go to pages/load_page
#3

[eluser]Unknown[/eluser]
I'm with mddd.

Routing is definitely your friend for what you're trying to do.

http://ellislab.com/codeigniter/user-gui...uting.html
#4

[eluser]vanquish[/eluser]
[quote author="mddd" date="1279733402"]Why are you doing this in .htaccess? Just change this:
Code:
RewriteRule ^(.*)$ index.php/pages/load_page/$1 [L]
back into this:
Code:
RewriteRule ^(.*)$ index.php/$1 [L]

and do the routing in your routes.php:
Code:
$route['auth/(:any)'] = 'auth/$1'; // this doesn't change a thing for any url starting with auth/
$route['(:any)'] = 'pages/load_page/$1'; // all other pages go to pages/load_page
[/quote]

Thanks for the replies guys!

Great idea in concept, but it did not work in practice. I made the changes exactly, but now all pages are just going to "load_page" and no URL parameters are being passed through.

I can click any page of the site, and it is just the same controller always being loaded. The auth controller is never touched. I tried changing the single quotes ' to double quotes " in the routing rule, and that did not work either.
#5

[eluser]mddd[/eluser]
Did you put them in the right order? If you put the second route first, it will match EVERYTHING. So then you'll never get to the auth controller.
Always put the most specific route first and the most generic route last.

With the routes I described, anything starting with 'auth/' should be going to the auth controller.

Looking at it again, I do see a mistake on my part: I wrote
Code:
$route['auth/(.*)']
which will match only things starting with 'auth/'. Not 'auth' by itself. So it's better to write
Code:
$route['auth(.*)'] = 'auth$1';
That way the route will also match if you just have 'auth' in the url.
#6

[eluser]vanquish[/eluser]
The auth route actually worked - it was a caching issue in Google Chrome that wasn't showing the correct page.

I figured out the problem with the rest of the pages too! My URL segment order had changed, and unfortunately there was no warning of this in the routes documentation. So I had to change $this->uri->segment(4) to $this->uri->segment(2), etc and all was good!!

Thanks so much guys! People on this forum are brilliant.




Theme © iAndrew 2016 - Forum software by © MyBB