dqvzfp6468 2014-02-23 14: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 14: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部