找到你要的答案

Q:.htaccess How to rewrite subdomain to root and main domain to subfolder

Q:.htaccess如何重写主域和子域的根文件夹

I have a following htaccess code:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/.*
RewriteRule ^(.*)$ subfolder/another/$1 [L]

RewriteCond %{REQUEST_URI} !^/subfolder/another/
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ subfolder/another/index.php

So when I go to domain.com it goes to root/subfolder/another And when I open sub.domain.com it also goes to root/subfolder/another, but I want it to go to root.

So I added this in addition to above:

RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

but it still goes to root/subfolder/another What should I do?

我有一个.htaccess代码如下:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/.*
RewriteRule ^(.*)$ subfolder/another/$1 [L]

RewriteCond %{REQUEST_URI} !^/subfolder/another/
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ subfolder/another/index.php

So when I go to domain.com it goes to root/subfolder/another And when I open sub.domain.com it also goes to root/subfolder/another, but I want it to go to root.

所以我除了上面加了这个:

RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

but it still goes to root/subfolder/another What should I do?

answer1: 回答1:

If you want a rule to only match when you request the site from domain.com, make a condition that only matches when you request the site from domain.com. Besides this, it makes no sense to me what your first two rules are doing together.

Also, escape dots when you want to match literal dots.

RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ subfolder/another/index.php [L]

RewriteCond %{HTTP_HOST} ^sub\.domain\.com$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

如果你想有一个规则,只配当你请求的网站,使一个条件,只有比赛的时候,你要求的网站从Domain.com。除此之外,我不明白你的前两条规则是一起做什么。

此外,当您想匹配文字点时,请避开点。

RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ subfolder/another/index.php [L]

RewriteCond %{HTTP_HOST} ^sub\.domain\.com$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
.htaccess  mod-rewrite