1

I have the following files in my website

{website}/restserver/user-create.php?id=...
{website}/restserver/user-read.php
{website}/restserver/session-login.php
...

I'd like to access them using the following addresses

{website}/user/create?id=...
{website}/user/read?id=...
{website}/session/login?id=...

I think you get it. I need to use the two level path to build the filename, maintaining the querystring.

Is it possible?

Thanks!

3 Answers 3

2
RewriteEngine On
RewriteBase /
RewriteRule ^/user/create$ /restserver/user-create.php [NC,L]
RewriteRule ^/user/read$ /restserver/user-read.php [NC,L]
RewriteRule ^/session/login$ /restserver/session-login.php [NC,L]
Sign up to request clarification or add additional context in comments.

Comments

1

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?((?!restserver/)[^/]+)/([^/])/?$ restserver/$1-$2.php [L,NC]

Query string will be automatically carried over to target URI.

4 Comments

Hi, can you explain the last line? Because I like this solution better, but I can't seem to make it work, while the one of @RamRaider does.
^/user and ^/session will only work from httpd config NOT from .htaccess. Also it is not a good idea to hardcode these paths. If you need /user/info tomorrow then you will need another rule using that approach.
I had minor issue with my rule which is fixed now. (?!restserver/) is a negative lookahead that says if URI doesn't start with restserver then route it to /restserver/<capture1>-<capture2> where capture1 and capture2 are the captured groups (...) in pattern.
Thanks, now it is perfect!
0

From the solutions above, I found a general solution.

RewriteRule ^([^/\.]+)/([^/\.]+)?$ restserver/$1-$2.php [L]

Thank you all!

Comments

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.