duandi6531 2016-08-10 07:49
浏览 22
已采纳

无法使用htaccess重写php url

This will be a niche question but I am having trouble rewriting my url. I am trying to rewrite from /view.php?user=Alex0111 to view/Alex0111. I also have a second get variable of /view.php?user=Alex0111&id=5 which I want to be view/Alex0111/5 Here are the contents of my .htaccess file

DirectoryIndex Home.php

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^view/([0-9A-Za-z]+) view.php?user=$1 [NC,L] #doesn't work causes internal error

I've checked this line of code multiple times to the tutorial I am following but I am missing the mark on something.

  • 写回答

1条回答 默认 最新

  • dongxie3681 2016-08-10 08:05
    关注

    Replace both of your rewrite rules with:

    RewriteRule ^view/(.+)/(.+)  view.php?user=$1&id=$2  [NC,END,QSA]
    

    Your first rewrite rule will interfere because it will rewrite the path as Markus mentioned in comments.

    The [END] flag will throw an error if you have an old Apache version. In that case, use the [L]

    The [QSA] flag tells the server to add any additional query parameters that the user sent. eg: view/Alex01/5?param=value

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改