1

我的网站中有以下结构,我需要将所有流量重定向到子域

结构:

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/) 之外的所有流量重定向到子域

例子:

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

谢谢!

4

1 回答 1

1

你需要的比那里多一点。您必须首先使用 . 检查域RewriteCond。该模式^subdomain/?(index\.php)?$应匹配对子域根的请求或index.php带有或不带有/. 从技术上讲,它也可以匹配subdomainindex.php没有/between 的无效值,但无论如何都会导致 404。

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]
于 2012-12-29T13:29:14.253 回答