【问题标题】:.htaccess Rewrite for different domains.htaccess 为不同的域重写
【发布时间】:2021-12-25 07:43:00
【问题描述】:

以下是我尝试过的现有 .htaccess 规则设置

RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^api/([a-z]{1,3})/([0-9a-zA-Z]{1,15})$ api.php?ac=$2&type=$1 [L]
RewriteRule ^s/([0-9a-zA-Z]{1,15})$ init.php?ac=$1&type=s [L]
RewriteCond %{REQUEST_URI} !^.*\.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %{REQUEST_FILENAME}.php

我将几个域 exampleA.com 和 exampleB.com 停放在 example.com

我想重写下面的case

example.com/s -> error 404 page
example.com/s/ -> error 404 page
example.com/s/abc -> init.php?type=s&account=abc
example.com/api/s/abc -> api.php?type=s&account=abc

example.com 除外

exampleA.com -> init.php?type=d
exampleB.com -> init.php?type=d
exampleA.com/abc -> init.php?type=d&account=abc
exampleB.com/abc -> init.php?type=d&account=abc
exampleA.com/api/abc -> api.php?type=d&account=abc
exampleB.com/api/abc -> api.php?type=d&account=abc

如何编写 RewriteCond 和 RewriteRule 来存档?

【问题讨论】:

  • 在使用提供的解决方案时,请确保您拥有子域的通用目录结构。

标签: apache .htaccess mod-rewrite


【解决方案1】:

根据您显示的尝试,请尝试遵循您的规则文件中的 htaccess 规则。在测试您的 URL 之前,请确保您已清除浏览器缓存。这也考虑到您所有的多个子域都具有相同的文件夹路径。

RewriteEngine ON
##Apply https to urls.
RewriteCond %{HTTPS} off 
RewriteRule ^(.*)/?$ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]

##Rules for example.com/s OR example.com/s/ to redirect to 404 here.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^s/?$ - [R=404,NC,L]

##Rules for example.com/s/abc -> init.php?type=s&account=abc
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(s)/(abc)/?$ init.php?type=$1&account=$2 [NC,QSA,L]

##Rules for example.com/api/s/abc -> api.php?type=s&account=abc
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(api)/(s)/(abc)/?$ $1.php?type=$2&account=$3 [NC,QSA,L]

##Rules for exampleA.com -> init.php?type=d AND for exampleB.com -> init.php?type=d
RewriteCond %{HTTP_HOST} ^example(A|B)\.com$ [NC]
RewriteRule ^/?$ init.php?type=d [L]

##Rules for exampleA.com/abc -> init.php?type=d&account=abc AND for exampleB.com/abc -> init.php?type=d&account=abc
RewriteCond %{HTTP_HOST} ^example(A|B)\.com$ [NC]
RewriteRule ^abc/?$ init.php?type=d&account=abc

##Rules for exampleA.com/api/abc -> api.php?type=d&account=abc AND for exampleB.com/api/abc -> api.php?type=d&account=abc
RewriteCond %{HTTP_HOST} ^example(A|B)\.com$ [NC]
RewriteRule ^(api)/(abc)/?$ $1.php?type=d&account=$2 [NC,QSA,L]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多