【问题标题】:.htaccess how to display long sub-directory urls as short urls in address bar?.htaccess 如何在地址栏中将长子目录网址显示为短网址?
【发布时间】:2017-11-12 14:58:20
【问题描述】:

我这几天一直在尝试解决这个问题。 我的根文件夹是https://www.example.com,我有很多关于它的文章,但在不同的文件夹和子文件夹中以便更好地组织。

问题是我希望链接看起来更好而不是像:

> https://www.example.com/reviews/review_1/index.html
                                 /review_2/index.html
                                 /review_3/index.html

https://www.example.com/articles/blog/content_1/index.html
                                     /content_2/index.html
                                     /content_3/index.html

但是,我希望“评论和文章”下的所有文件夹都显示为 root/dirs:

https://www.example.com/review_1/index.html
https://www.example.com/content_1/index.html

...那么是否有可能以某种方式排除 ../reviews/../articles/blog/ 文件夹,但要具有相同的目录结构并且不具有重复的内容、索引或重复的 DIR?

编辑/我找到了解决方案:

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule ^review_1/(.*) reviews/review_1/$1

此代码将从文件夹 reviews/review_1/ 重定向到根目录上不存在的文件夹 review_1,并将所有内容从 reviews/review_1/ 复制到 review_1

【问题讨论】:

标签: apache .htaccess


【解决方案1】:

只需创建一个关联箭头,指定 url 之间的映射以及它们在引擎盖下的含义,例如我在 php 中这样使用-

$route = array(
   array(
    'url' => 'review_1/index.html',
    'map_to' => '/articles/blog/reviews/index.html'
  ),
   array(
  )
);

或者更好的版本,比如实现简单的路由-

$route = array( array( 'url' => '/review/', 'controller' => 'controller_class', 'action' => 'model_class' ), array( ) );

最好继续第二个过程.. 在自己实现路由或使用基本框架方面做一些研究。 希望对您有所帮助。

【讨论】:

  • 很遗憾,我对php一窍不通,所以按照建议的方式解决这个问题有点小问题。我希望是否有任何我可以实现的 .htaccess 代码?
  • 您可以指定覆盖规则,但对于每个单独的请求,管理起来并不困难。
  • 你可以这样写-RewriteRule {pattern_to_be_matched} {pattern maps to this} [flags]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-28
  • 2011-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-16
  • 1970-01-01
相关资源
最近更新 更多