dqvzfp6468 2014-02-23 22:23
浏览 26
已采纳

htaccess - 使用GET重定向到php文件[关闭]

Some user will access a content of my site with this URL: www.example.com/example

But I need to be redirected to: www.example.com/page.php?p=example

So all values ​​that I put after the slash is redirected to the page.php, as the value of "p" parameter.

  • 写回答

1条回答 默认 最新

  • doutuoshou8915 2014-02-23 22:26
    关注

    This question is quite easy and has been asked before.
    This is why I will not give you the exact answer, but rather redirect you to tutorials and guidelines explaining you what to do. This'll allow you to understand the solution even better.

    If you're running your site on an Apache server, you can use Mod Rewrite. https://httpd.apache.org/docs/current/mod/mod_rewrite.html

    Also check this out: http://corz.org/server/tricks/htaccess2.php

    But as people seem to downvote me because of this, I'll help you out.
    In your .htaccess file, you place the following

    Whenever you use mod_rewrite (the part of Apache that does all this magic), you need to do before any ReWrite rules. You only need to do this once per .htaccess file.
    +FollowSymLinks must be enabled for any rules to work, this is a security requirement of the rewrite engine. Normally it's enabled in the root and you shouldn't have to add it, but it doesn't hurt to do so

    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^(.*) page.php?p=$1 [NC]
    

    NC means no-case a.k.a. case-insensitive.
    You can also add L (-> [NC,L]) to inidicate this is the Last Rewrite Rule.

    You may have noticed, the solution above uses regular expression to match variables. What that simply means is.. match the part inside (.+) and use it to construct "$1" in the new URL. In other words, (.+) = $1 you could have multiple (.+) parts and for each, mod_rewrite automatically creates a matching $1, $2, $3, etc, in your target (aka. 'substitution') URL.

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

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100