且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

htaccess重写条件/子域的规则

更新时间:2023-02-22 08:06:58

请遵守以下规则:

 选项+ 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