Forum Moderators: phranque

Message Too Old, No Replies

.htaccess browser redirection very very slow

Redirection depending on HTTP_USER_AGENT seems to be very slow

         

jimmy74

9:44 pm on Jan 21, 2008 (gmt 0)

10+ Year Member



Hi,

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

jdMorgan

10:07 pm on Jan 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your opera redirect will loop until the browser or server reaches its redirection limit.

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]

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

jimmy74

11:29 am on Jan 22, 2008 (gmt 0)

10+ Year Member



Hello 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

jdMorgan

1:41 pm on Jan 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It may need a "loop stopper" just like the first rule:

# 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 get this working, it offers two features not supported by your original code:
  • Supports requests for "www.subdomain.example.com" (some people think they always have to type "www" in front of the domain/subdomain.)
  • Will only rewrite if the index_xyz.html file exists for the requested subdomain. Otherwise, allows a 404 response showing the originally-requested URL, and so does not 'expose' the inner workings of your sites.

    Jim
  • jimmy74

    5:13 pm on Jan 22, 2008 (gmt 0)

    10+ Year Member



    Hello Jim,

    No luck with the new code for me, maybe it depends on the server configuration. Most importantly I could set up a .htaccess code, so the system is working.

    Again, you are great.

    Thank you,

    jimmy74


    phranque

    4:57 am on Jan 23, 2008 (gmt 0)

    WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



    welcome to WebmasterWorld [webmasterworld.com], jimmy74!

    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

    jimmy74

    8:10 pm on Jan 23, 2008 (gmt 0)

    10+ Year Member



    Hello phranque,

    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

    jimmy74

    8:25 pm on Jan 26, 2008 (gmt 0)

    10+ Year Member



    Hello guys,

    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

    jimmy74

    8:31 pm on Jan 26, 2008 (gmt 0)

    10+ Year Member



    Oops, correction:

    The first part is for Opera browsers to redirect the <subdomain>.example.com to opera/index_<subdomain>.html.

    The second part is almost the same but other browsers than Opera to redirect the <subdomain>.example.com to /index_<subdomain>.html.

    Something like that.

    jimmy74

    jdMorgan

    4:50 pm on Jan 28, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    The best solution is to 301 redirect all requests for example.com to www.example.com. This both avoids duplicate-content 'penalties' and fixes the problem of redirecting opera to the index page.

    Try a search here for "www non-www redirect" to get started.

    Jim

    jimmy74

    7:43 pm on Jan 28, 2008 (gmt 0)

    10+ Year Member



    Hi jdMorgan,

    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

    jdMorgan

    10:59 pm on Jan 28, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Code it like you wrote it... Fully-specifying the requirements is more than half the work!

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

    Note that you've asked for external redirects, which is not the usual approach, since external redirects will 'expose' the 'inner workings' of your user-agent-to-page mapping, and will require the client to re-request the home page using the new URL provided in the redirect response. In most cases, Webmasters use an internal rewrite to prevent this exposure, and to prevent the delay associated with redirecting the client.

    Also note that these rules redirect the "home page" URLs only, since you did not specify otherwise.

    Pray for no typos...

    Jim

    jimmy74

    7:33 pm on Jan 30, 2008 (gmt 0)

    10+ Year Member



    I have extended the code with my additional requirements I realized later, it works very well, mission accomplished. I use external rewrites (the most important thing that at least this version works), probably I try to modify them to internal rewrites in the future. Thank you for help jdMorgan, as I said, you are great!