Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess for PDF URL

I have a structure called /pdf/ where generated PDFs are stored. I am trying to use .htaccess to route visitors to the PDF folder.

I have this rule in my .htaccess file:

RewriteEngine On
RewriteBase /
RewriteRule (.*) index.php?page=$1.html
RewriteRule ??? for pdf??

(Note the ??? marks...)

When I try to access the url /manual_chap_1 it is working. But when I try /pdf/manual_chap_1 it doesn't seem to be working. Here is some output I see:

$_GET[page] => index.php.html

I want to be able to generate the PDF file, if the PDF file does not exist in the requested location, and then, I want it to load an HTML page.

like image 689
Maggie Avatar asked Feb 02 '12 00:02

Maggie


1 Answers

Welcome to StackOverFlow, try this:

RewriteEngine On
RewriteBase /

RewriteRule ^pdf/([a-z0-9\-_\.]+)$ pdf.php?file=$1 [L,NC,QSA]

RewriteCond %{REQUEST_URI} !^/pdf
RewriteRule ^([a-z0-9\-_\.]+)/?$ index.php?page=$1.html [L,NC,QSA]

L: The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed. This corresponds to the last command in Perl, or the break command in C. Use this flag to indicate that the current rule should be applied immediately without considering further rules.

QSA : When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.

NC: Use of the [NC] flag causes the RewriteRule to be matched in a case-insensitive manner. That is, it doesn't care whether letters appear as upper-case or lower-case in the matched URI.

Also, I would recommend to use dashes instead of underscore.

like image 193
Book Of Zeus Avatar answered Nov 06 '22 16:11

Book Of Zeus