【问题标题】:.htaccess - Rewrite subfolder to parent with access to other files in the parent.htaccess - 将子文件夹重写为父文件夹,并可以访问父文件夹中的其他文件
【发布时间】:2015-05-05 21:03:07
【问题描述】:

我正在尝试创建一个 RewriteRule,它允许我从 URL 中隐藏子文件夹名称。这似乎很简单,尽管我也想保留对父文件夹中文件的访问权限,但我不知道如何实现这一点以及这是否可能。为了更清楚一点,我的文件树如下:

[root]
 L[parent]
   file1
   L[subfolder]
     file2

所以我想做的是能够将http://example.com/parent/subfolder/file2 重写为http://example.com/parent/file2,同时仍然可以访问http://example.com/parent/file1。到目前为止,我找到了可能的解决方案 here 和 here,但它们都没有按预期工作。我还查看了 Apache Aliases,但我不确定在这种情况下是否可以使用它们。请问有什么建议吗?

编辑:这是当前的.htaccess:

<IfModule mod_rewrite.c>

 RewriteEngine On
 RewriteBase /pico/
 RewriteRule sitemap\.xml \?sitemap
 RewriteRule ^parent?$ parent/ [R=301,L] ## Redirects the old file to the index with the same name
 RewriteRule ^parent/subfolder/?$ parent/ [R=301,L] ## Redirects the index of the subfolder to the parent
 RewriteRule ^old-folder/?$ parent/subfolder/new-file1 [R=301,L] ## Redirects the old index to a new file in the subfolder
 RewriteRule ^old-folder/old-file parent/new-file [R=301,L] ## Redirects the files in the old folder to a new file in the parent
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d 
 RewriteRule . index.php [L]
 RewriteRule ^parent/([^/]+)/?$ parent/subfolder/$1 [L]

</IfModule>

# Prevent file browsing
Options -Indexes

带有cmets的规则只是示例,因为我在改变结构的同时将旧内容移动到新站点,所以还有更多重复的规则,就像上面的示例一样。

【问题讨论】:

  • 您当前的 .htaccess 文件在哪里?
  • 刚刚添加到问题中

标签: apache .htaccess mod-rewrite url-rewriting


【解决方案1】:

您刚刚尝试过这个吗?把它放在根目录下的 .htaccess 文件中。

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^parent/([^/]+)/?$ /parent/subfolder/$1 [L]

编辑:

根据您的评论,我正在解释我上面所说的内容。您说您希望将parent/subfolder 中的任何内容都像直接在parent 位置中一样读取。所以这意味着http://example.com/parent/file2 确实 NOT 真的存在。因为它实际上存在于http://example.com/parent/subfolder/file2中。

这就是条件的目的。它检查以确保对/parent/whatever 的任何请求都不是真正的文件,也不是真正的目录,如果这是真的,它会将其重写为/parent/subfolder/whatever。所以这是你要求的,但你没有像我给你的那样使用规则。你所做的只是将 rewriterule 行添加到文件的底部,它永远不会执行,因为这条规则

 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d 
 RewriteRule . index.php [L]

将在RewriteRule ^parent/([^/]+)/?$ /parent/subfolder/$1 [L] 之前执行,然后永远不会运行。提问时不能只提供 .htaccess 规则的一部分,因为某些规则会根据其顺序优先于其他规则。您需要像刚才一样提供所有规则。

所以你需要把你的规则改成这个。

 RewriteRule sitemap\.xml \?sitemap
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^parent/([^/]+)/?$ /parent/subfolder/$1 [L]
 RewriteRule ^parent?$ parent/ [R=301,L] ## Redirects the old file to the index with the same name
 RewriteRule ^parent/subfolder/?$ parent/ [R=301,L] ## Redirects the index of the subfolder to the parent
 RewriteRule ^old-folder/?$ parent/subfolder/new-file1 [R=301,L] ## Redirects the old index to a new file in the subfolder
 RewriteRule ^old-folder/old-file parent/new-file [R=301,L] ## Redirects the files in the old folder to a new file in the parent
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d 
 RewriteRule . index.php [L]

【讨论】:

  • 不幸的是,我无法在内部文件夹中添加额外的 .htaccess,因为我将 Pico CMS 与 Twig 一起使用,其中内容文件夹(实际的 URL 结构)与模板文件夹是分开的。无论如何,您的规则不是将file1 文件夹的索引重写为子文件夹中同名的文件吗?这不是我所追求的。我需要访问父文件夹及其子文件夹中的文件,就好像它们都在父文件夹中一样。
  • 然后稍微改变一下规则并将规则放在根.htaccess中。我更新了答案。
  • 还是什么都没有。我可能不理解它背后的逻辑,但在我看来,您正在将父级的所有子文件夹重写为一个特定的子文件夹。同样,这不是我需要的
  • 是的,你没有理解逻辑,因为它没有按照你的想法做。查看我的更新。
  • 感谢您花时间解释您的代码,但它仍然无法正常工作。似乎现在 .htaccess 导致 Apache 抛出 404 错误,而以前,通过常规重定向,错误是由 PHP 引擎抛出的。无论如何,在我找出问题所在之前,我无法确认您的代码是否有效。看来现在我将不得不放弃重写的想法并坚持当前的 URL 结构。无论如何感谢您的建议
猜你喜欢
  • 2014-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-30
  • 2014-02-09
  • 2021-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多