Laura Schoebel
Laura Schoebel

Reputation: 114

Questionable rewrite rule in htaccess file

I just found this rewrite rule appear in my htaccess file. I have no idea what it does and google doesn't seem to be able to come up with a sufficient answer either. Does anyone know what it is or does?

RewriteRule ^([^/]*)/$ /wp-hloper.php?p=$1 [L]

Upvotes: 0

Views: 103

Answers (1)

Ultimater
Ultimater

Reputation: 4738

RewriteRule will match any request sent to your server against the text on the left and rewrite it to the text on the right.

The [L] is a flag meaning "last" thus do not preform any more rewrites in this .htaccess request. Although, as confusing as it may be, how .htaccess works, if the URL was rewritten, .htaccess will need to start from the beginning of the .htaccess file again in case any more rewritting needs to be done even if it encounters a last flag.

Regarding the text on the left, it is a regular expression.

The first ^ means match from the beginning of the URL, and not somewhere in the middle.
The parenthesis saves all text matched between the parenthesis as match number 1 and can be accessed as $1 on the right.

[^/] is a character list. Since it begins with a ^ it inverts the whole characer list to a not.

Next, inside of the character list we find / meaning a forward slash. Thus the entire character list translates to: match ANY character which isn't a slash, once.

On the outside of the character list is a * which means match the stuff on the left 0 or more times.
Thus matching any combination of characters which doesn't contain a slash.

Finally, we end our parenthesis which saves the combination of characters as match number 1.
Next in the regular expression is / which matches a slash which must appear in the URL.
Finally we encounter a $ which means the regular expression must match to the end of the URL and not only a portion of it.

The following are all legal matches for the left:
help/, about/, and /.

On the right, we encounter /wp-hloper.php?p=$1.
This rewrites the URL to wp-hloper.php followed by a question mark marking the beginning of the query string. If no question mark appeared, when the URL is rewritten, it would preserved the query string of your initial URL if it was present.
Since the rewrite on the right has a query string, any query string that may have been present on the initial URL will be dropped during the rewrite.
For instances which you want to drop the query string altogether, you'd place a question mark followed by nothing.

In the query string we have p=$1 this will send a $_GET['p'] parameter to PHP which will carry the value of the $1 match from the regular expression we matched on the left.
Thus rewriting requests like help/ or / to your wordpress wp-hloper.php file and sending it $_GET['p']='help' and $_GET['p']='', without the tailing slash, respectively.

Upvotes: 1

Related Questions