如何编写 htaccess 重写规则,您可以在文件夹中加载网站,就像从根目录加载一样
我有以下文件夹结构
ROOT (loaded on domain.com)
+campaigns
|+campaign1
|-assets
|-index.php
|-campaign2
.htaccess
index.php
stuff.php
目前要访问文件夹 campaign1
内的网站,我必须在 URL 地址栏中输入:domain.com/campaigns/campaign1
什么我应该放入 .htaccess
文件中,以便当您放入 domain.com/campaign1
浏览器加载并显示 domain.com/campaigns/campaign1
中的所有内容,但当然不会明显更改地址栏中的 URL。
非常感谢您的帮助。
I have the following folder structure
ROOT (loaded on domain.com)
+campaigns
|+campaign1
|-assets
|-index.php
|-campaign2
.htaccess
index.php
stuff.php
At the moment to access the website inside the folder campaign1
i would have to enter in the URL address bar: domain.com/campaigns/campaign1
What should i put in the .htaccess
file so that when you putdomain.com/campaign1
the browser loads and shows everything from domain.com/campaigns/campaign1
but of course without visibly changing the URL in the address bar.
Thank you so much for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 mod_rewrite 在根
.htaccess
文件中执行类似以下操作。如果“campaign”子目录未知(或太多),则这是一个通用版本:其中
$1
是对RewriteRule<中捕获的子组的反向引用/code> 模式。
由于
campaign1
是您应该请求campaign1/
的目录(带有尾部斜杠)否则 mod_dir 将发出外部重定向以附加尾部斜杠。同样,您应该请求
example.com/compaign1/
- 末尾带有斜杠 -并且上述规则假设您是。如果省略尾部斜杠,它不会执行任何操作,并且您将收到 404。(如果您期望省略尾部斜杠的第三方请求,那么您将需要手动发出 301 重定向以附加尾部斜杠 在上述规则之前。)更新:
是的,你可以这样做。事实上,如果您希望以这种方式重写的活动数量有限,那么这甚至可能更好,因为它避免了文件系统检查。
您还可以删除第二条规则(上面第 1 节的第二部分),该规则阻止在请求文件或目录时进行进一步处理。 IE。删除以下内容:
因此,您可以执行以下操作,而不是(或之前)上面的第#2 节...
对于单个广告系列:
对于多个广告系列:
请注意,它具体是
$1 =campaign1
,即=
是 CondPattern (第二个参数)本身的前缀运算符。=
运算符使其成为精确的字符串匹配,而不是正则表达式。或者,使用正则表达式和交替:(
尽管这可以组合成单个指令。)
UPDATE#2:
在内部重写之前添加此内容以在以下位置添加尾部斜杠URL 的末尾:
请注意,所有内部链接必须已经链接到带有尾部斜杠的 URL。此重定向仅是为了搜索引擎和第三方链接的利益,这些链接可能引用不带尾部斜杠的非规范 URL。
You could do something like the following in the root
.htaccess
file using mod_rewrite. This is a general version if the "campaign" subdirectories are not known (or too many):Where
$1
is a backreference to the captured subgroup in theRewriteRule
pattern.Since
campaign1
is a directory you should be requestingcampaign1/
(with a trailing slash) otherwise mod_dir is going to issue an external redirect to append the trailing slash.Likewise, you should be requesting
example.com/compaign1/
- with a trailing slash - and the above rule assumes that you are. It won't do anything if the trailing slash is omitted and you will get a 404. (If you are expecting third party requests where the trailing slash is omitted then you will need to manually issue a 301 redirect to append the trailing slash before the above rule.)UPDATE:
Yes, you can do this. In fact, if the number of campaigns you wish to rewrite in this way is limited then this may even be preferable since it avoids the filesystem check.
You may also be able to remove the second rule (second part of section#1 above) that prevents further processing should a file or directory be requested. ie. Remove the following:
So, instead of (or before) section#2 above you could do the following...
For a single campaign:
For multiple campaigns:
Note that it is specifically
$1 =campaign1
, the=
is a prefix-operator on the CondPattern (2nd argument) itself. The=
operator makes it an exact string match, not a regex.Or, using a regex and alternation:
(Although this could be combined into a single directive.)
UPDATE#2:
Add this before the internal rewrites to add a trailing slash at the end of the URL:
Note that all internal links must already linking to the URL with a trailing slash. This redirect is only for the benefit of search engines and third party links that might be referencing the non-canonical URL without a trailing slash.