SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    .htaccess redirect

    I want to make all urls go to:

    https://www.domain.com/page/var1/var2/ etc. (with all to https and all with trailing slash).

    Also, I want index.php to receive all requests, despite the url being /page/variable/variable2 etc.

    I have this, but none of it works. I must be doing something wrong.

    Code:
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTP_HOST} ^domain.com  
    RewriteRule (.*) http://www.domain.com/$1 [R=301,L] 
    	
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L]	
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L,NS]

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,069
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Hey there,

    First things first, you can most likely get rid of the RewriteBase /. Some hosts require it, but generally it is only used to undo the effects of mod_alias, and since you don't seem to be using that ...

    Okay, if you want everything redirected to https, you should get rid of the first block that redirects to http, that's just doing work for nothing. Instead build redirection to www in the second rule so it fires if either there request is not for https or the request is for the www-less version of the domain. Also %{HTTPS} is either "on" or no value at all, so matching against "off" just won't work because it'll never take on that value (odd though that may seem given that "on" is a valid value ...)

    So

    Code:
    RewriteCond %{HTTPS} !=on [OR]
    RewriteCond %{HTTP_HOST} ^domain\.com$
    RewriteRule ^ https://www.domain.com%{REQUEST_URI} [L]
    The second part of your code to let index.php serve everything should work as intended, although /index.php should be replaced with index.php (remove the leading slash) for performance reasons, and some might say . should be replaced with .? but since you index file probably is index.php this is not strictly necessary.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Aw, Rémon, you know better! "RewriteCond %{HTTPS} off" will NEVER evaluate to TRUE (because {HTTPS} is either 'on' or null - that's why I prefer to look at the {SERVER_PORT} and match to ^80$ or ^443$).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,069
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by dklynn View Post
    Aw, Rémon, you know better! "RewriteCond %{HTTPS} off" will NEVER evaluate to TRUE (because {HTTPS} is either 'on' or null - that's why I prefer to look at the {SERVER_PORT} and match to ^80$ or ^443$).
    I know, that's why I said:

    Quote Originally Posted by ScallioXTX View Post
    Also %{HTTPS} is either "on" or no value at all, so matching against "off" just won't work because it'll never take on that value (odd though that may seem given that "on" is a valid value ...)
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Rémon,

    Sheech! I missed that!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

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