Home > database >  Need to redirect last part of url using htaccess with parameters
Need to redirect last part of url using htaccess with parameters

Time:10-06

#RewriteRule #htaccess I have a url for eg: www.example.com/path1/path2/pdfname.pdf. i need to redirect this url to another with the pdf name without pdf extension like(pdfname). Redirect Url should be www.example.com/path3/viewpdf.php?param=pdfname. Would appreciate your help, Thanks.

CodePudding user response:

Assuming that "path1", "path2" and "path3" are all fixed, literal strings that probably is what you are looking for:

RewriteEngine on
RewriteRule ^/?path1/path2/(. )\.pdf$ /path3/viewpdf.php?param=$1 [L]

This keeps the URL visible in the browser unchanged, which usually is what is desired.

That rule will work likewise in the central http server's host configuration (which usually is preferred) or, if you do not have access to that, in a distributed configuration file (often called ".htaccess"). In the later case that file needs to be readable by the http server process and it has to be located in the DOCUMENT_ROOT folder of the processing http server's host.


If instead of an internal rewrite you really want to redirect the request (so change the URL actually visible in the browser), then that variant should do:

RewriteEngine on
RewriteRule ^/?path1/path2/(. )\.pdf$ /path3/viewpdf.php?param=$1 [R=301,L]

Again no domain name (http host name) or protocol scheme has to be specified if they stay the same. The same hints as above apply.

  • Related