简体   繁体   中英

htaccess rewriting condition/rule for subdomains

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

Try this:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM