htaccess重写条件/子域的规则 [英] htaccess rewriting condition/rule for subdomains

查看:163
本文介绍了htaccess重写条件/子域的规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  RewriteRule ^ index\。(htm | html | php) //%{HTTP_HOST} / [R = 301,L] 
RewriteRule ^(。*)/ index\。(htm | html | php)http://%{HTTP_HOST} / $ 1 / 301,L]

意味着域中的所有索引文件将被重写。这很好,但不在子域。现在我以为我可以简单地为子域添加条件,如:

  RewriteCond%{HTTP_HOST}!^ subdomain\.example \.com $ 
RewriteCond%{HTTP_HOST}!^ www \。

但幸运的是不行。



在这里给出更多的信息是一个URL的样子:

  http://www.example.com/index 
和子域:
http://subdomain.example.com/index

使用上面的代码将是:

  http://www.example.com/ 
和子域:
http://subdomain.example.com/index

如果有一个人可以帮助我。



谢谢alot。



更新:



要提供进一步的信息,我需要解释事情如何工作。



在根目录中有子文件夹。

   - > /index.php 
- > / folderA
- > / subdomain
/ root - > / folderB
- > /index.php

网址如下所示:

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

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

我使用干净的URL,以便它只是 index 而不是 index.php
默认设置在调用页面时已经隐藏了index.php。问题将是当我将更改语言的意思是folderA和folderB。因此,我读出文件的基本名称,并使用标题功能重定向到正确的目录。主要问题是子域名。在领域里,它运作良好。只要我从具有URL的文件夹中找到一个索引页:

  http://subdomain.example.com/folderA/ (索引将被隐藏)

并将读出basename(=> index),并将标题:

  http://subdomain.example.com/folderB/(索引将被隐藏)

会造成问题。网址将以错误的方式重写。或另一个简单的例子:



root / subdomain / folderA /

此按钮只是一个链接:< a href =/ index.php> ...< / a> 。页面网址将是: subdomain.example.com/folderA/filexy 当点击该链接时,网址将被重写为 www.example.com/ subdomain / folderA /

解决方案

请遵守以下规则:

 选项+ FollowSymLinks -MultiViews 
#打开
上的mod_rewrite RewriteEngine On
RewriteBase /

# 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指令 ,然后您可以用以下代码替换上述代码:

  DirectoryIndex index.htm index.html index.php 


I would like to rewrite all index files like /index in my subdomain as well as in my domain. Up to this point there was no need to use a subdomain. Now the problem is a rewrite rule from the htaccess file. This rewrites the URL as in the given code below:

RewriteRule ^index\.(htm|html|php) http://%{HTTP_HOST}/ [R=301,L]
RewriteRule ^(.*)/index\.(htm|html|php) http://%{HTTP_HOST}/$1/ [R=301,L]

Means that in the domain all index files will be rewritten. This works well but not in the subdomain. Now I thought I can simply add a condition for my subdomain like:

RewriteCond %{HTTP_HOST} !^subdomain\.example\.com$
RewriteCond %{HTTP_HOST} !^www\.

but this fortunately does not work.

To give further information here is what a URL looks like:

http://www.example.com/index
and the subdomain:
http://subdomain.example.com/index

With the code above the URL will be:

http://www.example.com/
and the subdomain:
http://subdomain.example.com/index

It would be great if someone could help me out.

Thanks alot.

UPDATE:

To give further information I need to explain how things work.

In the root dir there is the folder for the subdomain.

                                    --> /index.php
                      --> /folderA 
       --> /subdomain               
/root                 --> /folderB
                                    --> /index.php

The URL´s look like that:

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

and

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

I do use clean URL´s so that it is just index and not index.php etc. Default settings already hide index.php when calling a page. The problem will be when I will change the languages what means folderA and folderB. Therefor I read out the basename of the file and use header function to redirect to the right dir. The main problem is the subdomain. In the domain it works well. Just when I have a index page from a folder with the URL:

http://subdomain.example.com/folderA/(index will be hidden)

and will read out the basename (=>index) and will header to:

http://subdomain.example.com/folderB/(index will be hidden)

it will be caused a problem. The URL will be rewritten in a wrong way. Or another simple example:

Having a logo button on all pages in root/subdomain/folderA/

This button is a link with just: <a href="/index.php">...</a>. The page URL will be: subdomain.example.com/folderA/filexy when clicking that link the URL will be rewritten to www.example.com/subdomain/folderA/

解决方案

Have your rules like this:

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: Though I suggest taking a look at DirectoryIndex directive and then you can replace above code with this line:

DirectoryIndex index.htm index.html index.php

这篇关于htaccess重写条件/子域的规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