使用 .htaccess 重写文件夹名称 [英] rewrite a folder name using .htaccess
问题描述
我想知道是否可以使用 .htaccess
来重写文件夹名称.我的意思是这个.
I am wondering wether it's possible to use .htaccess
to rewrite a folder name.
What I mean is this.
假设我有一个像这样的网址:
Lets say I have a url like:
www.site.com/folder1/page.php
现在我想将url重写为(例如)
Now I want to rewrite the url to (for example)
www.site.com/apple/page.php
folder1 是我网站空间上的一个现有文件夹.
The folder1 is an existing folder on my webspace.
重要:apple"不是文件夹而只是一个名称!
好的,这是一个循序渐进的计划:
Ok here is a step by step plan:
- 用户类型
www.site.com/folder1/login.php
- url 应该重写而不是将 url 重定向到
www.site.com/apple/login.php
这意味着 apple
只是一个名字而不是一个目录.所有代码都应该来自folder1.实际上,apple 应该只是 folder1
的别名.我不能只是将 folder1
重命名为 Apple
.因此,我只需将 folder1
重写为 apple
.
This means that apple
is just a name and not a directory. All the code should just come from folder1. Acutally apple should just be an alias for folder1
. I can't just rename folder1
to Apple
. Therefor I would just rewrite folder1
to apple
.
推荐答案
mod_rewrite 只能重写/重定向请求的 URI.因此,您需要请求 /apple/...
将其重写为相应的 /folder1/...
.
mod_rewrite can only rewrite/redirect requested URIs. So you would need to request /apple/…
to get it rewritten to a corresponding /folder1/…
.
试试这个:
RewriteEngine on
RewriteRule ^apple/(.*) folder1/$1
此规则会将每个以 URI 路径 /apple/...
开头的请求在内部重写为 /folder1/...
.
This rule will rewrite every request that starts with the URI path /apple/…
internally to /folder1/…
.
编辑 因为您实际上正在寻找相反的方式:
Edit As you are actually looking for the other way round:
RewriteCond %{THE_REQUEST} ^GET /folder1/
RewriteRule ^folder1/(.*) /apple/$1 [L,R=301]
此规则旨在与上述其他规则一起使用./folder1/...
的请求将被外部重定向到 /apple/...
并且 /apple/...
的请求将被内部重写回/folder1/...
.
This rule is designed to work together with the other rule above. Requests of /folder1/…
will be redirected externally to /apple/…
and requests of /apple/…
will then be rewritten internally back to /folder1/…
.
这篇关于使用 .htaccess 重写文件夹名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!