.htaccess quick question

Discussion in 'Apache' started by Leo727, May 17, 2008.

  1. #1
    Hi,

    I currently have code to add www before the URL if it's missing, and to add a trailing slash if it's missing, and the code is working fine. However, I would like make this code work on any domain without needing to change the code at all - the reason is because I will be automatically copying this .htaccess file across multiple domains, so I want to remove all references to a specific domain and have it automatically work on any domain. Here is the current code :

    RewriteCond %{HTTP_HOST} ^DOMAIN1\.com [NC]
    RewriteRule ^(.*)$ http://www.DOMAIN1.com/$1 [L,R=301]
    
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ http://www.DOMAIN1.com/$1/ [L,R=301]
    Code (markup):
    So to put it simply, I want to remove the DOMAIN1.com from the above code so that it still works the same but doesn't need to be changed for specific domains. Is this possible?

    Thanks!
    Leo
     
    Leo727, May 17, 2008 IP
  2. Leo727

    Leo727 Active Member

    Messages:
    161
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Does anybody have even an idea to point me in the right direction?
     
    Leo727, May 21, 2008 IP
  3. Leo727

    Leo727 Active Member

    Messages:
    161
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    60
    #3
    I seem to have it working now, in case anyone is interested here is the final code which apparently works as intended :

    #  add www if the current URL doesn't have it
    RewriteCond %{HTTP_HOST} ^(.*)/$ [NC]
    RewriteRule ^(.*)$ %1/$1 [L,R=301,NC]
    
    #  add trailing slash if the current URL doesn't have it
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ %1/$1/ [L,R=301,NC]
    Code (markup):
     
    Leo727, May 21, 2008 IP
  4. Leo727

    Leo727 Active Member

    Messages:
    161
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    60
    #4
    I've found the above code doesn't always work, here's a better version thanks to AskApache.com

    #  add www if the current URL doesn't have it
    RewriteCond %{HTTP_HOST} !^(www.|$) [NC]
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    # add trailing slashes if missing
    RewriteRule ^/*(.+/)?([^.]*[^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
    Code (markup):
    You'll also need to have the following lines in your .htaccess if they're not already there :

    Options +FollowSymLinks
    RewriteEngine On

    For more information and variations see : http://www.askapache.com/htaccess/commonly-used-htaccess-code-examples.html

    Hope this helps.
     
    Leo727, Jul 2, 2008 IP