4

I'm trying to get the following dynamic URL:

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

To be rewritten in the browser's address bar:

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

Basically, the user is entering in a request to view a PDF that is available on the file/web server and based on the ID number provided, the URL is rewritten to go and retrieve the document from a sub-folder under DOCUMENT_ROOT/pdfs/ whose folder name matches the ID provided along with the PDF filename matching the same ID. What can I try next? Everything I've tried does not work.

1 Answer 1

1

Try the following rule in 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]
Sign up to request clarification or add additional context in comments.

2 Comments

This worked perfectly for me! Thank you for your help! You helped restore my sanity! LOL.
UPDATE: This worked fine for on a dedicated Red Hat server...but for the life of me, when I tried this exact same file on a DreamHost shared server I'm back at square one. It doesn't work anymore. I've used the above with htaccess Tester @ htaccess.madewithlove.be and it fails there too. It says THE_REQUEST is not supported. Any other suggestions please?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.