SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Enthusiast Dreeass's Avatar
    Join Date
    Sep 2012
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Multiple rewrite conditions and rules

    This is the first that that I worked with the htaccess file because I wanted to keep the easy way of GET variables and make the url nice. So I have the following code in my .htaccess file.

    So the following URL: http://www.rsteams.com/act/test should 'direct' to http://rsteams.com/index.php?act=test

    When not using the second condition which does the GET stuff, it works (remove the www). What am I doing wrong?

    Code:
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www.rsteams.com [NC]
    RewriteRule ^(.*)$ http://rsteams.com/$1 [L,R=301]
    
    RewriteCond %{REQUEST_URI} act/(.*)/
    RewriteRule ^act/(.*)/$ http://rsteams.com/index.php?act=$2
    
    AuthGroupFile /dev/null
    AuthType Basic
    AuthUserFile /home/*CENSORED*/domains/rsteams.com/.htpasswd/public_html/.htpasswd
    AuthName "Administrators' Area"
    require valid-user
    
    <FilesMatch "\.php$">
    
    AddHandler x-httpd-php53 .php
    
    </FilesMatch>
    The FilesMatch I had to setup to use PHP 5.3 on my webhost that I rent.

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,605
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    Dreeass,

    You're using the LAZY code to capture the entire {REQUEST_URI} and not the parts. How do you expect (.*) to be translated to act= ... well, not test but act/test? Moreover, WHY the trailing / in your RewriteRule? Then, too, what do you think RewriteBase / is doing? The second RewriteCond statement?

    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

  3. #3
    SitePoint Enthusiast Dreeass's Avatar
    Join Date
    Sep 2012
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    Dreeass,

    You're using the LAZY code to capture the entire {REQUEST_URI} and not the parts. How do you expect (.*) to be translated to act= ... well, not test but act/test? Moreover, WHY the trailing / in your RewriteRule? Then, too, what do you think RewriteBase / is doing? The second RewriteCond statement?

    Regards,

    DK
    I'm just following a tutorial from YouTube and some stuff I read about this, there's no need to be angry. Well explain to me in normal terms what I'm doing wrong and if you'd be so kind, please show it to me in code so I understand. But don't just give me code if you want, I'd like to understand it too so I can use it in the future.

  4. #4
    SitePoint Enthusiast Dreeass's Avatar
    Join Date
    Sep 2012
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    Dreeass,

    You're using the LAZY code to capture the entire {REQUEST_URI} and not the parts. How do you expect (.*) to be translated to act= ... well, not test but act/test? Moreover, WHY the trailing / in your RewriteRule? Then, too, what do you think RewriteBase / is doing? The second RewriteCond statement?

    Regards,

    DK
    Also the RewriteBase and the part to remove www wasn't written by me but by someone from the webhosting service.

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,904
    Mentioned
    139 Post(s)
    Tagged
    2 Thread(s)
    Code:
    RewriteEngine On
    This is fine. Without this the rest wouldn't work.

    Code:
    RewriteBase /
    Everybody and their aunt puts this in the their .htaccess while in 99 out of 100 cases it's not needed at all. The only time I recall I've ever needed it is on a host with mass virtual hosting, but I don't think many hosts in the wild use this.
    Most likely you can remove this without any consequences.

    Code:
    RewriteCond %{HTTP_HOST} ^www.rsteams.com [NC]
    RewriteRule ^(.*)$ http://rsteams.com/$1 [L,R=301]
    Other than forgetting to escape the dots in the RewriteCond (i.e. it should be www\.rsteams\.com) this is okay. Slightly better would be

    Code:
    RewriteCond %{HTTP_HOST} ^www\.rsteams\.com [NC]
    RewriteRule .? http://rsteams.com%{REQUEST_URI} [L,R=301]
    because it doesn't introduce a new variable, but the difference in performance is negligible.

    Code:
    RewriteCond %{REQUEST_URI} act/(.*)/
    This line serves no purpose whatsoever, since this intent is already conveyed in the next line. Get rid of this line.

    Code:
    RewriteRule ^act/(.*)/$ http://rsteams.com/index.php?act=$2
    Remove the trailing / at the end (as per your spec, you wanted to match /act/test -- no trailing slash there), and replace (.*) with the characters you actually want to match. Also, you should just put index.php there, not the complete URL -- as that might trip Apache into a 302 redirect instead of a just a rewrite. Also, you're referencing $1, not $2 (since it's the first atom -- the first match within parentheses). There is no $2 (or above) in this RewriteRule.
    For example, to match digits and letters, it'd be:

    Code:
    RewriteRule ^act/([a-z0-9]+)$ index.php?act=$1
    Code:
    AuthGroupFile /dev/null
    AuthType Basic
    AuthUserFile /home/*CENSORED*/domains/rsteams.com/.htpasswd/public_html/.htpasswd
    AuthName "Administrators' Area"
    require valid-user
    This is fine

    Code:
    <FilesMatch "\.php$">
    AddHandler x-httpd-php53 .php
    </FilesMatch>
    Not a fan of this setup but as this is completely out of your hands: fine as is.

    All together now:
    Code:
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^www\.rsteams\.com [NC]
    RewriteRule .? http://rsteams.com%{REQUEST_URI} [L,R=301]
    
    RewriteRule ^act/([a-z0-9]+)$ index.php?act=$1
    
    AuthGroupFile /dev/null
    AuthType Basic
    AuthUserFile /home/*CENSORED*/domains/rsteams.com/.htpasswd/public_html/.htpasswd
    AuthName "Administrators' Area"
    require valid-user
    
    <FilesMatch "\.php$">
    AddHandler x-httpd-php53 .php
    </FilesMatch>
    Rémon - Hosting Advisor

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

  6. #6
    SitePoint Enthusiast Dreeass's Avatar
    Join Date
    Sep 2012
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Code:
    RewriteEngine On
    This is fine. Without this the rest wouldn't work.

    Code:
    RewriteBase /
    Everybody and their aunt puts this in the their .htaccess while in 99 out of 100 cases it's not needed at all. The only time I recall I've ever needed it is on a host with mass virtual hosting, but I don't think many hosts in the wild use this.
    Most likely you can remove this without any consequences.

    Code:
    RewriteCond %{HTTP_HOST} ^www.rsteams.com [NC]
    RewriteRule ^(.*)$ http://rsteams.com/$1 [L,R=301]
    Other than forgetting to escape the dots in the RewriteCond (i.e. it should be www\.rsteams\.com) this is okay. Slightly better would be

    Code:
    RewriteCond %{HTTP_HOST} ^www\.rsteams\.com [NC]
    RewriteRule .? http://rsteams.com%{REQUEST_URI} [L,R=301]
    because it doesn't introduce a new variable, but the difference in performance is negligible.

    Code:
    RewriteCond %{REQUEST_URI} act/(.*)/
    This line serves no purpose whatsoever, since this intent is already conveyed in the next line. Get rid of this line.

    Code:
    RewriteRule ^act/(.*)/$ http://rsteams.com/index.php?act=$2
    Remove the trailing / at the end (as per your spec, you wanted to match /act/test -- no trailing slash there), and replace (.*) with the characters you actually want to match. Also, you should just put index.php there, not the complete URL -- as that might trip Apache into a 302 redirect instead of a just a rewrite. Also, you're referencing $1, not $2 (since it's the first atom -- the first match within parentheses). There is no $2 (or above) in this RewriteRule.
    For example, to match digits and letters, it'd be:

    Code:
    RewriteRule ^act/([a-z0-9]+)$ index.php?act=$1
    Code:
    AuthGroupFile /dev/null
    AuthType Basic
    AuthUserFile /home/*CENSORED*/domains/rsteams.com/.htpasswd/public_html/.htpasswd
    AuthName "Administrators' Area"
    require valid-user
    This is fine

    Code:
    <FilesMatch "\.php$">
    AddHandler x-httpd-php53 .php
    </FilesMatch>
    Not a fan of this setup but as this is completely out of your hands: fine as is.

    All together now:
    Code:
    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^www\.rsteams\.com [NC]
    RewriteRule .? http://rsteams.com%{REQUEST_URI} [L,R=301]
    
    RewriteRule ^act/([a-z0-9]+)$ index.php?act=$1
    
    AuthGroupFile /dev/null
    AuthType Basic
    AuthUserFile /home/*CENSORED*/domains/rsteams.com/.htpasswd/public_html/.htpasswd
    AuthName "Administrators' Area"
    require valid-user
    
    <FilesMatch "\.php$">
    AddHandler x-httpd-php53 .php
    </FilesMatch>
    Thank you very much for the explanation, it directs well now. Thanks a ton!
    (Bedankt)

  7. #7
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,605
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    Nice job, Rémon!

    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


Tags for this Thread

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
  •