ptmr.io
ptmr.io

Reputation: 2315

.htaccess, check if RewriteRule Regex exists as a file or dir

In order to check if a file eixists (does not exist), I'll write something like that

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((en|es|de)/)?([a-z0-9\+_-]+)/?$ something.php?hl=$2&seoq=$3 [QSA,L,NC]

This works great, BUT how do I check if the file exists based on the result variables of the RewriteRule.

--

Example:

RewriteCond $3 -f (???)
RewriteRule ^((en|es|de)/)?(.*) $3?hl=$2 [QSA,L,NC]

--

What does/should it do?

Whenever a file, e.g. /en/subdir/something.jpg, is called in the address bar, the server should look, does /subdir/something.jpg exist (please note: no subfolder /en!)

The same should happen for directories. Conclusion: if $3 [= (.*) in the RewriteRule] exists as a file or directory, it should show this file or directory, else ignore. How can this be done?

Upvotes: 2

Views: 610

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

Try:

RewriteCond %{REQUEST_URI} ^((en|es|de)/)?(.*)
RewriteCond %{DOCUMENT_ROOT}/%3 !-f
RewriteCond %{DOCUMENT_ROOT}/%3 !-d
RewriteRule ^((en|es|de)/)?([a-z0-9\+_-]+)/?$ something.php?hl=$2&seoq=$3 [QSA,L,NC]

Upvotes: 1

Related Questions