【问题标题】:.htaccess : Rewrite url, one variable to directory.htaccess : 重写url,一个变量到目录
【发布时间】:2018-02-09 17:36:09
【问题描述】:

我想知道这个最终结果是否可以通过一些htaccess规则来实现。

config 文件夹中。其中 * 是要显示为文件夹的页面名称。

/projectdir/config/?p=* or /projectdir/config/index.php?p=*
to /projectdir/config/*/

例子

/projectdir/config/?p=page1 => /projectdir/config/page1/
/projectdir/config/index.php?p=page1 => /projectdir/config/page1/

我尝试了this问题中发布的代码,但没有结果

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w\d~%.:_\-]+)$ index.php?p=$1 [NC]

基本上是:http://localhost/index.php?page=controller To

http://localhost/controller/

【问题讨论】:

  • 你能分享一下你到目前为止所尝试的吗?谢谢。
  • @starkeen 我尝试了this 问题中发布的代码(第一个代码块),但没有结果
  • 请编辑您的问题以发布您尝试过的内容。
  • @starkeen 好的,问题已更新
  • 你在浏览器中输入了什么网址,会发生什么?

标签: .htaccess url-rewriting


【解决方案1】:

您可以在/project/config/.htaccess使用此代码:

Options +FollowSymlinks
RewriteEngine on
RewriteBase /project/config/

RewriteCond %{THE_REQUEST} /config/(?:index\.php)?\?p=([^\s&]+)&p2=([^\s&]+)\s [NC]
RewriteRule ^ %1/%2? [R=301,NE,L]

RewriteCond %{THE_REQUEST} /config/(?:index\.php)?\?p=([^\s&]+)\s [NC]
RewriteRule ^ %1? [R=301,NE,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/([^/]+)/?$ index.php?p=$1&p2=$2 [L,QSA]

RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]

【讨论】:

  • 谢谢,但它不起作用。使用此代码,结果是:ip/project/config/?p=page1 => ip/config/page1?p=page1
  • 好的,现在规则起作用了(部分),结果如下:ip/project/config/?p=page1 => ip/project/config/page1?p=page1
  • 结果应该是这个ip/project/config/?p=page1 => ip/project/config/page1/
  • 您好,是否可以修改规则来处理可能的第二个参数 p2?如果参数 p2 存在 ip/project/config/?p=page1&p2=subpage1 => ip/project/config/page1/subpage1。我试图修改规则,但不太了解 htaccess 文件的语法,我没有成功。
  • 嗨,可以编辑此规则并添加此规则:ip/project/config/?p=page1&param1=param1&param2=param2&param3=param3 => ip/project/config/page1/param1/param2(隐藏 param3),保持这两个规则已经存在?我试图改变规则,但我认为它们相互冲突。
猜你喜欢
  • 2015-08-30
  • 2010-10-26
  • 2014-06-10
  • 1970-01-01
  • 1970-01-01
  • 2012-06-02
  • 1970-01-01
  • 2017-10-01
  • 2020-04-02
相关资源
最近更新 更多