BentCoder
BentCoder

Reputation: 12740

Passing URL with htaccess

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
}

Upvotes: 3

Views: 91

Answers (2)

Jon Lin
Jon Lin

Reputation: 143886

Try:

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

Upvotes: 2

douwe
douwe

Reputation: 1315

Try this:

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

without the RewriteCond

Upvotes: 1

Related Questions