user36319
user36319

Reputation: 69

htaccess redirect starting URL expression to blog page

I want to redirect certain URLs starting with an expression. For ex

I want to redirect:

www.example.com/%2FE (www.example.com/%2FExxxxxxxx) to my blog page in my .htaccess file.

I can redirect www.example.com/2FExxxxx but I am not able to target the %.

The xxxx... I have used in the URL is to represent any expression after %2FE.

This is my code:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule %2FE /blog [R=301,L]
<IfModule>

Can anyone here help me?

Upvotes: 1

Views: 108

Answers (1)

MrWhite
MrWhite

Reputation: 45829

By default Apache rejects (with a server generated 404) any URL that contains an encoded slash (%2F) in the URL-path part of the URL. This occurs before the request is processed by .htaccess. (This is considered a security feature.)

To specifically permit encoded slashes, there is the AllowEncodedSlashes directive (default value is Off). But this can only be set in a server or virtualhost context. It cannot be set in .htaccess. To permit encoded slashes, AllowEncodedSlashes can be set to either On or NoDecode (preferable).

For example:

# In a server / virtualhost context (not .htaccess)
AllowEncodedSlashes NoDecode

Then, once the above has been implemented in the server config and the webserver restarted, you can proceed to match the slash using mod_rewrite in .htaccess...

RewriteRule %2FE /blog [R=301,L]

Ordinarily, the RewriteRule pattern matches against the %-decoded URL-path. However, if the NoDecode option has been set then the encoded slash (%2F) is not decoded. So the above "should" work (except that the pattern is not anchored, so potentially matches too much).

But note that multiple (decoded) slashes are reduced in the URL-path that is matched by the RewriteRule pattern. So matching multiple-contiguous slashes here is not possible.

I would instead match against the THE_REQUEST server variable, which is as per the original request and always remains %-encoded (if that is how the request has been made). And multiple slashes are preserved. Note that THE_REQUEST contains the first line of the HTTP request headers, not just the URL-path.

For example:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/%2FE [NC]
RewriteRule . /blog [R=301,L]

You should not use the <IfModule> wrapper here.

Upvotes: 2

Related Questions