doudou1897 2016-12-20 22:16
浏览 52
已采纳

.htaccess Php重写网址

I try to realize a system of rewriting URLs in .htaccess.

Then here is my goal: If I have an url of this form: http://localhost/view.php?Id=456

Then I want to transform it to: http://localhost/456

I use this rule in htaccess:

RewriteRule ^ ([a-zA-Z0-9] +) $ view.php? Id = $ 1

Now this works very well!

But my problem I want to add points to id ie instead of 456 I can put: my.book

That is to say: http://localhost/my.book

  • 写回答

2条回答 默认 最新

  • dongwu8653 2016-12-20 23:12
    关注
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([a-zA-Z0-9\.]+)$ view.php?id=$1 [QSA,L]
    

    You need RewriteCond %{REQUEST_FILENAME} !-f before the RewriteRule line to tell the server that the RewriteRule written below to be executed if the input passed in the URL is not an actual file. Because server searches for a file matching the input you pass in the URL and also it won't work in case you pass my.book in the URL since web server recognizes . as prefix for extension like .php or .html or like so and thereby it results in Not Found error if there is no file named my.book exists. So, you also need to escape . in the URL.

    To allow .'s in the input, you need to add . with escape sequence \ in the character class group like ^([a-zA-Z0-9\.]+)$. Note, allowing this can result in escaping the extension in the URL, that is, passing view.php in the URL won't navigate to the actual file. Rather, it will be considered as a value in the query string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog