【问题标题】:Rewriting URL using htaccess and mod_rewrite使用 htaccess 和 mod_rewrite 重写 URL
【发布时间】:2013-08-22 06:58:49
【问题描述】:

我是 htaccess mod_rewrite 规则的新手。我想从 URL 中消除 GET 变量。我尝试了以下规则:

RewriteEngine On
RewriteCond %{THE_REQUEST} \?[^\ ]+
RewriteRule (.*) /$1? [R=301,L] #remove query string

上述规则消除了查询?id,但它将URL重定向为www.example.com/admin/test.php,没有子目录project和扩展名.php

我要转换

www.example.com/project/admin/test.php?id=1 

www.example.com/project/admin/test

【问题讨论】:

  • 如果id是2,是否需要重写?
  • id 是动态的.. 如果有任何 id 被传递,它应该重写 @undone
  • 那么,你只想从 URL 的末尾删除 .php 吗?
  • 想要删除 .php 并且还想要阻止从现有 URL 中删除子目录“project”。因为上面的代码也删除了“project”子目录。我想要像 www.example.com/project/admin/test

标签: php .htaccess url mod-rewrite


【解决方案1】:

浏览 www.example.com/project/admin/test?id=1www.example.com/project/admin/test.php?id= 相同1

RewriteCond %{REQUEST_FILENAME}\.php  -f
RewriteCond %{REQUEST_FILENAME}       !-f
RewriteRule  (.*)  $1.php

【讨论】:

  • 它没有删除 .php 扩展名和查询字符串 ?id=.. @undone
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 2015-02-14
  • 2021-04-27
  • 1970-01-01
  • 2020-05-14
  • 2013-03-29
相关资源
最近更新 更多