如何使用 htaccess 将所有子文件重定向到链接目录中的主链接
您好,我有这样的链接目录:
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-2
和 a-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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能就是您正在寻找的:
另一种选择是,如果名称方案类似于“a-.html”:
我个人不使用那些
条件...是的,这可能会正式防止内部服务器错误。但这有什么帮助呢?更新:
在评论中,您提出了另一个问题(再次:请在将来为单独的问题打开一个单独的问题......)。我建议这样的实现:
您的指令看起来不错,可能您遇到的问题是指令的顺序。请始终记住,指令是从上到下处理的。您的最后一个
RewriteRule
非常通用,它将匹配 anything 。始终将更通用的规则保留在底部,将更精确的例外保留在顶部。并且总是尝试修改规则,使其不那么通用:那么
也许类似的事情
是可能的?
This probably is what you are looking for:
An alternative would be that, if the name scheme is some thing like "a-.html":
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?UPDATE:
In the comments you asked another question (again: please open a separate question in future for separate questions...). I would suggest such implementation:
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
maybe something like that
is possible?