简体   繁体   English

使用GET Vars进行URL重写

[英]URL Rewriting with GET Vars

I'm trying to rewrite all the URL on my repertory with HTACCESS. 我正在尝试使用HTACCESS重写目录中的所有URL。 I want them to have the same model/architecture. 我希望他们具有相同的模型/架构。 I found some code on different websites, but there are not working when I try them. 我在不同的网站上找到了一些代码,但是当我尝试它们时却无法正常工作。

Here is what I want to do : 这是我想做的:

  • I have this URL : localhost/projects.php?id=1 我有这个网址: localhost / projects.php?id = 1
  • I want it to look like this : localhost/projects/1 我希望它看起来像这样: localhost / projects / 1

  • Here is another exemple : 这是另一个示例:

  • I have this URL : localhost/profile.php?id=1 我有这个网址:localhost / profile.php?id = 1
  • I want it to look like this : localhost/profile/1 我希望它看起来像这样:localhost / profile / 1
  • In other words I want to transform this : 换句话说,我想改变这个:

    • localhost/page.php?param=value&parm2=value2&... 本地主机/page.php?param=value&parm2=value2&...

    to

    • localhost/page/value/value2/... 本地主机/页面/值/值2 / ...

    Is there any way I can do this with all my pages ? 有什么办法可以对我的所有页面执行此操作? I don't know what I should write on my HTACCESS file. 我不知道应该在HTACCESS文件中写什么。

    First of parsing all route with .htaccess will be a bloody pain, you should just handle requested file and its parameters then you should parse parameters inside your php code (this is called as routing) and you will create your routing for your application. 首先,使用.htaccess解析所有路由会很痛苦,您应该只处理请求的文件及其参数,然后解析php代码中的参数(称为路由),然后将为应用程序创建路由。

    Please check the .htaccess rule below; 请检查以下.htaccess规则;

    #These are our conditions to run our rewrite rule
    RewriteCond %{REQUEST_FILENAME} !-f #Request is not a file
    RewriteCond %{REQUEST_FILENAME} !-d #Request is not a directory
    RewriteCond %{REQUEST_FILENAME} !-l #Request is not a link
    #This is our rewrite rule   
    RewriteRule ^([a-zA-Z0-9]*)?/(.*)$ $1.php/$2 [QSA,L] #QSA means QueryStringAppend modify according to your need, L means this is my last rule which same with stop processing rewrite rules
    

    And you should have a php code something like below which process the request for your routing 而且您应该有一个类似下面的php代码,该代码可以处理您的路由请求

    <?PHP
        $requestedPath = trim($_SERVER["PHP_SELF"],"/");
        $request = explode("/",$requestedPath,2);
        $requestFileName = $request[0]; //This is your requested file as you know already from .htaccess
        $requestParams = $request[1]; //You should parse this params according to your routing need
    ?>
    

    Hope these informations helps you about both routing and generic .htaccess rule. 希望这些信息对路由和通用.htaccess规则有所帮助。

    PS: Your request will be like www.example.com/myaction/first-parameter/second-parameter/third-parameter and for this example $requestParams will be first-parameter/second-parameter/third-parameter 附注:您的请求将类似于www.example.com/myaction/first-parameter/second-parameter/third-parameter,在此示例中,$ requestParams将是first-parameter/second-parameter/third-parameter

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM