Apache mod-rewrite htaccess - dynamic url with parameters

fellow programmers!

I am trying to get the following dynamic url:

http://example.com/pdfs/viewpdf?id=1494

To rewrite in the address bar of the browser:

http://example.com/pdfs/1494/1494.pdf

In principle, the user enters a request to view the PDF file available on the file / web server, and based on the provided identification number, the URL is rewritten to go to and receive the document from the subfolder in DOCUMENT_ROOT / pdfs / the folder name matches the identifier specified with PDF file name matching one ID. Can anyone help? Everything I tried does not work.

Thanks in advance!

+4
source share
1 answer

: root/.htaccess:

RewriteEngine on
#1 Iteration)Redirect "/pdfs/viewpdf/?id=123" to "/pdfs/123/123.pdf"
RewriteCond %{THE_REQUEST} /pdfs/viewpdf/?\?id=([^&\s]+) [NC]
RewriteRule ^ /pdfs/%1/%1.pdf? [L,R]
#2 iteration) internally map "/pdfs/123/123.pdf" to "/pdfs/viewpdf/?id=123"
RewriteRule ^pdfs/[^/]+/([^.]+)\.pdf$ /pdfs/viewpdf/?id=$1 [L,QSA]
+1

Source: https://habr.com/ru/post/1658864/


All Articles