dsvs50005 2015-03-26 20:17
浏览 87

使用.htaccess在另一个虚拟目录中创建虚拟目录会引发500内部服务器错误

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?

  • 写回答

1条回答 默认 最新

  • doushang3352 2015-03-26 20:51
    关注

    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]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?