Passing GET parametrs to .htaccess - css

I'm trying to minify css/js files if there is exists minified css/js copy of this file with .min. in it's name though .htaccess. I'm using following code:
RewriteCond %{DOCUMENT_ROOT}/$1.min.$2 -f
RewriteRule ^([^.]+)\.(css|js)$ /$1.min.$2 [L,NC]
So, when I go to /current/assets/css/main.css I see /current/assets/css/main.min.css instead. Is there any way to make it only if I add some GET parametr to url? For example /current/assets/css/main.css?t=1 gives minified file, but /current/assets/css/main.css not. I tried:
RewriteCond %{DOCUMENT_ROOT}/$1.min.$2 -f
RewriteRule ^([^.]+)\.(css|js)\?t=[0-9].*$ /$1.min.$2 [L,NC]
But it's not working. Guess I mabye should use %{QUERY_STRING} but can't get how. Thanks for advices.

I found how to do that. It was all about %{QUERY_STRING}.
RewriteCond %{QUERY_STRING} ^t=([0-9]*)$
RewriteCond %{DOCUMENT_ROOT}/$1.min.$2 -f
RewriteRule ^([^.]+)\.(css|js)$ /$1.min.$2 [L,NC]

Related

Rewrite .htaccess all files except

I currently have the following in my .htaccess file which rewrites all files names so I can limit access to my media.
#RewriteCond %{REQUEST_FILENAME} -s
#RewriteRule ^wp-content/uploads/(.*)$ dl-file.php?file=$1 [QSA,L]
However, I would like specific files to be ignored. For example: ^wp-content/uploads/2022/12/image-26.png and ^wp-content/uploads/2022/11/horse47.jpg
How can I update my rewrite rules to skip those specific files (and others).
I tried rewriting the original files after the above (3rd line) but does not work.
Solved thank you. Mr White
I tried rewriting the original files after the above (3rd line) but does not work.
"After" is too late, the request will have already been rewritten to your script! You would need to rewrite the files before your existing rule.
For example:
RewriteRule ^wp-content/uploads/2022/12/image-26\.png$ - [L]
RewriteRule ^wp-content/uploads/2022/11/horse47\.jpg$ - [L]
# Existing directives go here...
The L flag prevents the following rules from being processed.
Don't forget to backslash-escape the literal dot and include the end-of-string anchor on the regex.
Alternatively, add exceptions (conditions) to the existing rule. For example:
RewriteCond $1 !^2022/12/image-26\.png$
RewriteCond $1 !^2022/11/horse47\.jpg$
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^wp-content/uploads/(.*) dl-file.php?file=$1 [QSA,L]
The ! prefix negates the expression. So it is successful when it does not match.

Codeigniter CSS, JS and images Not loading, assumes as directory probably

