Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php htaccess子域重写条件/规则_Php_.htaccess_Url_Dns_Rewrite - Fatal编程技术网

Php htaccess子域重写条件/规则

Php htaccess子域重写条件/规则,php,.htaccess,url,dns,rewrite,Php,.htaccess,Url,Dns,Rewrite,我想重写我的子域和域中的所有索引文件,如/index。到目前为止,还没有必要使用子域。现在的问题是htaccess文件中的重写规则。这将重写URL,如下面给定的代码所示: RewriteRule ^index\.(htm|html|php) http://%{HTTP_HOST}/ [R=301,L] RewriteRule ^(.*)/index\.(htm|html|php) http://%{HTTP_HOST}/$1/ [R=301,L] 意味着域中的所有索引文件都将被重写。这很好,但

我想重写我的子域和域中的所有索引文件,如
/index
。到目前为止,还没有必要使用子域。现在的问题是htaccess文件中的重写规则。这将重写URL,如下面给定的代码所示:

RewriteRule ^index\.(htm|html|php) http://%{HTTP_HOST}/ [R=301,L]
RewriteRule ^(.*)/index\.(htm|html|php) http://%{HTTP_HOST}/$1/ [R=301,L]
意味着域中的所有索引文件都将被重写。这很好,但在子域中不起作用。现在我想我可以简单地为我的子域添加一个条件,如:

RewriteCond %{HTTP_HOST} !^subdomain\.example\.com$
RewriteCond %{HTTP_HOST} !^www\.
但幸运的是,这不起作用

要在此处提供更多信息,请参见URL的外观:

http://www.example.com/index
and the subdomain:
http://subdomain.example.com/index
使用上面的代码,URL将是:

http://www.example.com/
and the subdomain:
http://subdomain.example.com/index
如果有人能帮我,那就太好了

非常感谢

更新:

为了提供进一步的信息,我需要解释事情是如何运作的

在根目录中有子域的文件夹

                                    --> /index.php
                      --> /folderA 
       --> /subdomain               
/root                 --> /folderB
                                    --> /index.php
URL的外观如下所示:

http://www.example.com/subdomain/folderA/index

我确实使用干净的URL,所以它只是
index
,而不是
index.php
等等。 调用页面时,默认设置已隐藏index.php。问题是什么时候我会改变语言,什么是folderA和folderB。因此,我读取了文件的basename,并使用header函数重定向到right dir。主要问题是子域。在这个领域,它工作得很好。就在我从一个带有URL的文件夹中获得索引页时:

http://subdomain.example.com/folderA/(index will be hidden)
并将读取basename(=>索引)并将标题发送至:

http://subdomain.example.com/folderB/(index will be hidden)
这将导致一个问题。URL将以错误的方式重写。或者另一个简单的例子:

root/subdomain/folderA/

此按钮只是一个链接:
。页面URL将是:
subdomain.example.com/folderA/filexy
单击该链接时,URL将被重写为
www.example.com/subdomain/folderA/
尝试以下操作:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^subdomain\.example\.com$ [NC]
    RewriteRule ^/index\.(htm|html|php)$  http://%{HTTP_HOST}/$1 [NC,R=301,L]    
</IfModule>

重新启动发动机
重写cond%{HTTP_HOST}^子域\.example\.com$[NC]
重写规则^/index\(html | php)$http://%{http_HOST}/$1[NC,R=301,L]

您的规则如下:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# do nothing if subdomain
RewriteCond %{HTTP_HOST} !^subdomain\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule - [L]

RewriteRule ^index\.(htm|html|php) http://%{HTTP_HOST}/ [R=301,L]
RewriteRule ^(.*)/index\.(htm|html|php) http://%{HTTP_HOST}/$1/ [R=301,L]
PS:尽管我建议看一下,然后你可以用这行代码替换上面的代码:

DirectoryIndex index.htm index.html index.php

我的错,我把OR开关留在那里了(去掉“、OR”请修改规则,在那里添加正确的子域名,然后再试一次,这样你就不想将子域名的
/admin/index.html
重定向到
/admin/
?您好,谢谢您回答这个问题。好的,当使用您的代码时,会发生一些奇怪的事情。当我在该子域中有一个类似
的链接时,它会我将创建一个重写的URL,如
subdomain.example.com/subdomain/folder/
,因为您也可以看到,上面的代码中没有规则使
/subdomain/folder/
。在/subdomain文件夹中可能有其他一些.htaccess。根目录中只有htaccess。
DirectoryIndex index.htm index.html index.php