I have two PHP files: profile.php
(accepts a u
value with an integer) and user.profile.php
. What I want to accomplish is this:
profile.php?u=1
should be example.com/profile/1
.user.profile.php
should example.com/profile/1/user
user.profile.php
also needs the same u
parameter value that profile.php
has. So here is what I wrote:
RewriteRule ^profile/(\d+)?$ profile.php?u=$1 [NC,L]
RewriteRule ^profile/(\d+)?$/users user.profile.php?u=$1 [NC,L]
The first part of that code works as intended. When I navigate to example.com/profile/1
, it shows the content that would be displayed when I navigate to example.com/profile.php?u=1
. However, the second line in that code throws a 500 Internal Server Error
. Is it not possible to create a virtual directory inside another one?
An exmpale of what cmorrissey is telling is that you missplaced the $
and put it in the wrong order on the last rule. It should be at the end of the first parameter in the RewriteRule
. Try this code.
RewriteRule ^profile/([^/]+)/user/?$ /user.profile.php?u=$1 [NC,L]
RewriteRule ^profile/([^/]+)/?$ /profile.php?u=$1 [NC,L]
BTW you can also streamline your code instead of having two separate files just for the additional directory. Just make user directory optional. And then add an additional query string parameter that you can check for. If it's there then show what you would show if the user folder is in the URL. e.g. I can capture the (user) folder if it's in the URI and then pass it to the ux
query string. Then you can check for in the PHP code. If ux
parameter is blank, then you know user folder is not in the URI.
RewriteRule ^profile/([^/]+)/?(user)?/?$ /profile.php?u=$1&ux=$2 [NC,L]