I have a small htaccess file, which contains some simple rules.
First I want to redirect or to add www at the begin of the main domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|serv)\.(.*\-.*)\.com [NC]
RewriteRule ^(.*) http://www.maindomain.com%{REQUEST_URI} [L,R=301]
In the second step I want to remove the folder serv from the URI.
I have following folder structure:
- serv/
- content of the main web page
To do that I have following rules:
RewriteCond %{HTTP_HOST} ^serv.* [NC]
RewriteCond %{REQUEST_URI} !^/serv.* [NC]
RewriteRule ^(.*) /serv/$1 [L,QSA]
This works so far, but my own rewrite rules conflicts with the conditions and rules above:
RewriteEngine On
RewriteBase /
RewriteRule ^search/$ search.php [L,QSA]
So my URI get sometimes something like that http://www.maindomain.de/search/search.
Here is my full htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|serv)\.(.*\-.*)\.com [NC]
RewriteRule ^(.*) http://www.maindomain.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^serv.* [NC]
RewriteCond %{REQUEST_URI} !^/serv.* [NC]
RewriteRule ^(.*) /serv/$1 [L,QSA]
RewriteEngine On
RewriteBase /
RewriteRule ^search/$ search.php [L,QSA]
What is wrong? I don’t find the mistake.
Furthermore I want to know if it is possible, if the subdomain isn’t serv and the URI doesn’t contain serv that the subdomain will be redirected to www.
For the last part of your question, add the following to your .htaccess file
Edit:
I replaced %1 reference with the host Above, which you can change to match your actual host.
For the first part of your question, your rule below will match any host that does not begin with www. or serv. that does not have a dash in the domain i.e. it will match
www.maindomain.combecause it does not have a ‘-`.This will result in an infinite redirect as you observed.
To fix it change it to the following