htaccess - read image from own server if external image not found - .htaccess

I try some code examples from stackoverflow, but nothing seems to work.
So I have <img src="http://external-url.com/image.png" /> and I want to show not-found.png from my server if http://external-url.com/image.png doesn't exists.
Can I do that ?

You can use this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(png|jpe?g|tiff|gif)$ /path/to/not-found.png [L,NC]
Change /path/to/not-found.png to your actual path of not-found.png.

Related

Refresh page on apache with React doesn't work

I'm trying to use React to create a SPA, but I'm running into problem when trying to reload the page.
Before I continue, I must say I already read these questions this and this. I manageg to made it work, but only for the first url.
For example, I have a blog page containing all the posts, accessed via this url mysite.com/blog. This one works fine, if I refresh the page, everything reloads again.
However, when I try to access a single post using a dynamic url, then the page doesn't work. For example, I have this router setup:
// Definiton
<Route path="/post/:url" component={Post} />
// Link
<NavLink to={"post/" + post.url}>...</NavLink>
// Url on the browser
mysite.com/post/this-is-my-first-post
In this case, it's not working. This is my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
After looking more at the problem, I noticed it's trying to load the main files from a different location. For example, when I'm on the page mysite.com/blog it's loading the .css and .js files from mysite.com.
But when I'm the page mysite.com/post/this-is-my-first-post and I refresh the page, it's trying to load the .css and .js files from the directory mysite.com/blog.
I did what Stuffix told in the answe, to check the url definition and also the config at my apache, but everything is enabled and working, just like the answer says.
Well, after looking at some other projects I have using Angular, I noticed one thing, and it was easier than I tought.
Just had to add the tag base on the head of my index.html.
<base href="/" />
This htaccess worked for me with react and angular
<IfModule mod_rewrite.c>
RewriteEngine on
# -- REDIRECTION to https (optional):
# If you need this, uncomment the next two commands
# RewriteCond %{HTTPS} !on
# RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# --
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) index.html [NC,L]
</IfModule>
In your <Route />, you're declaring the post route is /post/this-is-my-first-post which is correct.
But in your <NavLink />, you're pointing towards /blog/post/this-is-my-first-post since you've missed a slash.
<NavLink to={"post/" + post.url}>...</NavLink>
| here
Thus leading to a 404.
If this doesn't help, your snippet looks fine so the problem might be on what does post.url returns.
Also, your .htaccess looks fine so I would say you might have missed something in your Apache config like AllowOverride all. See this answer.

URL rewrite in subfolder css not working

How are you today,
I have a problem in url rewriting..
I Upload my website on pesco sub-directory, Like www.example.com/pesco and my css path like this /css/style.css but when site run css must be like www.example.com/pesco/css/style.css but my css showing like this www.example.com/css/style.css url rewrite missed the pesco sub-directory
Please any one give me the solution
My Code is below you can see.
ErrorDocument 404 /notfound.php
RewriteEngine on
RewriteBase /pesco/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.html index.php
RewriteRule ^aboutus.html aboutus.php
RewriteRule ^([a-zA-Z0-9_-]+).html$ products.php?catid=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).html$ detail.php?category=$1&product=$2
I think your error is just a reference problem. You can try ./css/style.css as this means it looks to find css as a directory inside the same directory as the file requesting it, whereas /css/style.css is essentially looking in the root directory

yii, mamp, htaccess rewrite urls

I am trying to rewrite yii urls but with no luck. I have spent hours going through sites and come up with the same answer each time which doesn't work for me:
http://www.yiiframework.com/doc/guide/1.1/en/topics.url#user-friendly-urls
I would like to resolve the urls to basic paths e.g
/index.php/site/index
to
/
/index.php/ads and /index.php/ads/
to
/ads
/ads/details?ad=9
to
/ads/9
The problem seems to be that the .htaccess has no effect.
I am using:
mamp pro
on a mac with lion
and the web directory is different to the webserver root.
I have set AllowOveride through the console.
The .htaccess is in the same folder as the main index.php but doesn't register even if I create an error.
I have had no problem with other non-yii web directories using an htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php
//main.php
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
I have a project where my Yii application is in a subfolder as well.
This is my .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
Then in main.php, you'll need to add these lines to the 'rules' array:
'' => 'site/index', // This is for the home page
Is ads the name of your controller? In other words, do you have a file named AdsController.php in your controllers folder? If so, then it will work by default.
Just curious why you're hitting the index.php directly? You should be able to setup your localhost to point directly to your Yii app folder. For example, on my machine I created http:// mytestapp/ and it points to the app's folder in htdocs.
Since you're on a Mac, you need to edit your etc/hosts file as well...
I added a line like this:
127.0.0.1 mytestapp
Hope that helps...

.htaccess 2 Dyanamic Rewrite URL

I have a dynamic url at
http://www.technicae.net/cgi-bin/type/mt-search.cgi?IncludeBlogs=2&tag=2%|tag|&limit=20
where the '|tag|' and '20' part change depending on the tag. This is Movable Type blogging platform which is written in perl. The '|tag|' part is a place holder for the tag. For example, if the tag was 'there' the url would be
http://www.technicae.net/cgi-bin/type/mt-search.cgi?IncludeBlogs=2&tag=2%there&limit=20
and not
http://www.technicae.net/cgi-bin/type/mt-search.cgi?IncludeBlogs=2&tag=2%|tag|&limit=20
I was wondering how to rewrite that in htaccess because everything I try doesn't work. I want it to be
http://www.technicae.net/tag/|tag|
instead of
http://www.technicae.net/cgi-bin/type/mt-search.cgi?IncludeBlogs=2&tag=2%|tag|&limit=20
can you please help me?
note
The URLS are not functional.
This is what you're searching for :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^tag/([^/]+)$ cgi-bin/type/mt-search.cgi?IncludeBlogs=2&tag=2%$1 [L]
RewriteRule ^tag/([^/]+)/limit/([^/]+)$ cgi-bin/type/mt-search.cgi?IncludeBlogs=2&tag=2%$1&limit=$2 [L]
This will allow thos kind of url :
www.domain.com/tag/<myTag>
www.domain.com/tag/<myTag>/limit/<myLimit>
But not :
www.domain.com/limit/<myLimit>
Assuming you are using Apache, make sure you have mod_rewrite enabled.
The .htaccess you should use (this may need tweaked) is:
RewriteEngine On
RewriteRule ^tag/([^\.]+)$ cgi-bin/type/mt-search.cgi?IncludeBlogs=2&tag=2%$1&limit=20 [NC,L]

Redirecting links without a .html extension to link with .html extension

After changing my site structure I need help redirecting links. How can I redirect all links that don't have a .html extension to the same link but with .html extension? The only link I don't want redirected is /admin.
Example: google.com/hey -> google.com/hey.html
google.com/hey.html -> do nothing
google.com/admin -> do nothing
Thanks in advance
Ohh sorry, try this man. The error was in the rewritecond.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !\.html$
RewriteRule (.+) $1.html [L]
If these links are onpage links an non external ones a .htaccess solution could help.
First be aware to use htaccess only if you do not have access to the server config, because htaccess slows the server down. Everything configured there can be configured as well in the .httpdconf
Sorry, I have not tested the code posted bellow but tell me if it has worked.
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} > \.html
RewriteRule (.+) $1.html [L]

Resources