0

I want to redirect

https://example.com/uploads/cms_files/GENERAL-TERMS-AND-CONDITIONS.pdf?docs/GENERAL_TERMS_AND_CONDITIONS.pdf

to

https://exmaple.com/uploads/cms_files/GENERAL-TERMS-AND-CONDITIONS.pdf

by using .htaccess file in core php but it's not redirecting because I have ?query in my URL but there is no key for the value after question mark.

Can someone help me how to redirect this URL in .htaccess?

1
  • Why would you have to rewrite that at all? If an object exists that can be requested using that URL the query string can simply be ignored...
    – arkascha
    Commented Jul 20, 2017 at 8:09

1 Answer 1

2

The direct implementation of what you ask would be this:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^docs/GENERAL_TERMS_AND_CONDITIONS\.pdf$
RewriteRule ^/?uploads/cms_files/GENERAL-TERMS-AND-CONDITIONS\.pdf https://example.com/uploads/cms_files/GENERAL-TERMS-AND-CONDITIONS.pdf [R=301,QSD]

That rule will work likewise in the http servers host configuration and in a dynamic configuration file (.htaccess). If you decide to use a dynamic configuration file, then you have to take care that its interpretation is enabled at all (AllowOverride directive), that such file is readable by the http server process and that it is located in the right location inside the http hosts document folder.

However why would you have to rewrite that at all? If an object exists that can be requested using that URL, then the query string can simply be ignored...


And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only supported as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

1
  • Thanks, it's really helpful
    – BornCoder
    Commented Jul 20, 2017 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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