apache .htaccess 中的 RewriteCond 和 RewriteRule

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1216075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:00:54  来源:igfitidea点击:

RewriteCond and RewriteRule in .htaccess

apache.htaccessmod-rewrite

提问by coderama

I have a client folder located at http://www.example.com/clientHowever, I've now installed SSL on the server, and want to add a permanent redirect using HTACCESS so that whenever /client is accessed, that it redirects to: https://www.example.com/client

我有一个位于http://www.example.com/client的客户端文件夹 但是,我现在已经在服务器上安装了 SSL,并且想要使用 HTACCESS 添加一个永久重定向,以便每当 /client 被访问时,它会重定向到:https: //www.example.com/client

Anybody know how to do that?

有人知道怎么做吗?

I've redirected my domains in the past like this:

我过去曾像这样重定向过我的域:

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/ [L,R=301]

This should not affect the solution, but the site must still redirect to www.example.com FIRST, and then to https://www.example.com/clientif for example, http://www.example.co.za/clientis entered.

这应该不会影响解决方案,但该站点仍必须首先重定向到 www.example.com,然后再重定向到https://www.example.com/client,例如http://www.example.co.za /client被输入。

回答by Gumbo

Try this:

尝试这个:

RewriteCond %{HTTPS} !on
RewriteRule ^client(/.*)?$ https://www.example.com%{REQUEST_URI} [L,R=301]

回答by David Valentino

RewriteEngine On RewriteRule ^/?$ https://www.example.com/client[301,NC,L]

RewriteEngine On RewriteRule ^/?$ https://www.example.com/client[301,NC,L]

It tells the apache, whenever the url is https://www.example.comor either with slash at the end, will redirect to ur /client

它告诉 apache,无论何时 url 是https://www.example.com或末尾带有斜杠,都将重定向到 ur /client