March 1st, 2018, 07:36 AM
-
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)?
March 1st, 2018, 10:52 AM
-
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.
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/
March 2nd, 2018, 03:15 AM
-
@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)?
March 2nd, 2018, 03:46 AM
-
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.
March 2nd, 2018, 03:51 AM
-
@requinix,
Could you show alternative instructions, instead of
Code:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
using different rules if necessary?
March 2nd, 2018, 03:53 AM
-
... also, could you explain what that regular expression does?
March 2nd, 2018, 04:41 AM
-
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.
March 2nd, 2018, 02:55 PM
-
@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?
March 3rd, 2018, 12:14 AM
-
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.
March 3rd, 2018, 01:45 AM
-
@requinix,
Could you explain how is it different? What is the actual use of [NC] flag?
March 3rd, 2018, 02:03 AM
-
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.