【问题标题】:htaccess redirect to subfolder and filehtaccess 重定向到子文件夹和文件
【发布时间】:2021-06-29 18:57:06
【问题描述】:

我认为有很多 htaccess 重定向示例,但我找不到与我类似的案例。

我的根目录有很多子文件夹

/suppliers
 |- /suppliers/abc
 |- /suppliers/xyz

/merchants
 |- /merchants/abc
 |- /merchants/xzy

等等

我想在我输入时重定向它们

/m/yyy -> /merchants/yyy/index.php
/m/yyy/abc -> /m/merchants/yyy/abc.php
/m/yyy/abc/ -> /m/merchants/yyy/abc/index.php

谁能告诉我如何做到这一点?

【问题讨论】:

  • 也许像? RewriteRule ^m/yyy$ /merchants/yyy/index.php?&%{QUERY_STRING}
  • 我更喜欢 yyy 不是 htaccess 中的字符串硬编码,在我的情况下 yyy 只是一个示例
  • 请分享您到目前为止所尝试的内容
  • 我试过 RewriteRule ^m/([0-9a-zA-Z]{1,15})/([0-9a-zA-Z]{1,15})$ /merchants /$1/$2.php [L] RewriteRule ^m/([0-9a-zA-Z]{1,15})$ /merchants/$1/index.php [L] 当我输入 example.com/m/abc 或 /m /abc/xyz,它成功重定向到example.com/merchants/abc/index.php 或/m/abc/xyz.php 但如果example.com/m/abc 则我不起作用
  • 请通过编辑您的问题发布此代码

标签: .htaccess directory


【解决方案1】:

您可以在站点根目录 .htaccess 中使用这些规则:

RewriteEngine On

# /m/yyy rule
RewriteRule ^m/([\w-]+)/?$ merchants/$1/index.php [L,NC]

# /m/yyy/abc rule
RewriteRule ^m/([\w-]+)/([\w-]+)$ merchants/$1/$2.php [L,NC]

# /m/yyy/abc/ rule
RewriteRule ^m/([\w-]+)/([\w-]+)/$ merchants/$1/$2/index.php [L,NC]

【讨论】:

  • 它对我有用,我可以知道 [\w-]+ 的功能吗?
  • 匹配字母、数字或连字符
猜你喜欢
  • 2014-12-03
  • 2017-12-05
  • 2015-10-06
  • 1970-01-01
  • 2014-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多