htaccess重写文件夹url以显示根文件夹中的内容

时间:2011-09-19 18:28:06

标签: .htaccess mod-rewrite

我是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会伪造文件夹中实际有页面。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

在根文件夹中使用此.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]
相关问题