Several rewrite rules with php?!?!

I’m trying to setup my htaccess to work with more than 1 rule. I have set it up like this with success:

Options +FollowSymLinks

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /

RewriteRule ^([a-zA-Z0-9_-]+)$ grouppublic.php?groupname=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ grouppublic.php?groupname=$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?ga=$1 [QSA]
</IfModule>

Now I want to add this:

RewriteRule ^([a-zA-Z0-9_-]+)$ blog/blog.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ blog/blog.php?username=$1

But with no luck… I have put the blog.php inside a folder called blog… Where do I go wrong?

Thanks in advance :slight_smile:

The problem is the rules for grouppublic.php and blog/blog.php match exactly the same thing ( ^([a-zA-Z0-9_-]+)$ and ^([a-zA-Z0-9_-]+)/$ )

Apache matches an incoming on the first one (grouppublic.php), so the second (blog/blog.php) never fires.

You need to make some distinction between the two.
For example /g/something for grouppublic.php and /b/something for blog/blog.php

PS.


RewriteRule ^([a-zA-Z0-9_-]+)$ grouppublic.php?groupname=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ grouppublic.php?groupname=$1

can be written as


RewriteRule ^([a-zA-Z0-9_-]+)/?$ grouppublic.php?groupname=$1

/? meaning “there may be a slash here, but there doesn’t have to be”

Not sure what you mean in the above?!?! Can you please explain?

Well, instead of www.yourdomain.com/{username} and www.yourdomain.com/{groupname}, use www.yourdomain.com/u/{username} and www.yourdomain.com/g/{groupname}

And let the RewriteRules match [1]u//B/?$ for blog/blog.php and [2]g//B/?$ for grouppublic.php

All should be well with the world then :slight_smile:

Alternatively you could redirect everything to some index.php


RewriteRule . /index.php

And let the index.php decide whether the given path is a groupname or a username, and disallow usernames to be the same as groupnames and the other way around.
Something along the lines of


$p = explode('/', $_SERVER['REQUEST_URI']);
$path = trim($p[0], '/');
if (user_exists($user))
{
   $_GET['username'] = $path;
   include('/blog/blog.php');
}
else if (group_exists($user))
{
  $_GET['groupname'] = $path;
  include('grouppublic.php');
}

Where of course user_exists and group_exists have to be implemented by you (return a boolean the indicates whether a user / group does exist).


  1. B ↩︎

  2. B ↩︎

Yes… Now it works, only have one problem though… All my images is now missing due to the u/ or the g/… I have my images like this : …/images/image.gif Is there a way out of this without having to change all the images path!!!

  1. Any nice PHP editor can do a search/replace over a complete directory.
    Should do the trick for you.

  2. Consider

RewriteRule ^u/images$ /images [L]
RewriteRule ^g/images$ /images [L]

First option is the preferred one, since it doesn’t bother apache with rewriting the path of each and every image.

I’d recommend modifying your CSS to make the images all relative to the base url.

That way you’ll save on loading time - as far as the browser is concerned, images/logo.jpg, u/images/logo.jpg and /g/images/logo.jpg will all be different images.

You may want to think about the directory names, before you get stuck to using ‘g’ and ‘u’. Not that ‘g’ and ‘u’ are bad choices - but they were thought up as examples, you may want to change that before making your changes too permanent. ‘Users’ and ‘Groups’ would be more in the ‘Tidy URI’ mindset.