复杂的 mod_rewrite。有什么想法吗?

发布于 2024-09-08 23:49:03 字数 1440 浏览 1 评论 0 原文

我想要 : - 如果使用 http,则从 http 切换到 https - 将子域重定向到index?o=子域(www除外) - 将子目录重定向到index?u=user

示例: http://www.mydomain.com 将被重定向到 https://www.mydomain.com

http:// subdomain.mydomain.com 将被重定向到 https://www.mydomain.com /index?o=subdomain

https://subdomain.mydomain.com 将被重定向到 < a href="https://www.mydomain.com/index?o=subdomain" rel="nofollow noreferrer">https://www.mydomain.com/index?o=subdomain

http://subdomain.mydomain.com/user 将被重定向到 https://www.mydomain.com/index?o=subdomain&u=user

https://subdomain.mydomain.com/user 将被重定向到 https://www.mydomain.com/index?o=subdomain&u=user

mod_Rewrite 是最好的选择吗?有什么想法吗?

提前致谢

I want to :
- switch from http to https if http is used
- redirect the subdomain to index?o=subdomain except www
- redirection the subdirectory to index?u=user

Example :
http://www.mydomain.com will be redirected to https://www.mydomain.com

http://subdomain.mydomain.com will be redirected to https://www.mydomain.com/index?o=subdomain

https://subdomain.mydomain.com will be redirected to https://www.mydomain.com/index?o=subdomain

http://subdomain.mydomain.com/user will be redirected to https://www.mydomain.com/index?o=subdomain&u=user

https://subdomain.mydomain.com/user will be redirected to https://www.mydomain.com/index?o=subdomain&u=user

Is mod_Rewrite the best to do that ? Any idea ?

Thanks in advance

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

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

发布评论

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

评论(1

冷情妓 2024-09-15 23:49:03

我现在没有时间测试它,但你可以尝试一下,看看它是否有效。有些事情可能会出现问题,所以如果您遇到问题,我很乐意稍后解决任何问题。另外,我认为我已经涵盖了您想做的所有事情,但如果我遗漏了什么,请告诉我。

RewriteEngine On

# Force redirect to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}/$0 [R=301,L]

编辑:我更新了下面的规则集。不过,我考虑了您的问题,尝试通过 TLS/SSL 提供子域时不会遇到问题吗?除此之外,以下之一应该执行您想要的操作(我希望这次没有错误):

如果您想要内部重定向:

RewriteCond %{HTTP_HOST}    !=mydomain.com
RewriteCond %{HTTP_HOST}    !^www
RewriteCond %{REQUEST_URI}  !^/index
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^([^\.]+)[^/]*/([^/]+)?
RewriteCond %1&u=%2          ^([^&]+)(&u=.+)?
RewriteRule ^.*$ /index?o=%1%2

如果您想要外部重定向:

RewriteCond %{HTTP_HOST}    !=mydomain.com
RewriteCond %{HTTP_HOST}    !^www
RewriteCond %{REQUEST_URI}  !^/index
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^([^\.]+)[^/]*/([^/]+)?
RewriteCond %1&u=%2          ^([^&]+)(&u=.+)?
RewriteRule ^.*$ https://www.mydomain.com/index?o=%1%2 [R=301,L]

I don't have time to test it right now, but you can try this and see if it works. There may be some potential for some things to go wrong, so if you have trouble with it I'd be happy to work out any kinks later. Also, I think that I covered everything you wanted to do, but let me know if I left something out.

RewriteEngine On

# Force redirect to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}/$0 [R=301,L]

Edit: I've updated the ruleset below. I thought about your question though, and aren't you going to have issues attempting to serve up your subdomains over TLS/SSL? That aside, one of the following should do what you want (without errors this time, I hope):

If you wanted internal redirection:

RewriteCond %{HTTP_HOST}    !=mydomain.com
RewriteCond %{HTTP_HOST}    !^www
RewriteCond %{REQUEST_URI}  !^/index
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^([^\.]+)[^/]*/([^/]+)?
RewriteCond %1&u=%2          ^([^&]+)(&u=.+)?
RewriteRule ^.*$ /index?o=%1%2

If you wanted external redirection:

RewriteCond %{HTTP_HOST}    !=mydomain.com
RewriteCond %{HTTP_HOST}    !^www
RewriteCond %{REQUEST_URI}  !^/index
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^([^\.]+)[^/]*/([^/]+)?
RewriteCond %1&u=%2          ^([^&]+)(&u=.+)?
RewriteRule ^.*$ https://www.mydomain.com/index?o=%1%2 [R=301,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文