简体   繁体   中英

Redirect a query string url to another url in .htaccess PHP?

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?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM