4

I'm not the best with .htaccess so I hope someone here can help. I'm trying to rewrite a URL to look more clean.

Original URL: domain.com/admin/users?userid=<id>
What I want: domain.com/admin/users/<id>

I have tried this code but I'm getting a 404:

RewriteRule ^admin/users/([a-zA-Z0-9_-]+)$ users.php?userid=$1
RewriteRule ^admin/users/([a-zA-Z0-9_-]+)/$ users.php?userid=$1
0

1 Answer 1

1

The directory was omitted from the rewrite. The character class can also be simplified and the trailing slash can be made optional:

RewriteRule ^admin/users/([-\w]+)/?$ admin/users.php?userid=$1

A detailed write up of \w can be found here, http://regular-expressions.info/shorthand.html. In short:

\w stands for “word character”. It always matches the ASCII characters [A-Za-z0-9_]

regex101 can also be used, http://regex101.com/r/Px2vTE/1

Sign up to request clarification or add additional context in comments.

2 Comments

Trying to write another URL, so the original URL is domain.com/admin/users-edit.php?userid=<id> and I want to rewrite it to domain.com/admin/users/<id>/edit I've tried RewriteRule ^admin/users/([-\w]+)/$/edit? admin/user-edit.php?userid=$1 but this won't work, any ideas why?
Got it working with RewriteRule ^admin/users/([-\w]+)/edit/?$ admin/user-edit.php?userid=$1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.