Max Cruer
Max Cruer

Reputation: 110

multiple complex .htaccess RewriteRules

I need help setting up the RewriteRules in my .htaccess file. I need to first check for any existing files, then some custom rewrites, and if any of those doesn'y match rewrite it to index.php.

My current .htaccess, located in the document root, file looks like this:

RewriteEngine On

RewriteBase /

# Don't rewrite existing files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

# Custom rewrites
# TODO: Don't know how to make these work
# RewriteRule ^/css/style\.css$  /css/style.css.php [L,NC,QSA]
# RewriteRule ^/js/config\.js$   /js/config.js.php [L,NC,QSA]
# RewriteRule ^/js/post\.js$     /js/post.js.php [L,NC,QSA]

# If any of the above don't match hand it to index.php
RewriteRule ^(.+)$ index.php [QSA,L]

php_value upload_max_filesize 10485760
php_value post_max_size 10485760

Thanks in advance!

Upvotes: 0

Views: 34

Answers (1)

anubhava
anubhava

Reputation: 784898

Have it this way:

RewriteEngine On
RewriteBase /

# Custom rewrite rule
RewriteRule ^(?:css/style\.css|js/(?:config|post)\.js)$ $0.php [L,NC]

# Don't rewrite existing files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# If any of the above don't match hand it to index.php
RewriteRule . index.php [L]

This is the new rule:

RewriteRule ^(?:css/style\.css|js/(?:config|post)\.js)$ $0.php [L,NC]

Which matches /css/style.css or /js/config.js or /js/post.js and added .php in the end of these URIs. $0 is back-reference of the complete match.

Upvotes: 3

Related Questions