ModRewrite: Image in folder if isn't found - .htaccess

I want to get a default image in a specific folder if the image file doesn't exist
so http://xyz.de/projekt/thumb/12_2323.jpg should check if http://xyz.de/somehidden/folderonwebspace/project/thumb/12_2323.jpg exists.
if not http://xyz.de/somehidden/folderonwebspace/project/thumb/default.png
RewriteRule ^projekt/thumb/([a-zA-Z0-9_]+\.(gif|jpg|jpeg|png){1}) /somehidden/folderonwebspace/project/thumb/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*$ /somehidden/folderonwebspace/project/thumb/default.png [L]

Related

htaccess remove parameter and index file

I'm trying to remove the proxy.php file and the parameter passed to it. This is what it looks like currently.
http://localhost/stuff/proxy.php?parameter=folder01/some.file
This is the way I would like it to look.
http://localhost/stuff/folder01/some.file
For my htaccess file, I have tried the following but they do not work.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /proxy.php?parameter=$1 [L]
And this
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /proxy.php?parameter=$1 [L,QSA]
Any ideas?
Try it in root dir or put this in stuff directory and remove RewriteBase line.
RewriteEngine on
RewriteBase /stuff/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ proxy.php?parameter=$1 [L]

.htaccess Issue: JS and CSS Rending index.php

I'm using a WAMP installation and routing everything through my index.php file. The CSS and JS files in my header get called just fine when I remove the .htaccess file, so the file paths are correct, but once I insert the .htaccess file again Chrometools throws this error upon including my JS file
Uncaught SyntaxError: Unexpected token <
When viewing Chrometool's Network>>Response, it shows the JS file contains the content of everything rendered by the index.php file.
Here's my code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteRule ^([^/]*)$ index.php?_page=$1
RewriteRule ^([^/]*)/([^/]*)$ index.php?_page=$1&_action=$2 [QSA,L]
RewriteRule ^([^/]*)/([^/?]*)/(.*)$ index.php?_page=$1&_action=$2&$3 [QSA,L]
I thought the %{REQUEST_FILENAME} !-f was supposed to prevent any rewriting of a file when it's called directly?
UPDATE
I've found the line causing the issue is is the second RewriteRule.
RewriteRule ^([^/]*)/([^/]*)$ index.php?_page=$1&_action=$2 [QSA,L]
Because my JS and CSS are stored two directories deep from my project root, the rule is directing the file to my index.php.
Neither of these below commands have any affect until I comment out the RewriteRule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [NC]
Any ideas?
Try and change this line.
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
To this
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [NC]
See of that works for you.
Edit:
You need the conditions for all your rules. Rewrite Conditions only applies to the first rule following it. Also you can just have it so if it matches static content, then just not do anything. Try your rules this way.
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [NC]
RewriteRule ^(.*)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ index.php?_page=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ index.php?_page=$1&_action=$2 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/?]*)/(.*)$ index.php?_page=$1&_action=$2&$3 [QSA,L]

.htaccess if request is a file or directory, redirect with absolute path

With this htaccess configuration, I have to set all my .js and .css or image path with absolute path.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [L]
How do I have to modify this htaccess so i can put path of the image, css, or js files doesn't have to be an absolute path?
Is it possible?
Thanks before.
ADD
Say my directory structure like this:
root
-- asset
-- images
-- css
-- js
It's probably easier to just add a relative URI base to your page's header:
<base href="/" />
But if you must use htaccess, and all of your scripts and styles are in the same place, you can trap it with a condition:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/path/to/css
RewriteRule ([^/]+\.css)$ /path/to/css/$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/path/to/js
RewriteRule ([^/]+\.js)$ /path/to/js/$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/path/to/images
RewriteRule ([^/]+\.(png|jpe?g|gif))$ /path/to/images/$1 [L,NC]
EDIT:
if the path to your js/css/images are all in the same place, you can try combining the rules:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/assets/
RewriteRule ^(css|js|images)/(.*)$ /assets/$1/$2 [L,NC]

rewrite to another file if .php file not exists

I'm using the following rule to serve the associate .php file
All last directory pathname will go to the associate .php file
Ex. /about/ or /about will go /about.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php`
So when I use http://example.com/abc it points to /abc.php, this is working fine. But if /abc.php file does not exist in that directory then I like to forward that to /listing.php file. How can I do that? please help
Also, I need this to apply to the root directory only. means
http://example.com/..*here*.. not for any subdirectory.
Thanks
Try this, this should be tested:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $0\.php !-f //check if (for example) abc.php not exists then
RewriteRule ^(.*)$ /listing.php
RewriteRule ^(.*)$ $1.php
Update: I tested this on my local server, note that: the folder is test you can adjust for your own
Options All -Indexes
RewriteEngine on
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond test\/$0\.php !-f
RewriteRule ^(.*)$ listening.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php

Htaccess :Rewrite rule issue

I've following code in a .htaccess file. The file is in a subfolder, lets say /content/.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([^/]+)(/([^/]+))?(/(edit)+)(/([^/]+))?/?$ edit.php?category=$1&slug=$3&edit=$5&password=$7 [L]
RewriteRule ^([^/]+)(/([^/]+)?/?)$ content.php?category=$1&slug=$3 [L]
I'm expecting above code will rewrite,
mydomain.com/content/text1/text2/edit/my_password to mydomain.com/content/edit.php?category=text1&slug=text2&password=my_password
mydomain.com/content/text1/text2 to mydomain.com/content/content.php?category=text1&slug=text2
mydomain.com/content/text1 to mydomain.com/content/content.php?category=text1
mydomain.com/content/ to mydomain.com/content/content.php
The code rewrites above correctly, But it breaks the asset urls.
e.g. It rewrites http://mydomain.com/content/js/tms.js to something else, it is same for the css files. My assets folders are /img, /css, /js.
If i commented RewriteRule ^([^/]+)(/([^/]+))?(/(edit)+)(/([^/]+))?/?$ edit.php?category=$1&slug=$3&edit=$5&password=$7 [L] line it works for the assets
Any ideas how to fix this?
The condition RewriteCond %{REQUEST_FILENAME} -d means that "the request is for an existing directory". If you add a ! in front of the -d it changes it to "the request is not for an existing directory".
Try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)(/edit)+/([^/]+?)/?$ edit.php?category=$1&slug=$2&password=$4 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+?)/?$ content.php?category=$1&slug=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+?)/?$ content.php?category=$1 [L]

Resources