在htaccess RewriteRule中排除特定子域或没有子域

Ana*_*log 5 regex .htaccess mod-rewrite

我在我的网络应用程序的不同子域上保留了动态内容和静态内容,但我最近发现如果直接调用动态内容可以在我的静态子域(www.)上查看,或者根本没有子域.


网址结构:

http(s)://(subdomain).(domain).(tld)/(static page) OR (direct/random hash) OR (secure/random hash)
Run Code Online (Sandbox Code Playgroud)

所有静态内容都可以通过"WWW"访问,这导致我的SEO友好域名,如

http(s)://www.domain1.com/about
http(s)://www.domain1.com/
http(s)://www.domain2.com/about
http(s)://www.domain2.com/
Run Code Online (Sandbox Code Playgroud)

通过网络应用程序查看的动态内容可以从域等访问

http(s)://dynamic1.domain1.com/direct/randomhash
http(s)://dynamic2.domain1.com/direct/randomhash
http(s)://dynamic1.domain2.com/direct/randomhash
http(s)://dynamic2.domain2.com/direct/randomhash
Run Code Online (Sandbox Code Playgroud)

这是我当前的.htaccess文件

重写URI开头的动态链接的规则,secure或者directindex.php文件的任何文件扩展名.

Header set Connection keep-alive
<IfModule mod_dir.c>
    DirectorySlash Off
</IfModule>
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^index.php  - [L]
    RewriteRule ^(secure|direct)  /index.php [L]
    RewriteCond %{REQUEST_URI} !^/server-status
    RewriteCond %{REQUEST_URI} !^/public/cache
    RewriteRule !\.(png|gif|css|jpg|zip|js|html|htm|swf|ico|fon|ttf|otf|svg|woff|woff2|eot)$  /index.php [L]
</IfModule>
Run Code Online (Sandbox Code Playgroud)

以上目前正在运行但我的问题是,如果用户只是将用于动态内容的子域更改为WWW,它仍然可以工作,并且将导致可以从我的SEO友好子域访问动态内容.

我希望能够编辑此规则,RewriteRule ^(secure|direct) /index.php [L]以便在存在活动www.子域或没有子域的情况下将其排除在外,如果域或子域是其他任何内容,则仍在工作.

也就是说子域或域仍然是通配符,除非子域是 www.


我假设我要补充%{HTTP_HOST}RewriteRule ^(secure|direct) /index.php [L],并使用正则表达式允许任何子域,域,和TLD但林不知道如何如果HTTP主机与WWW开始排除.或根本没有子域名.

我的最终目标是,在仍然使用任何其他子域/域时,无法通过一个子域或无子域查看securedirect路径www..

Cro*_*ses 6

您可以使用:

RewriteCond %{HTTP_HOST} ^(?:www\.)?[^.]+\.[^.]+$ [NC]
RewriteRule ^(secure|direct)  /index.php [L]
Run Code Online (Sandbox Code Playgroud)