skip to Main Content

I have links like this

  • http://panel.domain.com/users/index.php?user=8
  • http://panel.domain.com/users/settings/index.php?user=8

I want to rewrite them into this:

  • http://panel.domain.com/users/8/
  • http://panel.domain.com/users/8/settings

I want it to take anything after the user id and put it after users, and then go to index.php of that page and put the userid at the end, like this: users/USERID/chain/of/directories/index.php?user=USERID

I can make the first rewrite with this:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?user=$1

I’ve tried doing something like this

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^users/(.*)/$ $2/index.php?user=$1

$2 is first, because it should be everything AFTER users/ID/, and then $1 should be /ID/, which gets placed at the end of the directory chain: users/ID/settings/index.php?user=ID

But I really just can’t figure out how it works. I don’t know what it is that makes $1 and $2 work, and I don’t understand regex. I’ve been looking through the Pearl docs all night and just trying to slap stuff together but I just don’t get it.

2

Answers


  1. Could you please try following, written and tested based on your shown samples. You could catch both the variables in a single shot itself, you need not to use 2 conditions for this one.

    Place this .htaccess file inside your users folder only.

    Please make sure to clear your browser cache before testing your URLs.

    RewriteEngine ON
    RewriteBase /users/
    
    RewriteRule ^/?$ index.php [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(d+)/?$ index.php?users=$1 [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule ^(d+)/.*$ settings/index.php?users=$1 [L]
    
    Login or Signup to reply.
  2. You may use this .htaccess code in site root .htaccess:

    Options -MultiViews
    RewriteEngine On
    
    RewriteRule ^users/([w-]+)(/.+)?/?$ /users$2/index.php?user=$1 [L,QSA]
    

    This assumes you don’t have any .htaccess inside /users/ directory.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search