Results 1 to 6 of 6
  1. #1
    TravG's Avatar
    TravG is offline Private Member
    Join Date
    September 2008
    Posts
    2,069
    Thanks
    34
    Thanked 179 Times in 131 Posts

    Default Proper htaccess file

    You see so many different articles out there about how to properly redirect all the things you need to in your htaccess file that I am hoping someone will share here "all in one place" what all the different code should be. I have a feeling my htaccess file may not be correct, and unfortunately many of my different websites have different htaccess files which have been built over the years.

    So what exactly should your htaccess file look like if you have an ssl and you want to direct everything to the www version of your pages? I would like to force everything to go the https, www version of my pages and also make sure all the /index files are redirected to the non-index version.

    Thanks!
    Live Casino USA - the best USA live online casinos. Play USA online slots and other casino games like USA online blackjack. Play See USAlegalcasinos.com to find the best USA online casino. Want to play USA online poker? Find the best poker sites at 4DeucesPoker.com.

  2. #2
    Upotter's Avatar
    Upotter is offline Brand New Member
    Join Date
    June 2020
    Location
    Tokyo
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Depends on a website (for example, for some project you may need to disallow traffic from certain countries using 403)

  3. #3
    tufty is offline Public Member
    Join Date
    November 2016
    Posts
    526
    Thanks
    187
    Thanked 346 Times in 211 Posts

    Default

    To redirect your website from non-www to www, add the following lines in your website’s .htaccess file (change "yourdomain" to your domain):
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
    RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]


    To force the HTTPS connection on your website, add the following lines inside the website’s .htaccess file:
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    If htaccess ever crashes your site, go in via FTP and edit it.
    Last edited by tufty; 25 June 2020 at 9:00 am.

  4. The Following 2 Users Say Thank You to tufty For This Useful Post:

    fullbenefit (25 June 2020), TravG (25 June 2020)

  5. #4
    TravG's Avatar
    TravG is offline Private Member
    Join Date
    September 2008
    Posts
    2,069
    Thanks
    34
    Thanked 179 Times in 131 Posts

    Default

    Thanks. How about making the index page "disappear"? Example, xx.www.abc.com/index.html goes to xx.www.abc.com And doing that for all the pages in the website.
    Live Casino USA - the best USA live online casinos. Play USA online slots and other casino games like USA online blackjack. Play See USAlegalcasinos.com to find the best USA online casino. Want to play USA online poker? Find the best poker sites at 4DeucesPoker.com.

  6. #5
    tufty is offline Public Member
    Join Date
    November 2016
    Posts
    526
    Thanks
    187
    Thanked 346 Times in 211 Posts

    Default

    I can't advise about the index page in case that has consequences as I am not technical. What I suggest below should work, but I am not the one to advise on best practice for using this method to redirect an index page. Maybe someone can comment on that specifically. But to redirect any other pages because URL structures change you would place this in htaccess, for example:

    redirect 301 /privacypolicy.htm https://www.yourdomainhere/privacy-policy/

    In that example, the "/PrivacyPolicy.htm" is where my privacy page was and what it was called and the "https://www.yourdomainhere.com/privacy-policy/" is where it is now. It is correct to leave out the domain part of the first URL and include it in the second one.

    I have a very long list of redirects like that placed in my htaccess files as my websites were all non-wordpress .html pages that went wordpress, so I have lots of redirects from the original URL to its new equivalent.
    Last edited by tufty; 25 June 2020 at 8:10 pm.

  7. The Following User Says Thank You to tufty For This Useful Post:

    fullbenefit (25 June 2020)

  8. #6
    ddm
    ddm is offline Former Member
    Join Date
    July 2006
    Posts
    1,118
    Thanks
    418
    Thanked 470 Times in 287 Posts

    Default

    # BEGIN EXPIRES
    <IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 10 days"
    ExpiresByType text/css "access plus 1 week"
    ExpiresByType text/plain "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 week"
    ExpiresByType application/x-icon "access plus 1 year"
    </IfModule>
    # END EXPIRES
    Compress Content Using Mod Deflate
    # COMPRESSION
    # BEGIN DEFLATE COMPRESSION
    <IfModule mod_deflate.c>
    # Compress HTML, CSS, JavaScript, Text, XML and fonts
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
    AddOutputFilterByType DEFLATE application/x-font
    AddOutputFilterByType DEFLATE application/x-font-opentype
    AddOutputFilterByType DEFLATE application/x-font-otf
    AddOutputFilterByType DEFLATE application/x-font-truetype
    AddOutputFilterByType DEFLATE application/x-font-ttf
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE font/opentype
    AddOutputFilterByType DEFLATE font/otf
    AddOutputFilterByType DEFLATE font/ttf
    AddOutputFilterByType DEFLATE image/svg+xml
    AddOutputFilterByType DEFLATE image/x-icon
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/xml
    </IfModule>
    # END DEFLATE COMPRESSION
    # END COMPRESSION
    Gzip Compression
    # BEGIN GZIP
    # BEGIN GZIP COMPRESSION
    <IfModule mod_gzip.c>
    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
    mod_gzip_item_include handler ^cgi-script$
    mod_gzip_item_include mime ^text/.*
    mod_gzip_item_include mime ^application/x-javascript.*
    mod_gzip_item_exclude mime ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
    </IfModule>
    # END GZIP COMPRESSION
    # END GZIP
    Caching
    #CACHE CONTROL
    # BEGIN Cache-Control Headers
    <ifModule mod_headers.c>
    <filesMatch ".(ico|jpe?g|png|gif|swf)$">
    Header set Cache-Control "public"
    </filesMatch>
    <filesMatch ".(css)$">
    Header set Cache-Control "public"
    </filesMatch>
    <filesMatch ".(js)$">
    Header set Cache-Control "private"
    </filesMatch>
    <filesMatch ".(x?html?|php)$">
    Header set Cache-Control "private, must-revalidate"
    </filesMatch>
    </ifModule>
    # END Cache-Control Headers
    #END CACHE CONTROL
    Force Trailing Slash
    # FORCE TRAILING SLASH
    <IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_URI} /+[^.]+$
    RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
    </IfModule>
    # END FORCE TRAILING SLASH
    Prevent Hotlinking
    # PREVENT HOTLINKING
    RewriteEngine On
    #Replace ?yourdomainname.com/ with your website url
    RewriteCond %{HTTP_REFERER} !^http://(.+.)?yourdomainname.com/ [NC]
    RewriteCond %{HTTP_REFERER} !^$
    #Replace /images/nohotlink.jpg with your "hotlinking is not permitted" image url
    RewriteRule .*.(jpe?g|gif|bmp|png)$ /hotlinksnotpermitted.jpg [L]
    # END PREVENT HOTLINKING


    Redirect Website Visitors To A Mobile Site:
    # REDIRECT MOBILE DEVICES
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/m/.*$
    RewriteCond %{HTTP_ACCEPT} "text/vnd.wap.wml|application/vnd.wap.xhtml+xml" [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac| blaz|brew|cell|cldc|cmd-" [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji |leno|lg-c|lg-d|lg-g|lge-" [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek |qwap|sage|sams|sany" [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|m mp" [NC,OR]
    RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC]
    #------------- The line below excludes the iPad
    RewriteCond %{HTTP_USER_AGENT} !^.*iPad.*$
    #-------------
    RewriteCond %{HTTP_USER_AGENT} !macintosh [NC] #*SEE NOTE BELOW
    RewriteRule ^(.*)$ /m/ [L,R=302]
    # END REDIRECT MOBILE DEVICES
    Stop SPAM On WordPress Blog
    # STOP SPAM ON WORDPRESS BLOG
    RewriteEngine On
    RewriteCond %{REQUEST_METHOD} POST
    RewriteCond %{REQUEST_URI} .wp-comments-post.php*
    RewriteCond %{HTTP_REFERER} !.*yourdomainname.* [OR]
    RewriteCond %{HTTP_USER_AGENT} ^$
    RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]
    </IfModule>
    # END STOP SPAM ON WORDPRESS BLOG
    Replace yourdomainname with your actual domain name URL


    Redirect Feeds
    # REDIRECT DIFFERENT FEEDS TO A SINGLE FORMAT
    <IfModule mod_alias.c>
    RedirectMatch 301 /feed/(atom|rdf|rss|rss2)/?$ http://yourdomainname.com/feed/
    RedirectMatch 301 /comments/feed/(atom|rdf|rss|rss2)/?$ http://yourdomainname.com/comments/feed/
    </IfModule>
    # END REDIRECT DIFFERENT FEEDS TO A SINGLE FORMAT
    Replace yourdomainname with your actual domain name URL


    Configure Your Website For HTML5 Videos
    # CONFIGURE YOUR WEBSITE FOR HTML5 VIDEOS
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !=/favicon.ico
    AddType video/ogg .ogv
    AddType video/ogg .ogg
    AddType video/mp4 .mp4
    AddType video/webm .webm
    AddType application/x-shockwave-flash swf
    # END CONFIGURE YOUR WEBSITE FOR HTML5 VIDEOS
    Log PHP Errors Into An Error Log
    # LOG PHP errors INTO ~/public_html/php_error.log
    # display no errs to user
    php_flag display_startup_errors off
    php_flag display_errors off
    php_flag html_errors off
    # log to file
    php_flag log_errors on
    php_value error_log ~/public_html/php_error.log
    # END LOG PHP errors INTO ~/public_html/php_error.log
    Replace yourdomainname with your actual domain name URL and ~/public_html/php_error.log with the location of your log file


    Run PHP Inside JavaScript Files
    # RUN PHP INSIDE JAVASCRIPT FILES
    AddType application/x-httpd-php .js
    AddHandler x-httpd-php5 .js
    <FilesMatch ".(js|php)$">
    SetHandler application/x-httpd-php
    </FilesMatch>
    # RUN PHP INSIDE JAVASCRIPT FILES
    Block User IPs
    #BLOCKING USERS IP
    order allow,deny
    deny from 123.45.6.7
    deny from 0.1.2.3
    allow from all
    #END BLOCKING USERS IP
    Replace 123.45.6.7 and 0.1.2.3 with the actual IP addresses you want to block


    Prevent Directory Listing Of Files
    #PREVENT DIRECTORY LISTING
    IndexIgnore *
    #END PREVENT DIRECTORY LISTING
    Custom Error Document
    # ERROR DOCUMENT
    ErrorDocument code /directory/filename.ext
    ErrorDocument 404 /errors/lostandfound.html
    # END ERROR DOCUMENT
    Replace /directory/filename.ext with the error document you would like to use and /errors/lostandfound.html with the 404 error page you would like to use to replace the default error documents


    Redirect Non WWW URL To WWW URL
    # REDIRECT NON WWW URL TO WWW URL
    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^yourdomainname.com
    RewriteRule (.*) https://www.yourdomainname.com/$1 [R=301,L]
    # END REDIRECT NON WWW URL TO WWW URL
    Replace yourdomainname in three places, /olddirectory/oldfile.html with the old file name and newfile.html with the new file name


    Simple Redirect
    # SIMPLE REDIRECT
    Redirect /olddirectory/oldfile.html https://yourdomainname.com/newfile.html
    # END SIMPLE REDIRECT
    Replace /olddirectory/oldfile.html and https://yourdomainname.com/newfile.html


    Redirect Website To Https
    #Force SSL without www
    RewriteEngine On
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://yourdomain.tld/$1 [R,L]
    #Force SSL with www
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.yourdomain.tld/$1 [R,L]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •