Kevin
Kevin

Reputation: 2688

.htaccess Rewrite Rule Possibility?

I'm having a little issue. One of my clients recently sent out an email blast to 6500 people, that included an invalid link to a PDF file.

The link was simply: http://theirsite.com/pdf/thepdf.pdf%20

So, I'd like to be able to do an htaccess rewrite for them to the valid http://theirsite.com/pdf/thepdf.pdf file

So far, everything I've tried does not work.

Here is what I've tried thus far:

RewriteRule ^(/pdf/thepdf.pdf[%20|\s]+)$ /pdf/thepdf.pdf [R=301,L]

RewriteRule /pdf/thepdf.pdf([%20|\s]+)$ /pdf/thepdf.pdf [R=301,L]

RewriteRule /pdf/thepdf.pdf%20 /pdf/thepdf.pdf [R=301,L]

RewriteRule /pdf/thepdf.pdf%20 /pdf/thepdf.pdf [R=301,L]
RewriteRule /pdf/thepdf.pdf  /pdf/thepdf.pdf [R=301,L]

RewriteRule /pdf/thepdf.pdf(.+?) /pdf/thepdf.pdf [R=301,L]

Something to note here, if I click the original link, but remove the %20 and put in a space, the rewrite works.

Just does not work with the %20

Upvotes: 0

Views: 428

Answers (2)

Mike Brant
Mike Brant

Reputation: 71404

Since this is .htaccess there should not be a mandatory forward slash at the beginning of the match rule. Try this:

RewriteEngine On
RewriteRule ^/?pdf/thepdf\.pdf\s+$ /pdf/thepdf.pdf [R=301,L]

Note:

  • I put a /? at the beginning of the match rule as this is good general practice to make the rule work in either a host config context or an .htaccess context. Since a / would be required if this rule was in host config.
  • You should escape the . before .pdf otherwise it will act as a wildcard match.
  • I added anchors (^ and $) at the beginning and the end of the match to make sure this matches the entire resource string

Upvotes: 2

user1269636
user1269636

Reputation:

%20 is a URL encoded space, so you can use a lazy select (.+?) in your .htaccess file.

RewriteEngine On
RewriteBase /
RewriteRule /pdf/thepdf.pdf(.+?) /pdf/thepdf.pdf [L]

These rules should normally work.

Upvotes: 0

Related Questions