apache - 如何在主域上重写子域网址?
问题描述
我需要帮助来配置我的 htaccess。我需要将所有子域 url 重新写入主域。
这是我的 htaccess:
RewriteEngine On
Options +SymLinksIfOwnerMatch
# http => https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# add www to main domain
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
# language subdomains handler
RewriteCond %{QUERY_STRING} !(?:^|&)lang= [NC]
RewriteCond %{HTTP_HOST} ^([a-z]{2})\.
RewriteRule ^ %{REQUEST_URI}?lang=%1 [QSA,L]
我还有其他类似这样的重写网址:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteRule ^about/$ /about.php [NC]
此页面在主域上运行良好。 https://www.example.com/about/
但在子域中我得到 404 错误。 https://en.example.com/about/
谁能指导我如何解决它?
PS:这是另一个相关的问题: 如何通过htaccess和子域配置多种语言作为GET方法?
谢谢
解决方案
您可以像这样拥有完整的 .htaccess:
Options +SymLinksIfOwnerMatch
RewriteEngine On
# http => https
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# add www to main domain
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
# language subdomains handler
RewriteCond %{QUERY_STRING} !(?:^|&)lang= [NC]
RewriteCond %{HTTP_HOST} ^([a-z]{2})\.
RewriteRule ^ %{REQUEST_URI}?lang=%1 [QSA]
# php extension handler e.g. /about/ => /about.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
推荐阅读
- sql - 使用查询结果作为表名进行更新
- angular - 使用 rxjs 在 Angular 中获取 FireStorage URL,直到渲染
- android - altBeacon: didEnterRegion
- c# - C# itext - 在将其写入新 PDF 时,我如何知道它是否进入了下一页?
- azure-machine-learning-studio - 如何从 AzureML 文件共享访问本地文件?
- python - 我想向 discord.py 中的所有服务器发送消息,但是命令向我发送错误:
- r - SIR 模型(灰色)易感和(红色)感染。在受感染和 close_contact 中不断出现错误:不合格的数组
- flutter - Flutter - blurRadius 奇怪的行为
- spring - Spring Boot WebFlux Kotlin R2DBC 暂停分页返回类型
- rjava - 运行代码几次后出现“超出 GC 开销限制”错误