0

I've been researching the issue for hours and can't seem to find a working solution to this problem. I've created a redirect from /sites/default/files/OLB%20Registration%20Guide.pdf to /sites/default/files/downl_section/Online-Banking-Registration-Guide.pdf that doesn't work.

Here is the line in .htaccess

Redirect 301 "/sites/default/files/OLB Registration Guide.pdf" /sites/default/files/downl_section/Online-Banking-Registration-Guide.pdf

My first inclination was to remove %20, but still nothing... Does anyone have any insight as to how to create a redirect where the original url contains %20?

Thanks!

  • %2520 isn't the same as %20, it's actually the result of encoding that string. Something's double-encoded, basically. Your source for the redirect should probably be "/sites/default/files/OLB% Registration% Guide.pdf" – Clive Apr 3 '18 at 15:18
  • Whoops, I meant to write %20, thanks for noticing that mistake. Thanks for the suggestion. It did not work, though >_> – Jillian Hoenig Apr 3 '18 at 15:22
  • Can you please update on what exactly is happening with the redirect? Are you getting the old file? Are you getting a Drupal 404 page? etc. – Beebee Apr 3 '18 at 15:34
  • Sure, I'm seeing a 404 error with the old url – Jillian Hoenig Apr 3 '18 at 15:37
  • @JillianHoenig is it a Drupal 404 page? Or an Apache/Nginx/WebServer 404 error? – Beebee Apr 3 '18 at 15:49
1

If you are using the Redirect module it has an admin page for fixing 404 errors with redirects. Your file should be listed there since it gives you a 404 error.

fix 404 error

When clicked on "Add redirect" action link, your old link will be pre-populated on the next page for you to create a redirect.

create redirect

That should fix the redirect issue.

In case it doesn't try replacing white-space with + (plus) character. In the URLs + can be used instead of white-spaces.

  • 1
    Using the redirect module, don’t encode the space. Just write it out. this is the name.pdf if you encode it on the admin, it will encode that and store it encoded twice on the DB. – rovr138 Apr 3 '18 at 21:25
0

The redirect might not be going through because of this block in .htaccess:

# Pass all requests not referring directly to files in the filesystem to
# index.php. Clean URLs are handled in drupal_environment_initialize().
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]

Make sure your Redirect 301 ... line is above this block.

Notice the part "not referring directly to files". So if the old file doesn't exist, the request is being rewritten and coming into Drupal.

  • Interesting suggestion. My redirect does precede this line, but could you help me understand "not referring directly to files"? The file at the source url no longer exists... could that be related to the issue? – Jillian Hoenig Apr 3 '18 at 15:58
  • With the help of the those lines in .htaccess any request made to non-existing files or directories are handled with Drupal by redirecting them to index.php. That's how Redirect module or similars can pick up the requests and use it for redirecting. – osman Apr 3 '18 at 16:03
  • Is there a way to resolve the issue without installing a new module? – Jillian Hoenig Apr 3 '18 at 16:04
  • "could that be related to the issue?" Not at all. I was just saying the old file doesn't exist so the conditions the request needs to fulfill in order to go through to Drupal is being met, as you mentioned you're seeing a Drupal 404 page. – Beebee Apr 3 '18 at 16:07
  • @JillianHoenig I've just tried on my D7 and the redirect works for me. I've copied your Redirect line and pasted it just above the block I mentioned in my answer. I still saw 404 (because I don't have the file) but the URL in the address bar changes and I get a 301 response. So something else must be messing with your redirect and without seeing your whole mod_rewrite part of your .htaccess I'm a bit at a loss. – Beebee Apr 3 '18 at 16:20
0

The working htaccess rule that will do the Redirect is as follows.

Redirect 301 "/sites/default/files/OLB Registration Guide.pdf" "/sites/default/files/downl_section/Online-Banking-Registration-Guide.pdf"

This will only work if both the files exist in the system. If the first file does not exist then the rewrite rule that rewrites all urls that do not map to files to index.php will get applied. Here is the reference on the redirect / rewrite order - https://webmasters.stackexchange.com/questions/105937/execution-order-of-different-htaccess-redirects/105938#105938

Now you can also do this using Rewrite Rules. You will need to have the %MN patterns replaced by xMN in the match strings. There is a question related to this - https://stackoverflow.com/questions/46445000/htaccess-characters-not-escaping-in-rewrite-string

  • 1
    I'm not sure if this is accurate. I am pretty sure that all mod_write rules happen before mod_alias rules, because the modules execute in that order, and not by the order of the rules in the file. I have run into that issue before when mixing Redirect and Rewrite. – mpdonadio Nov 16 at 19:07
  • Interesting. Looks like I was wrong there. I will update the answer. Here is the reference for the same - webmasters.stackexchange.com/questions/105937/…. What is also interesting is that the rule worked for me. Now I see why. Since both the files existed in my folders the rewrite rules ignored it because of the file exists check before rewriting to index.php. – anoopjohn Nov 16 at 20:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.