2

I am using mod_rewrite in .htaccess. I can't figure out how to get the following done.

  • All css pages should go to /minify/?f=/styles.css
  • All js pages should go to /minify/?f=/js.js
  • All .txt, .gif, .jpg, .png files should open as they are
  • All other pages (eg: /page, /dir/page, /dir1/dir2/page) should go to /page.php
  • All other files in directories should open as it is (eg: /admin/login.php)

Edit: This is the code that I came up with. It's not elegant, and it fails sometimes. For eg: I can't get /admin/login.php to work. When I open that URL, it's going to page.php.

RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|\.txt)$
RewriteRule .* page.php
RewriteRule ^.*?\.css$ /min/?f=/styles.css [L]
RewriteRule ^.*?\.js$ /min/?f=/js.js [L]
2
  • Great. So anything you actually tried besides writing a list of requirements? How does your .htaccess look and where / how exactly does it fail? What ressources did you consult? Commented Nov 2, 2015 at 11:42
  • Apologies. I pasted the code that I came up with so far. It's not elegant and /admin/... pages don't work. I am hoping that you guys can help me with something better. Commented Nov 2, 2015 at 11:47

1 Answer 1

1

Try the following:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|js|png|txt|gif|jpe?g)$
RewriteRule ^ /page.php [L]

RewriteRule \.css$ /minify/?f=/styles.css [NE,L]
RewriteRule \.(js)$ /minify/?f=/$1.$1 [NE,L]
Sign up to request clarification or add additional context in comments.

5 Comments

I tried your code and it's working fine for all the URLs, except /. I get 403 Forbidden error for http:// website and http:// website/. It's working for http:// website/dir urls though.
@Castortroy The 403 is raised by something else.
I don't have a index.php file. It should also go to page.php. I removed all the code from the .htaccess and tried and it still throws 403 forbidden. So, I am guessing it's because of lack of a index file, but I do want them to be sent to page.php
You can add a custom rule @Castortroy like: RewriteRule ^/?$ /page.php [L]
Perfect !! You are a savior

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.