htaccess到除索引文档以外的子域的重写路径 [英] htaccess Rewrite path to subdomain except the Index Document
问题描述
我的网站结构如下,我需要将所有流量重定向到子域
I have the following structure in my website and I need to redirect all the traffic to a subdomain
结构:
domain.com/subdomain/
.../folder1/folder/1.txt
.../folder1/folder/2.txt
.../folder2/folder/1.txt
.../folder2/folder/1.txt
.../index.php
我想将除index.php或(domain.com/subdomain/)以外的所有流量重定向到子域
I want to redirect all the traffic except the index.php or (domain.com/subdomain/) to a subdomain
示例:
domain.com/subdomain/folder1/folder/1.txt ---> subdomain.domain.com/folder1/folder/1.txt
domain.com/subdomain/folder1/folder/2.txt ---> subdomain.domain.com/folder1/folder/1.txt
但是
domain.com/subdomain/index.php ---> No redirect
domain.com/subdomain/ ---> No redirect
这是我想出的:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ http://subdomain.domain.com/$1 [NC]
这适用于所有请求,但我想同时排除两者/&来自RewriteRule的index.php
This works for all the request but I want to exclude both / & index.php from this RewriteRule
谢谢!
推荐答案
您会比您那里需要的更多。您必须首先使用 RewriteCond
检查域。模式 ^ subdomain /?(index\.php)?$
应该将请求与子域根目录或 index.php
是否包含 /
。从技术上讲,它也将匹配无效的 subdomainindex.php
之间没有 /
的情况,但无论如何都会导致404。
You'll need a bit more than you have there. You must first check the domain with RewriteCond
. The pattern ^subdomain/?(index\.php)?$
should match requests to the subdomain root or index.php
with or without /
. Technically it would also match the invalid subdomainindex.php
without a /
between but that would result in a 404 anyway.
RewriteEngine On
# If the requested domain isn't already the subdomain...
RewriteCond %{HTTP_HOST} !^subdomain\. [NC]
# Rewrite it to the subdomain
# unless it is a request for index.php or /
RewriteRule ^subdomain/?(index\.php)?$ - [L]
RewriteRule ^subdomain/(.*) http://subdomain.com/$1 [L,R=301,NC]
这篇关于htaccess到除索引文档以外的子域的重写路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!