301 Redirect And HTACCESS

Discussion in 'PHP' started by MyNet-Tony, Feb 2, 2007.

  1. #1
    What would the code be to have a 301 redirect mydomain.com to www.mydomain.com

    Also I have an HTACCESS setup, would I add the code setup for wordpress permalinks. So would I add this new code in the same <IfModule mod_rewrite.c> </IfModule> or set it up under its own <IfModule mod_rewrite.c> </IfModule>?
     
    MyNet-Tony, Feb 2, 2007 IP
  2. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this:
    <IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase / 
    RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC] 
    RewriteRule ^(.*) http://www.domain.com/$1 [L,R=301] 
    </IfModule> 
    
    Code (markup):
     
    exam, Feb 2, 2007 IP
  3. minstrel

    minstrel Illustrious Member

    Messages:
    15,082
    Likes Received:
    1,243
    Best Answers:
    0
    Trophy Points:
    480
    #3
    <IfModule mod_rewrite.c> 
    </IfModule>
    Code (markup):
    Why do you need these two at all? Don't you know if mod_rewrite is enabled on your server? :confused:
     
    minstrel, Feb 2, 2007 IP
  4. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It can be useful if you're writing a product to be deployed on many servers that you don't have control over. :)
     
    exam, Feb 2, 2007 IP
  5. minstrel

    minstrel Illustrious Member

    Messages:
    15,082
    Likes Received:
    1,243
    Best Answers:
    0
    Trophy Points:
    480
    #5
    OK. It didn't seem like that was what the OP was doing... :)
     
    minstrel, Feb 2, 2007 IP
  6. MyNet-Tony

    MyNet-Tony Guest

    Messages:
    119
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I'm pretty new to PHP and Apache.

    So you dont need <IfModule mod_rewrite.c>
    </IfModule> at all?
     
    MyNet-Tony, Feb 2, 2007 IP
  7. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #7
    If the rewrite rules are only for your server and you know you have mod rewrite enabled, no. It does exactly what it says - anything inside the ifmodule tags will only be read if that particular module is enabled. If you try and rewrite without having mod_rewrite enabled, you'll run into problems so if you're distributing your software it's a good idea to use it. If you're just coding your own project on your own server, it's probably unnecessary.
     
    rodney88, Feb 3, 2007 IP
  8. minstrel

    minstrel Illustrious Member

    Messages:
    15,082
    Likes Received:
    1,243
    Best Answers:
    0
    Trophy Points:
    480
    #8
    It's entirely unnecessary. I've never used it. That's why I asked about its purpose.

    You might need to add one line though:

    Options +FollowSymLinks
    RewriteEngine On 
    RewriteBase / 
    RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC] 
    RewriteRule ^(.*) http://www.domain.com/$1 [L,R=301] 
    Code (markup):
     
    minstrel, Feb 3, 2007 IP
  9. hereyago

    hereyago Active Member

    Messages:
    389
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #9
    yea, thats all you need.
     
    hereyago, Feb 3, 2007 IP
  10. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Not at all - for the sake of two lines, it's simply a best practice to avoid causing internal server errors. Even if it is only for your server, it's normal to code with portability in mind.
     
    rodney88, Feb 3, 2007 IP
  11. sharpweb

    sharpweb Guest

    Messages:
    246
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #11
    hmmmm I tried to do this a whileback and came up with this code:

    RewriteCond %{HTTP_HOST} !^www\..*
    RewriteCond %{HTTP_HOST}   !^$
    RewriteCond %{HTTP_HOST} ^([^.]*)\.(com\.au)
    RewriteRule ^.*$     http://www.%1.%2%{REQUEST_URI} [R=permanent,L]
    Code (markup):
    It works, but maybe it doesn't do a 301. Should I change this part: [R=permanent,L] to: [R=301,L]

    Sorry to hijack the thread, but it's related to the initial question
     
    sharpweb, Feb 3, 2007 IP
  12. minstrel

    minstrel Illustrious Member

    Messages:
    15,082
    Likes Received:
    1,243
    Best Answers:
    0
    Trophy Points:
    480
    #12
    minstrel, Feb 3, 2007 IP
  13. MyNet-Tony

    MyNet-Tony Guest

    Messages:
    119
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Its ok, thanks everyone. I'm new to php/apache and I using Wordpress.
     
    MyNet-Tony, Feb 3, 2007 IP
  14. modulesoft

    modulesoft Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Sir i have a very simple question
    i have a website
    and its in php
    i have use the redirect engine to show extension as html insteade of php


    but in my site all links and stuff r in php
    and its now hard to chaneg each link to html

    so can u let meknow how to do that in htaccess here is my htaccess code


    Options +FollowSymLinks

    RewriteEngine On

    RewriteRule ^([^.]+).html$ /$1.php [QSA,L]



    its work fine
    if some one type my sitenae.com/index.php works
    and mysitename.com/index.html works fine

    but i want when a type .php so it iwll automatically redirect to respective html file

    please help me asap
     
    modulesoft, Feb 7, 2007 IP
  15. sharpweb

    sharpweb Guest

    Messages:
    246
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Thanks...checks out fine
     
    sharpweb, Feb 7, 2007 IP