如何编写 htaccess 重写规则,您可以在文件夹中加载网站,就像从根目录加载一样

发布于 2025-01-17 02:43:23 字数 448 浏览 1 评论 0原文

我有以下文件夹结构

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 put
domain.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天煞孤星 2025-01-24 02:43:23

您可以使用 mod_rewrite 在根 .htaccess 文件中执行类似以下操作。如果“campaign”子目录未知(或太多),则这是一个通用版本:

RewriteEngine On

# 1. Abort early if request already maps to a file or directory
RewriteRule ^campaigns/ - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# 2. Check if the first path-segment maps to a directory in "/campaigns` directory
# If so then internally rewrite the request to that subdirectory
RewriteCond %{DOCUMENT_ROOT}/campaigns/$1 -d
RewriteRule ^([^/]+)/ campaigns%{REQUEST_URI} [L]

其中$1是对RewriteRule<中捕获的子组的反向引用/code> 模式

我必须在 URL 地址栏中输入:example.com/campaigns/campaign1

由于 campaign1 是您应该请求 campaign1/ 的目录(带有尾部斜杠)否则 mod_dir 将发出外部重定向以附加尾部斜杠。

当您输入example.com/campaign1时,浏览器会加载并显示来自...的所有内容

同样,您应该请求 example.com/compaign1/ - 末尾带有斜杠 -并且上述规则假设您是。如果省略尾部斜杠,它不会执行任何操作,并且您将收到 404。(如果您期望省略尾部斜杠的第三方请求,那么您将需要手动发出 301 重定向以附加尾部斜杠 在上述规则之前。)


更新:

如果我知道进入营销活动文件夹的营销活动文件夹的名称怎么办?基本上,如果我想手动添加规则但特定于广告系列?

是的,你可以这样做。事实上,如果您希望以这种方式重写的活动数量有限,那么这甚至可能更好,因为它避免了文件系统检查。

您还可以删除第二条规则(上面第 1 节的第二部分),该规则阻止在请求文件或目录时进行进一步处理。 IE。删除以下内容:

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
重写规则 ^ - [L]

因此,您可以执行以下操作,而不是(或之前)上面的第#2 节...

对于单个广告系列:

# 2. Rewrite campaign to "/campaigns" subdirectory
RewriteRule ^compaign1/ campaigns%{REQUEST_URI} [L]

对于多个广告系列:

# 2. Rewrite campaigns to "/campaigns" subdirectory
RewriteCond $1 =campaign1 [OR]
RewriteCond $1 =campaign2 [OR]
RewriteCond $1 =campaign3
RewriteRule ^([^/]+)/ campaigns%{REQUEST_URI} [L]

请注意,它具体是 $1 =campaign1,即 =CondPattern (第二个参数)本身的前缀运算符。 = 运算符使其成为精确的字符串匹配,而不是正则表达式。

或者,使用正则表达式和交替:(

# 2. Rewrite campaigns to "/campaigns" subdirectory
RewriteCond $1 ^(campaign1|campaign2|campaign3)$
RewriteRule ^([^/]+)/ campaigns%{REQUEST_URI} [L]

尽管这可以组合成单个指令。)


UPDATE#2:

在内部重写之前添加此内容以在以下位置添加尾部斜杠URL 的末尾:

RewriteCond %{REQUEST_URI} !\.
RewriteRule !/$ %{REQUEST_URI}/ [R=301,L]

请注意,所有内部链接必须已经链接到带有尾部斜杠的 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):

RewriteEngine On

# 1. Abort early if request already maps to a file or directory
RewriteRule ^campaigns/ - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# 2. Check if the first path-segment maps to a directory in "/campaigns` directory
# If so then internally rewrite the request to that subdirectory
RewriteCond %{DOCUMENT_ROOT}/campaigns/$1 -d
RewriteRule ^([^/]+)/ campaigns%{REQUEST_URI} [L]

Where $1 is a backreference to the captured subgroup in the RewriteRule pattern.

i would have to enter in the URL address bar: example.com/campaigns/campaign1

Since campaign1 is a directory you should be requesting campaign1/ (with a trailing slash) otherwise mod_dir is going to issue an external redirect to append the trailing slash.

when you put example.com/campaign1 the browser loads and shows everything from ...

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:

What if i know the name of the campaign folder that goes into the campaigns folder? basically if i want to manually add the rule but campaign specific?

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:

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

So, instead of (or before) section#2 above you could do the following...

For a single campaign:

# 2. Rewrite campaign to "/campaigns" subdirectory
RewriteRule ^compaign1/ campaigns%{REQUEST_URI} [L]

For multiple campaigns:

# 2. Rewrite campaigns to "/campaigns" subdirectory
RewriteCond $1 =campaign1 [OR]
RewriteCond $1 =campaign2 [OR]
RewriteCond $1 =campaign3
RewriteRule ^([^/]+)/ campaigns%{REQUEST_URI} [L]

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:

# 2. Rewrite campaigns to "/campaigns" subdirectory
RewriteCond $1 ^(campaign1|campaign2|campaign3)$
RewriteRule ^([^/]+)/ campaigns%{REQUEST_URI} [L]

(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:

RewriteCond %{REQUEST_URI} !\.
RewriteRule !/$ %{REQUEST_URI}/ [R=301,L]

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文