I'm pretty new to Codeigniter and have done all possible searches that I can do before reaching here.
I am using Code igniter 3x, my local CSS, Images and JS are not getting loaded into the Codeingiter application. I tried directly opening the CSS path but i believe Codeingiter assumes the path to be class/method/parameters! Below screenshot for reference.
[
Following is my .htaccess file:
DirectoryIndex
RewriteEngine on
RewriteCond $1 !^(index\.php|img|assets|css|js|robots\.txt|favicon\.ico|logo\.png)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./$1 [L,QSA]
Any help on this would be highly appreciated.
Anything that needs to be changed?
As suggested by #Bhavesh Parab over comment, pleacing the asset folder at root fixed the issue.

.htaccess Rewrite url with images from a folder to a php file

I want to rewrite all the images in my wp-content/uploads folder to a thumbs.php file in wp-content/thumb folder without changing url
This is what I have added in my .htacccess
# BEGIN img
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{HTTP_USER_AGENT} !someweirdcrawler
RewriteRule ^(wp-content/uploads/.+?)\.(jpe?g|png|gif|svg)$ /wp-content/thumbs/index.php?img_source=http://mywebsite.com/$1.$2 [L]
</IfModule>
# END img
# other wordpress default stuff including wp-fastest-cache
This is what I want to achieve
Visitors visit my site or any image with urls like
http://mywebsite.com/wp-content/uploads/2050/12/Hello-Dolly-1.0.0.1-words.jpg
and the rewrites should be
http://mywebsite.com/wp-content/thumbs/index.php?img_source=http://mywebsite.com/wp-content/uploads/2050/12/Hello-Dolly-1.0.0.1-words.jpg
*For those who may think... thumbs.php is not timthumb file. I have written my own script for lossless image compression
UPDATE:
Its working for my other site.. What could be the problem?
FINAL UPDATE:
For Those Who come to find answers Later.
So Finally It was because of my WP FASTEST CACHE htaccess Code. Just removed the -d Directive and worked like a charm. But I am not using this code anymore because I have found better Options.
Here's the code:
# BEGIN img
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
#show original images to Image crawler Like wordpress photon or google images.
RewriteCond %{HTTP_USER_AGENT} !someweirdcrawler
RewriteRule ^(wp-content/uploads/.+?)\.(jpe?g|png|gif|svg)$ /wp-content/thumbs/index.php?img_source=http://mywebsite.com/$1.$2 [L]
</IfModule>
# END img
# other wordpress default stuff including wp-fastest-cache
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
The requested image is never going to be both a file and a directory, so your condition is never going to match and the request is never going to be rewritten. A valid request for an image should only be a file, so remove the second RewriteCond directive (-d directory check).
Its working for my other site..
Are you sure (the code is the same)?

deny file request and redirect to .php page using .htaccess

I'm working with wordpress and I am using .htaccess, but I don't really understand its functionality.
The scenario is the following:
When user tries to reach a file from upload directory (example: http://[localhost]/wordpress/uploads/video.mp4) he is redirected to a php page which decides if the user has a permission to download/view the file or not (for example, redirecting to the following: http://[localhost]/wordpress/serve.php?filename=video.mp4).
Does anyone knows what should I do with the .htaccess?
In which directory should the .htaccess file be placed?
You can use your WordPress root .htaccess file for this, or you can create one in the uploads directory. I'd recommend sticking to the file you're already using for WordPress.
/wordpress/.htaccess amended content
RewriteBase /wordpress/
# ^ your base should already be set - if it hasn't, put this just under RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^uploads\/(.*) serve.php?filename=$1 [R,L]
Note: If you do not want to redirect, you can still disguise it under the actual file name, and it will still return the PHP response. You can do this by removing the R flag. (This will keep the originally-requested URL in the address bar.)
EDIT: based on your comment, and revealing where your .htaccess file resides, try the following:
RewriteBase /wordpress/wp-content/uploads/
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*) /wp-content/themes/fun&share/serve.php?filename=$1 [R,L]
EDIT 2: My apologies, I did not account for the RewriteBase. Please see the code below:
RewriteEngine On
RewriteBase /wordpress/wp-content/uploads/
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*) ../themes/fun&share/serve.php?filename=$1 [R,L]
This .htaccess file must be placed in your uploads folder.
Try adding these rules to an htaccess file in your /wordpress/uploads directory:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ /wordpress/serve.php?filename=$1 [L]

wordpress installation does not work due to ,htaccess file

I have a site running at my server and here is the content of the .htaccees file
RewriteEngine on
RewriteCond $1 !^(index\.php|img|public|robots\.txt) [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]
few days back i installed Wordpress in a folder: example.com/my-blog/ as a stand-alone application
but i could not access it. if i remove the .htaccess file it does work fine but that makes a down the site which in any case i cant not afford. please help.
please help, i am newbie in this.
You could exlude your subfolder from rewriting by adding these two lines to your htaccesfile, before your rewriterule:
RewriteCond %{REQUEST_URI} !^/my-blog/ [NC]
RewriteCond %{REQUEST_URI}!=/my-blog [NC]
Ansari is right btw about $1 not being set and therefore, never rewriting anything.

Resources