28

我想知道是否可以.htaccess用来重写文件夹名称。我的意思是这个。

可以说我有一个像这样的网址:

www.site.com/folder1/page.php

现在我想将网址重写为(例如)

www.site.com/apple/page.php

folder1 是我的网络空间上的现有文件夹。

重要:“苹果”不是一个文件夹,而只是一个名字!

好的,这是一个分步计划:

  1. 用户类型www.site.com/folder1/login.php
  2. url 应该重写而不是将 url 重定向到www.site.com/apple/login.php

这意味着这apple只是一个名称而不是目录。所有代码都应该来自folder1。Acutally apple 应该只是folder1. 我不能只是重命名folder1Apple. 因此,我将重写folder1apple.

4

2 回答 2

59
于 2009-08-12T07:44:16.200 回答
5

try:

RewriteRule ^/apple(.*)?$ /folder1$1 [NC]

Where the folder you want to appear in the url is in the first part of the statement - this is what it will match against and the second part 'rewrites' it to your existing folder. the [NC] flag means that it will ignore case differences eg Apple/ will still forward.

See here for a tutorial: http://www.sitepoint.com/article/guide-url-rewriting/

There is also a nice test utility for windows you can download from here: http://www.helicontech.com/download/rxtest.zip Just to note for the tester you need to leave out the domain name - so the test would be against /folder1/login.php

to redirect from /folder1 to /apple try this:

RewriteRule ^/folder1(.*)?$ /apple$1 [R]

to redirect and then rewrite just combine the above in the htaccess file:

RewriteRule ^/folder1(.*)?$ /apple$1 [R]
RewriteRule ^/apple(.*)?$ /folder1$1 [NC]
于 2009-08-12T07:50:10.650 回答