【问题标题】:.htacess mod_rewrite slim routing.htaccess mod_rewrite 超薄路由
【发布时间】:2020-10-23 17:23:25
【问题描述】:

我有两种路线。

这行得通:

http://localhost/monitor/test1

这不是:

http://localhost/monitor/test2/test3

这是我目前的规则:

RewriteRule (^[^/]*$) public/$1 [L]

我知道这个只匹配最后一个斜线。如何更改正则表达式以匹配这两种情况?

【问题讨论】:

  • 能否请您从http://localhost/monitor/test2/test3 告诉我们您要重定向到哪个网址?
  • @RavinderSingh13 我想重定向到 public/test2/test3

标签: regex .htaccess url mod-rewrite slim


【解决方案1】:

第一种解决方案:请您尝试以下方法。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/monitor[^/]*/(.*)$
RewriteRule ^.*$ /public/%1 [NC,L]

或者,如果您在REQUEST_URI 起始部分中可能有除monitor 以外的任何其他字符串,请尝试以下操作。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[^/]*/(.*)$
RewriteRule ^.*$ /public/%1 [NC,L]


第二个解决方案: 或者我们可以在编写 RewriteRule 本身时捕获值,并且可以减少 1 个RewriteCond,这在此处的第一个解决方案中使用。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]*/(.*)$ /public/$1 [NC,L]

OR 与 monitor 匹配关键字:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^monitor[^/]*/(.*)$ /public/$1 [NC,L]

【讨论】:

  • 你已经帮了我很多,我真的很开心。对我有用的是第一个,但作为记录,公共目录不能以斜杠 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} ^/monitor[^ /]*/(.*)$ RewriteRule ^.*$ public/%1 [NC,L]
  • @DiegoDieh,欢迎您 :) 但实际上,该斜杠会在 URL 的域名之后生成 REQUEST URI,因此它存在于那里,很高兴为您欢呼
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-12
相关资源
最近更新 更多