【问题标题】:htaccess rewrite folder url to show content from root folderhtaccess 重写文件夹 url 以显示根文件夹中的内容
【发布时间】:2011-09-19 18:28:06
【问题描述】:

我是 htaccess 的新手,遇到了一些需要帮助的问题。在我的站点中,我有一个文件夹(实际上是空白的),我想为其提供根文件夹中的内容。

假设结构如下:

example.com

   -folder1

   -index.php

   -anotherpage.php

   -.htaccess

因此,当有人访问 example.com/folder1/index.php 时,他会看到该 url,但会从 example.com/index.php 获取内容。如果他访问 example.com/folder1/anotherpage.php,他将从 example.com/anotherpage.php 获取内容。当然,当他直接访问 example.com/index.php 时,他会看到 example.com/index.php。

我发现很多例子显示相反但不是我想要的。另外澄清一下,我想重写,而不是重定向,所以 url 会假装在 folder1 中实际上有页面。

任何帮助将不胜感激。

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    在根文件夹中使用这个 .htaccess:

    # Enable mod_rewrite
    RewriteEngine on
    # ^: Start with
    # folder1: name the folder
    # (\/?): Can containt an ending slash eg: folder1/ or folder1
    # $: End with
    # [QSA]: Query string append, means you can use %name=value
    # [NC]: Non Case: Case insensitive
    # [L]: Last rule, stop if this rule match the url
    RewriteRule ^folder1(\/?)$ /index.php [QSA,NC,L]
    # (.*): Everything
    # $1: the matching filename- file must exists.
    RewriteRule ^folder1/(.*)(\/?)$ /$1 [QSA,NC,L]
    

    【讨论】:

    • 你把这些规则放在哪里了?你能告诉我你的 .htaccess 吗?
    • 你能解释一下每一行是做什么的吗?
    猜你喜欢
    • 2021-07-01
    • 2011-09-13
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 2020-01-23
    相关资源
    最近更新 更多