如何使用 htaccess 将所有子文件重定向到链接目录中的主链接

发布于 2025-01-11 01:12:50 字数 1024 浏览 0 评论 0原文

您好,我有这样的链接目录:

www.example.com/a-letter/a-1

www.example.com/a-letter/a-2

www.example.com/a-letter/a-3

我使用以下代码从链接中删除了 a-letter 文件夹名称:

RewriteCond %{THE_REQUEST} /a-letter/ [NC]
RewriteRule ^a-letter/(.*)$ /$1 [L,R=301,NC,NE]

并且我想重定向全部 a-1 a-2a-3 文件位于 a-letter 文件夹下,如下所示,带有一个 htaccess 代码,

www.example.com/a-1
www.example.com/a-2
www.example.com/a-3

因为我正在使用此代码对于每个链接,我想为此使用一个代码。我如何指定所有文件的代码,

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^a-1$ /a-letter/a-1.html [L]
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^a-2$ /a-letter/a-2.html [L]
</IfModule>

如果您能帮助我,我将不胜感激。

Hi i have link directories like this:

www.example.com/a-letter/a-1

www.example.com/a-letter/a-2

www.example.com/a-letter/a-3

i removed a-letter folder name from the link with this code:

RewriteCond %{THE_REQUEST} /a-letter/ [NC]
RewriteRule ^a-letter/(.*)$ /$1 [L,R=301,NC,NE]

and i want to redirect all a-1 a-2 and a-3 files under the a-letter folder to like below with one htaccess code

www.example.com/a-1
www.example.com/a-2
www.example.com/a-3

because i am using this codes for each link and i want to use one code for this. How can i specify the code for all files

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^a-1$ /a-letter/a-1.html [L]
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^a-2$ /a-letter/a-2.html [L]
</IfModule>

I will be grateful if you could help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

迷途知返 2025-01-18 01:12:51

这可能就是您正在寻找的:

RewriteEngine on

RewriteRule ^/?a-letter/(.*)(/|\.html)?$ /$1 [R=301,L]

RequestCond %{DOCUMENT_ROOT}/a-letter%{REQUEST_URI}.html -f
RewriteRule ^/?(a-1|a-2)/? /a-letter/$1.html [END]

另一种选择是,如果名称方案类似于“a-.html”:

RewriteEngine on

RewriteRule ^/?a-letter/(.*)(/|\.html)?$ /$1 [R=301,L]

RequestCond %{DOCUMENT_ROOT}/a-letter%{REQUEST_URI}.html -f
RewriteRule ^/?a-(\d+)/? /a-letter/a-$1.html [END]

我个人不使用那些 条件...是的,这可能会正式防止内部服务器错误。但这有什么帮助呢?

  1. 在没有应用这些规则的情况下运营您的网站很可能没有多大意义。
  2. 你知道你是否安装了重写模块。而且你很可能不会那么频繁地改变。

更新:

在评论中,您提出了另一个问题(再次:请在将来为单独的问题打开一个单独的问题......)。我建议这样的实现:

RewriteEngine on 

RewriteRule ^/?a-letter/(.*)(/|\.html)?$ /$1 [R=301,L]
RewriteRule ^/?comments/a/(.*)(/|\.html)?$ /$1 [R=301,L] 

RequestCond %{DOCUMENT_ROOT}/a-letter%{REQUEST_URI}.html -f
RewriteRule ^/?a-(\d+)/? /a-letter/a-$1.html [END]

RequestCond %{DOCUMENT_ROOT}/comments/a%{REQUEST_URI}.html -f 
RewriteRule ^/?(.*)/? /comments/a/$1.html [END]

您的指令看起来不错,可能您遇到的问题是指令的顺序。请始终记住,指令是从上到下处理的。您的最后一个 RewriteRule 非常通用,它将匹配 anything 。始终将更通用的规则保留在底部,将更精确的例外保留在顶部。并且总是尝试修改规则,使其不那么通用:

那么

RewriteRule ^/?(.*)/? /comments/a/$1.html [END]

也许类似的事情

RewriteRule ^/?(\w*)/? /comments/a/$1.html [END]

是可能的?

This probably is what you are looking for:

RewriteEngine on

RewriteRule ^/?a-letter/(.*)(/|\.html)?$ /$1 [R=301,L]

RequestCond %{DOCUMENT_ROOT}/a-letter%{REQUEST_URI}.html -f
RewriteRule ^/?(a-1|a-2)/? /a-letter/$1.html [END]

An alternative would be that, if the name scheme is some thing like "a-.html":

RewriteEngine on

RewriteRule ^/?a-letter/(.*)(/|\.html)?$ /$1 [R=301,L]

RequestCond %{DOCUMENT_ROOT}/a-letter%{REQUEST_URI}.html -f
RewriteRule ^/?a-(\d+)/? /a-letter/a-$1.html [END]

I personally do not use those <IfModule mod_rewrite.c> conditions ... Yes, the might formally prevent an internal server error. But what does that help?

  1. Operating your site without those rules getting applied most likely does not make much sense.
  2. You know whether you have installed the rewriting module or not. And you are most likely not going to change that frequently.

UPDATE:

In the comments you asked another question (again: please open a separate question in future for separate questions...). I would suggest such implementation:

RewriteEngine on 

RewriteRule ^/?a-letter/(.*)(/|\.html)?$ /$1 [R=301,L]
RewriteRule ^/?comments/a/(.*)(/|\.html)?$ /$1 [R=301,L] 

RequestCond %{DOCUMENT_ROOT}/a-letter%{REQUEST_URI}.html -f
RewriteRule ^/?a-(\d+)/? /a-letter/a-$1.html [END]

RequestCond %{DOCUMENT_ROOT}/comments/a%{REQUEST_URI}.html -f 
RewriteRule ^/?(.*)/? /comments/a/$1.html [END]

Your directives look fine, it might be that the issue you ran into is the order of directives. Always keep in mind that the directives are processed from top to bottom. Your last RewriteRule is very generic, it will match anything . Always keep more generic rules at the bottom, more precise exceptions further atop. And always try to modify rules such that they are less generic:

So instead of

RewriteRule ^/?(.*)/? /comments/a/$1.html [END]

maybe something like that

RewriteRule ^/?(\w*)/? /comments/a/$1.html [END]

is possible?

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