#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    36
    Rep Power
    2

    htaccess code - need explanation


    I have the following htaccess code:

    Code:
    Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteRule ^phpinformation.php /info.php [R,NC,L]
    RewriteRule ^phpinformation /info.php [R,NC,L]
    RewriteBase /
    ## hide .php extension # To externally redirect /dir/foo.php to /dir/foo
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    RewriteRule ^ %1 [R,L,NC]
    ## To internally redirect /dir/foo to /dir/foo.php
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^ %{REQUEST_URI}.php [L]
    The code allows the existing URL http: //server_address/info.php to display without extensions, i.e. http: // server_address / info.
    It also allows a non-existent URL, http: // server_address / phpinformation, to be redirected to http: // server_address / info.
    Also, a non-existent address http: //server_address/phpinformation.php is redirected to an existing http: // server_address / info.

    Note
    : # is a comment mark.

    Can you explain in this code what each line does (line by line)?
  2. #2
  3. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,862
    Rep Power
    9646
    Code:
    RewriteRule ^phpinformation.php /info.php [R,NC,L]
    Redirect phpinformation.php to /info.php.
    Code:
    RewriteRule ^phpinformation /info.php [R,NC,L]
    Redirect phpinformation to /info.php.
    Code:
    RewriteBase /
    Remove a / prefix from URLs when matching.
    Code:
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    The next RewriteRule only happens if THE_REQUEST (eg, "GET /foo.php HTTP/1.1") matches that pattern. As in the request ends with .php. This is a silly way of doing that requirement but it works.
    Code:
    RewriteRule ^ %1 [R,L,NC]
    Redirect everything to whatever the value of the first capturing group from the previous RewriteConds was.
    Code:
    RewriteCond %{REQUEST_FILENAME}.php -f
    The next RewriteRule only happens if the requested file does not exist as a file.
    Code:
    RewriteRule ^ %{REQUEST_URI}.php [L]
    Silently rewrite everything to whatever it was plus a .php extension.
    Last edited by requinix; March 2nd, 2018 at 03:45 AM. Reason: s/suffix/prefix/
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    36
    Rep Power
    2
    @requinix,

    Instead of the following:

    Code:
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    The next RewriteRule only happens if THE_REQUEST (eg, "GET /foo.php HTTP/1.1") matches that pattern. As in the request ends with .php. This is a silly way of doing that requirement but it works.

    What are alternative ways (without regular expression)?
  6. #4
  7. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,862
    Rep Power
    9646
    Without regular expressions and mod_rewrite? You don't. I'm not talking about removing regular expressions. I mean that using a RewriteCond to match the request is silly because RewriteRule can already do that.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    36
    Rep Power
    2
    @requinix,

    Could you show alternative instructions, instead of
    Code:
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    using different rules if necessary?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    36
    Rep Power
    2
    ... also, could you explain what that regular expression does?
  12. #7
  13. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,862
    Rep Power
    9646
    On second thought, using just a RewriteRule makes the scheme more complex and fragile. I wouldn't do it.

    I still don't like THE_REQUEST though. It's for people who don't know that REQUEST_URI exists.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    36
    Rep Power
    2
    @requinix,

    Could you also explain the use of flags at each line. For example, I don't understand the use of [NC] flag.
    In the documentation it says that it makes uppercase letters equal as lowercase. However, this doesn't works. It depends if the OS has case-sensitive file system.

    So, practically, there is no use of [NC] flag without extra configuration, right? Am I wrong?
  16. #9
  17. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,862
    Rep Power
    9646
    I don't know what documentation you're reading, but the one I'm reading says NC "makes the pattern comparison case-insensitive". That's a far cry from saying lowercase and uppercase are the same thing.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    36
    Rep Power
    2
    @requinix,

    Could you explain how is it different? What is the actual use of [NC] flag?
  20. #11
  21. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,862
    Rep Power
    9646
    It makes the pattern comparison case-insensitive. Do you not understand what that means? RewriteCond and RewriteRule both have a pattern, they compare that pattern against something (depending), and when they compare in a case-insensitive way the uppercase and lowercase versions of a letter will be considered to match each other.

IMN logo majestic logo threadwatch logo seochat tools logo