如何写一个htaccess文件夹通配符子域通配符、文件夹、如何写、htaccess

2023-09-02 20:34:48 作者:龙行天下轻衣随

我想我所有的文件夹(使用通配符)重定向到其子域。例如:

  

www.mywebsite.com/folder1到folder1.mywebsite.com/~~V   www.mywebsite.com/folder2到folder2.mywebsite.com/~~V   www.mywebsite.com/folder3到folder3.mywebsite.com /

现在无需编写规则他们每个人,有没有办法把它们全部重定向到各自的子域名?

我想:

  RewriteEngine叙述上
的RewriteCond%{HTTP_HOST}!^ WWW  .mywebsite  .COM $
的RewriteCond%{HTTP_HOST} ^( W +)。mywebsite  .COM $
重写规则^(。*)$ / 1%/ $ 1 [QSA]
 

但它给了我一个内部错误500

解决方案 在搜索文件 文件夹时,若要搜索全部的文件夹,应该输入什么搜索通配符

您需要保持规则的循环,使用条件,首先检查 /%1 / $ 1 确实存在,或的URI尚未与%1

所以:

  RewriteEngine叙述上
的RewriteCond%{HTTP_HOST}!^ WWW  .mywebsite  .COM $
的RewriteCond%{HTTP_HOST} ^( W +)。mywebsite  .COM $
的RewriteCond%{DOCUMENT_ROOT} /%1%{REQUEST_URI} -f [OR]
的RewriteCond%{DOCUMENT_ROOT} /%1%{REQUEST_URI} -d
重写规则^(。*)$ / 1%/ $ 1 [QSA]
 

或者

  RewriteEngine叙述上
的RewriteCond%{HTTP_HOST}!^ WWW  .mywebsite  .COM $
的RewriteCond%{HTTP_HOST} ^( W +)。mywebsite  .COM $
的RewriteCond%{REQUEST_URI}:%1 ^ /([^ /] +)/([^:] *):! 1
重写规则^(。*)$ / 1%/ $ 1 [QSA]
 

^ /([^ /] +)/([^:] *):! 1 EX pression团体第一次在URI文件夹,并与反向引用它 1 。如果这些都是平等的,那么在URI的第一个文件夹是%1 这是backreferenced到previous比赛( w +)

I'm trying to redirect all my folders (using a wildcard) to their subdomain. For example:

www.mywebsite.com/folder1 to folder1.mywebsite.com/ www.mywebsite.com/folder2 to folder2.mywebsite.com/ www.mywebsite.com/folder3 to folder3.mywebsite.com/

Now without writing a rule for each one of them, is there a way to redirect them all to their respective subdomain?

I tried:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mywebsite.com$
RewriteCond %{HTTP_HOST} ^(w+).mywebsite.com$
RewriteRule ^(.*)$ /%1/$1 [QSA]

But it gives me an internal error 500

解决方案

You need to keep the rules from looping, use a condition to first check if the /%1/$1 actually exists, or that the URI doesn't already start with %1

So:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mywebsite.com$
RewriteCond %{HTTP_HOST} ^(w+).mywebsite.com$
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d
RewriteRule ^(.*)$ /%1/$1 [QSA]

Or:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mywebsite.com$
RewriteCond %{HTTP_HOST} ^(w+).mywebsite.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):1
RewriteRule ^(.*)$ /%1/$1 [QSA]

The !^/([^/]+)/([^:]*):1 expression groups the first folder in the URI, and backreferences it with 1. If those are equal, then the first folder in the URI is %1 which is backreferenced to the previous match (w+).