【问题标题】:.htaccess dynamic URL rewriting in PHP Apache.htaccess 在 PHP Apache 中的动态 URL 重写
【发布时间】:2014-05-08 18:18:48
【问题描述】:

我有一个类似的网址

www.example.com/program.php?course=1&program=Ajay

现在使用 .htaccess 我可以像这样转换它

www.example.com/program/1/Ajay

但我想像这样转换它

www.example.com/program/Ajay

谁能建议我怎么做?

非常感谢任何帮助。

我使用了这个代码

RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ program\.php\?course=(.*)&program_name=(.*)\ HTTP
RewriteRule ^ /program/%3\? [R,L]

【问题讨论】:

  • 你能展示你当前的规则吗?
  • @anubhava 更新了,请检查。
  • 为什么program_name= 在你的规则中> 你的问题没有任何参考。

标签: php .htaccess url-rewriting apache2 url-routing


【解决方案1】:

这是一个简单的例子,它可以帮助你:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^post-(.*)-([0-9]+)\.html$ post.php?post_name=$1&post_id=$2 [L]

【讨论】:

    【解决方案2】:

    以下规则应将www.example.com/a.php?x=1&y=2&z=Ajay 更改为www.example.com/target/Ajay

    RewriteEngine On
    RewriteCond %{THE_REQUEST} ^(GET|POST)\ /program\.php\?course=(.*)&program=(.*)\ HTTP
    RewriteRule ^ /program/%3\? [R,L]
    
    RewriteRule ^/program/(.*)$ /program.php?program=$1 [L]
    

    %4 指的是&z= 之后的值。如果您想要来自?x=&y= 的值,您可以使用%2 中的值x=%3 中的值&y=

    例如将www.example.com/a.php?x=1&y=2&z=Ajay 更改为www.example.com/target/x/Ajay

    RewriteEngine On
    RewriteCond %{THE_REQUEST} ^(GET|POST)\ /a\.php\?x=(.*)&y=(.*)&z=(.*)\ HTTP
    RewriteRule ^ /target/%2/%4\? [R,L]
    

    【讨论】:

    • 将上述内容添加到根文件夹中的 .htaccess 文件中应该可以。
    • 您可以编辑您的帖子并放入您的 htaccess 文件中的内容吗?
    • 您的 htaccess 文件中需要有 RewriteEngine On。还将%2 更改为%3
    • 在您的program.php 中是否必须将值传递给course
    • 你没有添加第二个重写规则?
    猜你喜欢
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多