【问题标题】:How to get /folder/subfolder to display as /subfolder/ using htaccess如何使用 htaccess 让 /folder/subfolder 显示为 /subfolder/
【发布时间】:2018-07-04 22:45:37
【问题描述】:

我正在尝试执行以下操作...

需要重写、重命名或重定向的原始路径:

http://www.example.com/_plugin/notifications/

收件人:

http://www.example.com/notifications/

通过根 htaccess 文件...任何帮助将不胜感激!

================================================ ===================

这不起作用:

RewriteEngine On
RewriteRule ^/_plugin/notifications$ /notifications [L]

这也不起作用:

RewriteEngine On
RedirectMatch ^/_plugin/notifications$ /notifications/

这也不起作用:

RewriteEngine On
RewriteRule ^notifications/(.*)$ /notifications$1 [R=301,NC,L]

==============================

编辑

在 @Starkeen 的帮助下 - 我对此有一些后续问题。

  1. 有没有办法缩短 .htaccess 文件,但如果可以说我 文件夹中有多个子文件夹,而不是写那个 1 每个子文件夹的条件和 2 条规则?
  2. 如何让子文件夹的子文件夹显示?目前它正在向我抛出 404... :(

============

在后续问题 #1 上仍然需要帮助,但我相信我收到了问题 #2。

后续问题 #2 的解决方案(我相信):

RewriteCond %{THE_REQUEST} /_plugin/notifications/ [NC]
RewriteRule ^_plugin/notifications/(.*)$ /notifications/$1 [L,R]
RewriteRule ^notifications/(.*)$ /_plugin/notifications/$1 [L]

【问题讨论】:

标签: .htaccess redirect mod-rewrite url-rewriting rename


【解决方案1】:

您尝试过的规则都不正确。 要将/folder/subfolder 重定向到/root/subfolder,您需要一个永久的301 重定向规则,如下所示:

RewriteEngine on

RewriteCond %{THE_REQUEST} /folder/subfolder/ [NC]
RewriteRule ^folder/subfolder/$ /subfolder/ [L,R]

以上将重定向 http://example.com/folder/subfolder/http://example.com/subfolder 。 如果根目录中不存在/subfolder/,您将收到 404 错误。为避免 404 错误,您可以使用内部 Rewrite/subfolder/ uri 重写回其原始位置 /folder/subfolder/,就在第一个下面 :

RewriteRule ^subfolder/?$ /folder/subfolder/ [L]

【讨论】:

  • 这行得通!非常感谢你!如果可能的话,跟进或两个问题:有没有办法缩短 .htaccess 文件,但如果假设我在文件夹中有多个子文件夹,而不是为每个子文件夹编写 1 个条件和 2 个规则?另外,我将如何允许子文件夹的子文件夹显示?目前它正在向我抛出 404 ...... :(
  • 请参阅对我发布的原始问题的编辑。谢谢 :)
猜你喜欢
  • 2016-11-12
  • 2015-02-13
  • 2015-02-17
  • 2018-10-21
  • 1970-01-01
  • 1970-01-01
  • 2012-07-24
  • 1970-01-01
  • 2021-09-29
相关资源
最近更新 更多