$20 for htaccess: Force WWW, add Trailing Slash...

Discussion in 'Apache' started by abi, Apr 24, 2010.

  1. #1
    Hello,

    I've tried to do this - i carnt, so if someone can do it -- i'll paypal them $20. :p

    I need the following....

    • Force http://site.com to http://www.site.com (Add www)
    • Make http://www.site.com/page into http://www.site.com/page/ (Trailing Slash)
    • Work with ANY domain - So no mention of actual domain or tld in htaccess. Can be dragged into any site, without editing the htaccess file.

    And finally, every request should actually be redirected to the index as variables if the real page does not exist.

    Example:
    http://www.site .com/page/ <--- is actually showing site.com/index.php?a=page
    http://www.site.com/page/3 <--- is actually showing site.com/index.php?a=page&b=3

    Only 2 variables are needed. - some requests may only have 1, some may have 2. If a user tries to load a real page, the real page should be shown, not the redirected to index page.

    The file i wrote worked perfect -- but i've been told that it does unnecessary redirects. So i'm really looking for a highly optimized htaccess file, that only does the redirects it needs to.

    $20 goes to the solution others agree is the best way to do it. If theres only one solution - they get it, if multiple solutions but no one says which is best -- its the first person who posted it. :rolleyes:

    Thanks for reading! I hope someone can do the above -- as it's been a nightmare trying to do it. I hate htaccess files lol.
     
    abi, Apr 24, 2010 IP
  2. mariusv

    mariusv Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you have mod_rewrite module in apache is very simple to do that..Give me more detaile on PM about your apache/server configuration and i can help you fast
     
    mariusv, Apr 24, 2010 IP
  3. foxyproxy

    foxyproxy Peon

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    is this been solved?
    where's your site?(just pm me for th url if you wnat hide the site from public.,;if you still need a help)
     
    foxyproxy, Apr 25, 2010 IP
  4. abi

    abi Peon

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the replies guys. kishor213 is the only one to PM me a solution so far - but it includes "YourSite.com" style rewrite rules -- where the htaccess file would need to be edited each differernt site it is used on. I really need it to pick up the domain from the server itself. (Unless someone can say a reason why this isnt a good idea)

    Let me post the htaccess file i wrote... I have been told that it is bad, because it does too many redirects. So im looking for improvements.

    
    Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /
    
    # ADD / FORWARD SLASH
    RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$ 
    RewriteRule ^(.*)$ [%{HTTP_HOST}...] [R=301,L] 
    
    # ADD WWW
    RewriteCond %{HTTP_HOST} !^www\. 
    RewriteRule ^(.*)$ [%{HTTP_HOST}...] [R=301,L]
    
    # REDIRECT TO INDEX EVERY TIME
    RewriteRule ^([0-9a-zA-Z-]+)/?(([0-9a-zA-Z-]+)/)?$ /index.php?p=$1&l=$3 [L]
    
    Code (markup):

    Remember, it's the first person to post a htaccess file in this THREAD that others agree is the best way to do it that gets the $20. :)
    Or if only one reply - that person. As long as it's better then my htaccess attempt ;)

    Thanks again guys!
     
    abi, Apr 25, 2010 IP
  5. kishor212

    kishor212 Guest

    Messages:
    124
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    RewriteEngine on

    Options FollowSymlinks

    rewritecond %{http_host} ^Yoursite.com [nc]

    rewriterule ^(.*)$ http://www.yoursite.com/$1 [r=301,nc]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
    RewriteRule (.*)$ http://yoursite.com/$1/ [R=301,L]

    Not fit for $20 since it need the site url..!

    But hope i can get 10 or $15

    well it deserves i think..!
     
    kishor212, Apr 25, 2010 IP
  6. kishor212

    kishor212 Guest

    Messages:
    124
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Well.. Your Script too looks fine.... And will work fine i hope..!

    I cant find any Thing wrong there..!
     
    kishor212, Apr 25, 2010 IP
  7. abi

    abi Peon

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Still looking for help guys!

    It must be an easy one for you Apache/htaccess experts :)

    Nobody want $20? lol.

    Cheers.
     
    abi, Apr 28, 2010 IP
  8. mariusv

    mariusv Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    LoL @abi give me an PM with your server conf and i will help you if you really need help
     
    mariusv, Apr 30, 2010 IP
  9. abi

    abi Peon

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    This is sorted now guys - cheers!

    If a mod could lock or delete it :)
     
    abi, May 7, 2010 IP