英文:
mod_rewrite apache FQDN to Hostname
问题
我正在尝试找到一个mod_rewrite代码,用于强制将访问完全限定域名(FQDN)的用户重写为不带域名的主机名,而且不希望他们陷入循环中。我只能找到相反方向的重写示例。有人有建议吗?
例如,如果我访问http://appname.example.com/test.php
重写后的URL应该是http://appname/test.php
任何建议都将很好。
英文:
I'm trying to find a mod_rewrite code for forcing users that go to FQDN to be re-written to the hostname without the domain and don't want them to be stuck in a loop. I can only find re-write examples for the other way round. Anyone have a suggestion on how I can do this?
So example if I was to go to http://appname.example.com/test.php
The rewritten URL should be http://appname/test.php
Any suggestions would be great.
答案1
得分: 0
将 http://<appname>.example.com/<url-path>
重定向到 http://<appname>/<url-path>
(其中 <appname>
和 <url-path>
完全可变,<appname>
也是本地网络上可解析的主机名),你可以在根目录的 .htaccess
文件顶部使用 mod_rewrite 做以下操作:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)([a-z-]+)\.example\.com [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]
这个规则使用负向先行断言来排除 www
子域名。
%1
在 替换 字符串中是从请求的 主机名 中捕获的 <appname>
的反向引用。
%{REQUEST_URI}
包含完整的相对根路径 URL 路径(以斜杠开头)。
这也处理以点结尾的完全限定域名(FQDN)。
英文:
To "redirect" http://<appname>.example.com/<url-path>
to http://<appname>/<url-path>
(where <appname>
and <url-path>
are entirely variable and <appname>
is also a resolvable hostname on the local network) then you would do something like the following using mod_rewrite at the top of the root .htaccess
file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)([a-z-]+)\.example\.com [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]
This excludes the www
subdomain using a negative lookahead.
%1
in the substitution string is a backreference to the <appname>
as captured from the requested hostname.
%{REQUEST_URI}
contains the full root-relative URL-path (starting with a slash).
This also handles FQDN (that end in a dot).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论