Forum Moderators: phranque
I have the following code in my .htaccess file (Apache webserver, managed hosting):
RewriteEngine On
RewriteCond %{HTTP_HOST}!^www\\.mydomain\\.com
RewriteCond %{HTTP_HOST} ([^.]+)\\.mydomain\\.com [NC]
RewriteRule ^(.*)$ [mydomain.com...] [L,R]
This code basically redirects the subdomains of mydomain.com website to the corresponding index page (for example de.mydomain.com goes to mydomain.com/index_de.html)
This works fine and immediately, but I also wanted to set up a redirection for a specific browser, in this case, Opera browser.
I added two more lines:
RewriteCond %{HTTP_USER_AGENT} \"Opera\"
RewriteRule ^(.*)$ /opera/index.html [L,R=301]
So in case of Opera browser I would like to redirect the user to [mydomain.com...] The additional code works, however the Opera specific redirection is not immediate, it usually takes 3 or 4 minutes with blank screen and later I get the desired index.html from mydomain.com Opera directory.
So the important thing is that I really wonder why I should wait for such a long time for the Opera specific redirection, do you have any idea?
Or do you have any alternative solution for the Opera specific redirection?
Thanks,
jimmy74
Your "subdomain redirect" will 'expose' the internal path to the actual filepath -- not something I'd recommend.
For some reason, duplicate backslashes appear in the RewriteCond regex patterns in your post. The patterns shown below have been corrected.
RewriteEngine on
#
# Externally redirect opera requests
RewriteCond %{HTTP_USER_AGENT} Opera
RewriteRule !^opera/index\.html$ http://www.example.com/opera/index.html [R=301,L]
#
# Internally rewrite subdomain requests to index_<subdomain>.html (if it exists)
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteCond %{DOCUMENT_ROOT}index_%2.html$ -f
RewriteRule .* /index_%2.html [L]
Jim
Based on your guidelines I modified the .htaccess file and it's working, you are great!
I tried the code part after "# Internally rewrite subdomain requests to index_<subdomain>.html (if it exists)" and it worked for the main domain (like example.com), but somehow got a redirection error like in case of de.example.com. I don't think it's a problem, because the code for this functionality works what is in the first post. So I think the problem is solved, but if you have time you can comment the "# Internally rewrite subdomain requests to index_<subdomain>.html (if it exists)" and what was in the first post for it.
Thank you for your help,
jimmy74
# Internally rewrite subdomain requests to index_<subdomain>.html (if it exists)
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteCond %{DOCUMENT_ROOT}index_%2.html$ -f
RewriteRule !^index_[^.]+\.html$ /index_%2.html [L]
if you can turn up rewrite logging [httpd.apache.org] that may help figure out if/what is happening in mod_rewrite.
(you need config file access and apache restart for this.)
otherwise, it might be that your DocumentRoot directive doesn't specify the trailing slash, so you could try this:
RewriteCond %{DOCUMENT_ROOT}/index_%2.html$ -f
Unfortunately I have a managed hosting account, I don't have the possibility for reaching the config file access and apache restart.
No luck for me with RewriteCond %{DOCUMENT_ROOT}/index_%2.html$ -f
But there is no problem, because similar to the first post, this code (which is probably not too nice) works for me:
RewriteEngine On
RewriteCond %{HTTP_HOST}!^www\.example\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/index_%1.html [L,R]
You are really helpful (as well as jdMorgan), thank you very much.
jimmy74
I am afraid I need your help again, sorry. I have the following code:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} Opera
RewriteCond %{HTTP_HOST}!^www\.example\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com [NC]
RewriteRule!^opera/index\.html$ http://www.example.com/opera/index_%1.html [R=301,L]
RewriteCond %{HTTP_HOST}!^www\.example\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/index_%1.html [L,R]
The first part is for Opera browsers to redirect the <subdomain>.example.com to opera/index_<subdomain>.html.
The second part is the same but other browsers than Opera.
Now the problem is the Opera part. In case of example.com I would like to redirect it to opera/index.html. I tried several things, no luck.
Please help me again.
Thank you,
jimmy74
I have created a new .htaccess file (with RewriteEngine On), but it's still not perfect:
RewriteCond %{HTTP_USER_AGENT} Opera
RewriteCond %{HTTP_HOST}!^www\.example\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com [NC]
RewriteRule!^opera/index\.html$ http://www.example.com/opera/index_%1.html [R=301,L]
RewriteCond %{HTTP_USER_AGENT} Opera
RewriteCond %{HTTP_HOST} .
RewriteRule!^opera/index\.html$ http://www.example.com/opera/index.html [R=301,L]
RewriteCond %{HTTP_HOST}!^www\.example\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/index_%1.html [L,R]
Formerly I used the last part starting with RewriteCond %{HTTP_HOST}!^www\.example\.com. The goal was to redirect <subdomain>.example.com to example.com\index_<subdomain>.html. In case of example.com it goes to example.com\index.html. No matter if I entered www. or not in the address bar, it worked probably because of the server configuration.
Later I added the first part to redirect the Opera browsers (based on your help). Here the goal was to redirect <subdomain>.example.com to example.com\opera\index_<subdomain>.html. If I use the first part and the last part, the previous two goals can be reached, however in case of example.com it goes to example.com\index.html which is not good for me: in case of Opera I need example.com to be redirected to example.com/opera/index.html.
So I added the middle part of the code, which works, but dominates the Opera redirection, so in case of <subdomain>.example.com or example.com it always goes to example.com/opera/index.html. So the first part seems to be blocked, but I need the functionality of the first part to redirect <subdomain>.example.com to example.com\opera\index_<subdomain>.html.
Here is the summary what I would like to achieve:
For Opera browser:
<subdomain>.example.com goes to example.com\opera\index_<subdomain>.html
example.com goes to example.com\opera\index.html
For browsers other than Opera:
<subdomain>.example.com goes to example.com\index_<subdomain>.html
example.com goes to example.com\index.html
Can you please help me?
Thank you,
jimmy74
# For Opera browser:
# <subdomain>.example.com/ goes to www.example.com/opera/index_<subdomain>.html
RewriteCond %{HTTP_USER_AGENT} ^Opera/
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^$ http://www.example.com/opera/index_%1.html [R=301,L]
#
# example.com/ and www.example.com/ go to www.example.com/opera/index.html
RewriteCond %{HTTP_USER_AGENT} ^Opera/
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^$ http://www.example.com/opera/index.html [R=301,L]
#
#
# For browsers other than Opera:
# <subdomain>.example.com/ goes to www.example.com\index_<subdomain>.html
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^$ http://www.example.com/index_%1.html [R=301,L]
#
# example.com/ and www.example.com/ go to www.example.com\index.html
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^$ http://www.example.com/index.html [R=301,L]
Also note that these rules redirect the "home page" URLs only, since you did not specify otherwise.
Pray for no typos...
Jim