3

htaccess below diverts PDF calls to validate.php where name of the PDF is get with $_GET['filename']

Problem is, it works with URLs below but it doesn't when there are many sub directories so what I need is, instead of just getting name of the file, I should pass whole URL to validation.php something like ^(.*)$ /validate.php?request_url=$1 [L] (this is just an example).

Note: instead of whole URL, anything after domain is acceptable.

WORKS:

http://www.whatever.com/1.pdf
http://www.whatever.com/2.pdf
http://www.whatever.com/3.pdf

WON'T WORK:

http://www.whatever.com/sub1/1.pdf
http://www.whatever.com/sub1/sub2/1.pdf
http://www.whatever.com/sub1/sub2/sub3/1.pdf

How can I do it?

Thanks

CURRENT HTACCESS:

RewriteEngine On
RewriteCond %{REQUEST_URI} \.(pdf)$ [NC]
RewriteRule ^(.*)$ /validate.php?filename=$1 [L]

CURRENT VALIDATION.PHP

if (isset($_GET['filename']))
{
   //Do something with it
}
else
{
   //Don't do anything
}

2 Answers 2

2

Try:

RewriteEngine On
RewriteCond %{REQUEST_URI} \.(pdf)$ [NC]
RewriteRule ^ /validate.php?filename=%{REQUEST_URI} [L]
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

RewriteRule ^(.*)\.pdf$ /validate.php?filename=$1 [L]

without the RewriteCond

1 Comment

+1 to you as well because yours solves the problem too however, the reason why I accepted @Jon Lin's is because his solution gives extension of file as well. Not a big deal but saves me time from writing extra lines in validation file. Thanks anyway

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.