Geraldo de Oliveira
Geraldo de Oliveira

Reputation: 23

.htaccess file rewrite second rule

I would like to create a rule for pdf files like domain.com/folder/directory/filename.pdf would be accessible with domain.com/filename.pdf, so I've created this rule:

RewriteRule ^([a-zA-Z0-9_\.-]*).pdf$ /images/qualita/certificati/$1.pdf [NC,L]

It works. Now I'd like to do the same with another file that is in another folder, so I'm adding this second rule:

RewriteRule ^([a-zA-Z0-9_\.-]*).pdf$ /images/catalogo/$1.pdf [NC,L]

It doesn't work. It gives me error 404. And I think it's because the first rule. Even if I added the [L] flag, so what can I do to make all the .pdf files accessible like domain.com/filename.pdf?

Upvotes: 0

Views: 42

Answers (2)

Mohammed Elhag
Mohammed Elhag

Reputation: 4302

Try this :

RewriteCond %{DOCUMENT_ROOT}/images/qualita/certificati%{REQUEST_URI}\.pdf -f
RewriteRule ^([a-zA-Z0-9_\.-]*).pdf$ /images/qualita/certificati/$1.pdf [NC,L]
RewriteCond %{DOCUMENT_ROOT}/images/catalogo%{REQUEST_URI}\.pdf -f
RewriteRule ^([a-zA-Z0-9_\.-]*).pdf$ /images/catalogo/$1.pdf [NC,L]

These rules will check first whether the request is targeting /images/qualita/certificati/ directory or not and then check another directory /images/catalogo/ .

If you need to check first /images/catalogo make the third & forth lines first.

Be aware that , if there is same file in both directories a priority will be given to the first one .

Upvotes: 1

grupowebex
grupowebex

Reputation: 185

Maybe double rewrite, like this (no tested)

RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^([a-zA-Z0-9_\.-]*).pdf$ /images/qualita/certificati/$1.pdf

RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^\/images\/qualita\/certificati\/([a-zA-Z0-9_\.-]*)\.pdf$ /images/catalogo/$1.pdf

Upvotes: 1

Related Questions