Page 1 of 1

Help with .htaccess

Posted: Wed Mar 24, 2010 2:23 pm
by ben.artiss
Hi everyone,

I'm having a little trouble with this htaccess problem- I only want to add a conditional slash instead of using 2 different statements. Here's the file, the commented out bit almost worked but got a little sketchy with more than 2 URI parameters:

Code: Select all

RewriteEngine On
 
# These don't work quite right
# RewriteRule    ^admin/?([^/]*[/?])$     admin.php/$1     [QSA,L]
# RewriteRule    ^mob/?([^/]*[/?])$         mobile.php/$1    [QSA,L]
 
# These work fine but require 2 statements per file
RewriteRule    ^admin$         admin.php/$1    [QSA,L]
RewriteRule    ^admin/(.*)$    admin.php       [QSA,L]
RewriteRule    ^mob$           mobile.php      [QSA,L]
RewriteRule    ^mob/(.*)$      mobile.php/$1   [QSA,L]
 
# This bit is the fallback
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Does anyone know how to fix this? Thanks in advance,

Ben

Re: Help with .htaccess

Posted: Wed Mar 24, 2010 3:00 pm
by ell0bo
well, first off your admin ones won't work. Well, the first will, but you need that /$1 one lower.

as for why the top two aren't working, it's because you're not saying the (...) section is optional. You tell it there may or may not be a /, but there always has to be something else.

if there's an index/ the first /? will consume it, leaving nothing for the (...) . If you put a ? after it, then it SHOULD work. Don't quote me on that though.

Re: Help with .htaccess

Posted: Wed Mar 24, 2010 7:06 pm
by ben.artiss
Oops, thanks well spotted :P but I'm still not getting to grips with it properly. I've tried all sorts and it keeps giving me the same result, but it's so close!

/index.php

Code: Select all

<?php
define('MODE', 'public');
...
/admin.php

Code: Select all

<?php
define('MODE', 'admin');
...
/.htaccess

Code: Select all

...
RewriteRule ^admin[/?](.*)$    admin.php/$1    [QSA,L]
# OR RewriteRule ^admin([/?].*)$    admin.php/$1    [QSA,L]
# OR RewriteRule ^admin(.[/?]*)$    admin.php/$1    [QSA,L]
...
If I go to http://localhost/site/ the page shows the mode to be 'public', and if i go to http://localhost/site/admin/ the page shows 'admin', but if i go to http://localhost/site/admin (without the trailing slash) the page shows 'public'. I think it's nearly there, there's just a conditional slash missing somewhere (I hope).

Re: Help with .htaccess

Posted: Wed Mar 24, 2010 8:41 pm
by ell0bo
yeah.... [/?] means the set of / or ? . drop those brackets and just use /?

Re: Help with .htaccess

Posted: Thu Mar 25, 2010 3:26 am
by ben.artiss
Thanks for clarifying that (I didn't know that), but this line gives me an internal server error:

Code: Select all

RewriteRule ^admin/?(.*)$    admin.php/$1    [QSA,L]
Maybe I should just stick to 2 statements? :)

Re: Help with .htaccess

Posted: Thu Mar 25, 2010 9:21 am
by ell0bo
alright... I'm being stupid here. All you want is to translate a leading index or admin to index.php or admin.php right? ok... simple

RewriteRule ^index(\.php)? index.php [PT,NC]