mod_rewrite (redirect and etc)

Discussion in 'Apache Migration/Compatibility' started by career, Apr 26, 2009.

  1. career

    career New Member

    Good day

    I've installed v.4.0.1.

    Firstly, I need to redirect all links www.domain.com to domain.com.
    Also I really want to rewrite links with ip to links with domain name instead ip.

    -------
    Redirect in .htaccess:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^yoursite\.com
    RewriteRule ^(.*)$ http://yoursite.com/$1 [R=permanent,L] .

    or like this:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
    RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
    -------

    Both code not working. Seems it causes a cyclic redirect.
  2. auser

    auser Super Moderator

    #redirect all links www.domain.com to domain.com
    #Redirect in .htaccess:
    RewriteEngine on
    RewriteRule ^$ http://domain.com

    #rewrite links with ip to links with domain name instead ip
    RewriteCond %{HTTP_HOST} ^11.22.33.44$
    RewriteRule ^(.*)$ http://domain.com/$1 [R=301]

    not sure if it actually works?
  3. career

    career New Member

    test that before - infinite redirect )

    Why this expression doesn't work?
    RedirectMatch (.*) http://domain.com$1


    it's a good solution
  4. career

    career New Member

  5. career

    career New Member

    Thanks auser but I actually need not to redirect something. I just need to rewrite in all pages Ip to domain name.
  6. auser

    auser Super Moderator

    Last edited: Apr 27, 2009
  7. career

    career New Member

    Auser, it's not acceptable in my case:
    http://11.22.33.44/folder/x.php?x=1&y=2 --> http://domain.com/folder/x.php?x=1&y=2
  8. mistwang

    mistwang LiteSpeed Staff

    to redirect from www.domain.com to domain.com, you have to match the www domain name.

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www.domain\.com$ [NC]
    RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

    or

    RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
    RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

    it will take care of IP->domain.com as well.
  9. career

    career New Member

    Tnx mistwang, now everything is working properly.


    But can you specify why this my rule causes infinite redirect?
    RewriteEngine On
    RewriteRule ^([^?]*)(.*)$ http://domain.com/$1$2
  10. mistwang

    mistwang LiteSpeed Staff

    It cause infinite redirections because it rewrite the url when "domain.com" is used, so the result URL is the same as the original.
  11. career

    career New Member

    Tnx mistwang

Share This Page