htaccess的子域重写

编程入门 行业动态 更新时间:2024-10-28 01:15:32
本文介绍了htaccess的子域重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有很多话题在这里大约子域,但没有人可以帮我...

It is many topics here about subdomains but no one can help me...

我用htacces设置子域文件夹

I use htacces to set subdomain to folder

所以,如果我们把 en.example/something

我用的是这样的。

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\$ RewriteRule ^(.*)$ example/%1/$1 [NC]

这工作正常,但联系地址在栏更改为 example/en/something但我想继续 en.example/something

This works fine but adress in bar is changed to example/en/something but I want keep en.example/something

所以我想这个

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\$ RewriteRule ^([^.]+)\.example\(.*) /$1/$2

或只是

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\$ RewriteRule ^(.*)$ %1/$1 [NC]

但这不起作用。任何解决方案或想法?

but this doesn't work. Any solution or ideas ?

一个解决方案是使用的语言有(example/en/something),我把它改写,但之后如果我工作的子目录我得到这样的 example/subdirectory/en/something - 可怕的。 也许我喜欢 example/en/subdirectory/something 芽如何进行这个。 ..

One solution is use language there (example/en/something) where I rewrite it but after If I work on subdirectories I get something like example/subdirectory/en/something - terrible. Maybe I like example/en/subdirectory/something bud how proceed this...

和还对一些私人服务器第一个案例送我去可能默认域,所以它不是为我工作。 (也许这是某些服务器条件或设置)

And also on some private servers first one case send me to "maybe" default domain, so it is not working for me. (maybe this is some server condition or settings)

推荐答案

我知道这是晚了一个月,但也许这仍然将是为别人有用。这里有几件事情:

I know this is a month late, but maybe this will still be useful for somebody. A few things here:

关于你的第一个重写规则:

Regarding your first RewriteRule:

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\$ RewriteRule ^(.*)$ example/%1/$1 [NC]

,因为它似乎你已经发现,改写到另一个URL也将用户的浏览器重定向到新的URL,即使是在自己的域名。为了掩盖它,你必须重写文件路径在服务器上(像你做你的下两个规则)。

As it seems you already discovered, rewriting to another URL will also redirect the user's browser to that new URL, even if it's at your own domain. To keep it hidden, you have to rewrite to a file path on the server (like you do with your next two rules).

关于你的第二个重写规则:

Regarding your second RewriteRule:

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\$ RewriteRule ^([^.]+)\.example\(.*) /$1/$2

问题存在,你不能域名匹配的重写规则,只有URL路径。如果您的网址是 www.example/something/somethingelse ,你想匹配字符串就是什么/ somethingelse 。换句话说,它排除的 www.example / ,让你有这样的重写规则的模式永远不会匹配的域名,因为该模式甚至没有被对URL的一部分进行测试,但你包括在模式的域名,导致匹配失败。

The problem there is that you can't match the domain name in the RewriteRule, only the URL path. If your URL is www.example/something/somethingelse, the string you're trying to match is just something/somethingelse. In other words, it excludes www.example/, so this RewriteRule pattern you have will never match the domain name because the pattern isn't even being tested against that part of the URL, but you included the domain name in the pattern, causing the match to fail.

关于你的第三个重写规则:

Regarding your third RewriteRule:

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\$ RewriteRule ^(.*)$ %1/$1 [NC]

这看起来像它应该工作,所以我不能肯定地说,为什么它不是不知道更多关于你的文件的组织在服务器上,等等。比方说,你把所有的网站的文件中的的/ home /人/的public_html / 。为了让重写规则的工作,因为它是现在,你需要有一个连接子目录的public_html 。所以,如果有人去的 en.example/something ,该重写规则会导致Apache来服务于 /家/人/的public_html / EN /某事文件。我的猜测为什么它不工作对你来说,你可能有子域名指向的地方比其他的public_html (假设你居然在我的例子举办类似的网站文件)。请记住,你重写什么( / $ 1 / $ 2 在这种情况下)是服务器,而不是URL到你的网站上的文件路径。

This looks like it should work, so I can't say for sure why it isn't without knowing more about how your files are organized on the server and so forth. Let's say you have all of the website's files in /home/somebody/public_html/. In order for the RewriteRule to work as it is right now, you would need to have an en subdirectory in public_html. So, if somebody went to en.example/something, the RewriteRule would cause Apache to serve the file at /home/somebody/public_html/en/something. My guess why it's not working for you is that you might have the subdomain pointing somewhere other than public_html (assuming you actually had the website files organized like in my example). Remember that what you're rewriting to (/$1/$2 in this case) is a file path on the server, not a URL to your website.

我希望帮助!你可能已经解决了这个了,但即使你有,我希望其他人仍然会发现这个有用。

I hope that helps! You may have already solved this by now, but even if you have, I'm hoping other people will still find this useful.

更多推荐

htaccess的子域重写

本文发布于:2023-10-31 17:30:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546779.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   htaccess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!