Help with .htaccess

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Help with .htaccess

Post 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
ell0bo
Forum Commoner
Posts: 79
Joined: Wed Aug 13, 2008 4:15 pm

Re: Help with .htaccess

Post 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.
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: Help with .htaccess

Post 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).
ell0bo
Forum Commoner
Posts: 79
Joined: Wed Aug 13, 2008 4:15 pm

Re: Help with .htaccess

Post by ell0bo »

yeah.... [/?] means the set of / or ? . drop those brackets and just use /?
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: Help with .htaccess

Post 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? :)
ell0bo
Forum Commoner
Posts: 79
Joined: Wed Aug 13, 2008 4:15 pm

Re: Help with .htaccess

Post 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]
Post Reply