I'm trying to use htaccess redirect to a file, but can't get the image to display.
I want to redirect ANY request to mydomain.com or www.mydomain.com to www.mydomain.com/test.html
This is the content of my htaccess file:
# Turn rewriting on
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !=/test.html
RewriteCond %{REQUEST_URI} !=*\.png$ [NC]
RewriteRule .* /test.html
I have this line of code in my test.html file:
<img src="image.png" alt="My Image">
But the image doesn't display at all, I just get a broken image. I also tried using absolute path for image, same thing.
Try replacing your .htaccess file with the following
# Turn rewriting on
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#if the request does not end with test.html or is not a .png
RewriteCond %{REQUEST_URI} !(test\.html|\.png)$ [NC]
# Rewrite it to test.html
RewriteRule .* /test.html [L]
This will exclude various types of images
#if the request does not end with .gif,.png or jpg
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png)$ [NC]
RewriteRule ^index.html$ index.php [NC]
Try:
RewriteCond %{REQUEST_URI} !\.png$ [NC]
Not familiar with !. prefix. Check the RewriteCond Docs
Related
What am I trying to achieve?
I want to convert all URL's that are within the /migrate directory (and subdirectories) to lowercase.
What have I done?
I have migrated my website from DNN to Wordpress. Therefore I have URL's in my posts that reference images with a combination of upper and lowercase letters.
All of my directories and files on disk in the /migrate folder (and subfolders) are lowercase.
When I view an article, if the reference to the image has an uppercase character, I am getting a 404 which is expected.
For example:
https://xxx.domain.net/wp-content/migrate/ABC/image.jpg
https://xxx.domain.net/wp-content/migrate/Xyz/image.jpg
https://xxx.domain.net/wp-content/migrate/abc/Image.jpg
https://xxx.domain.net/wp-content/migrate/ABC/Xyz/image.jpg
So after reading various articles online and on here I tried a few things:
1) Enabled mod_speling and added this to apache2.conf
LoadModule speling_module /usr/lib/apache2/modules/mod_speling.so
<IfModule mod_speling.c>
CheckSpelling On
CheckCaseOnly On
</IfModule>
This didn't work, continued to get 404's.
2) Tired to use Rewritemap in my /etc/sites-enabled/mysite.conf file.
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} /migrate [A-Z]
RewriteRule ^(.*)$ /${lc:$1} [R=301,L]
However this would rewrite all URL's to lowercase and it broke some of the Wordpress plugins and editors as the files on disk had uppercase characters.
My .htaccess file in the Wordpress root directory is as follows:
RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
#BEGIN Wordpress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
After hours and hours of trying different things and reading through the logs, I have worked this out!
Basically I needed to ignore all the other Wordpress directories from rewriting.
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteCond %{REQUEST_URI} !^/wp-content/uploads/
RewriteCond %{REQUEST_URI} !^/wp-content/themes/
RewriteCond %{REQUEST_URI} !^/wp-content/plugins/
RewriteCond %{REQUEST_URI} !^/wp-includes/
RewriteCond %{REQUEST_URI} !^/wp-admin/
RewriteRule ^/?(.*)$ /${lc:$1} [R=301,L]
I am moving most pages from a site and I want to redirect all requests to a 404 error page except all/any pages in a certain cname on the site, all/any pages in a certain sub-directory on the site and a certain page in a certain directory on the site.
Here is what I have so far:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^blogs.example.com [NC]
RewriteCond %{REQUEST_URI} !^/private [NC]
RewriteCond %{REQUEST_URI} !^/public/special-form.php [NC]
RewriteCond %{REQUEST_URI} !^/public/special-form_submitted.php [NC]
RewriteCond %{REQUEST_URI} !^/error404.php [NC]
RewriteRule ^(.*) http://example.com/error404.php [R=301,L]
Your issue is mainly the / at the start of your RewriteRule, why?
Because you're using it with an .htaccess so the RewriteRule path starts without the / at the begin.
If it was inside the VirtualHost then that would have worked just fine.
Basically this is what your rule is telling the server to do after our comments conversation and your update:
Options +FollowSymlinks
RewriteEngine On
# if domain is not blogs.example.com
RewriteCond %{HTTP_HOST} !^blogs.example.com [NC]
# and if address does not start with /private
RewriteCond %{REQUEST_URI} !^/private [NC]
# and if the file is not /public/special-form.php
RewriteCond %{REQUEST_URI} !^/public/special-form.php [NC]
# and if the file is not /public/special-form_submitted.php
RewriteCond %{REQUEST_URI} !^/public/special-form_submitted.php [NC]
# and if the file is not /error404.php
RewriteCond %{REQUEST_URI} !^/error404.php [NC]
# then redirect to http://example.com/error404.php
RewriteRule ^ http://example.com/error404.php [R=301,L]
So anything else including images or css or not listed on the conditions would be redirected, in order to avoid that you would have to verify all the needed files within those 2 php pages and white list it, for example with a condition like this:
RewriteCond %{REQUEST_URI} !^/public/[^.]+\.(css|pdf|jpg|doc)$ [NC]
Basically the above means anything within the public folder that ends with .css or .pdf or .doc or .jpg should be displayed.
NOTE: Keep in mind that if your images and documents and whatever else you need to show to your users from those 2 PHP pages are not within the public folder the above will not work, that was merely an example to illustrate an easy way to couple multiple types of files into it.
You could also resume this 2 conditions:
RewriteCond %{REQUEST_URI} !^/public/special-form.php [NC]
RewriteCond %{REQUEST_URI} !^/public/special-form_submitted.php [NC]
as follow:
RewriteCond %{REQUEST_URI} !^/public/(special-form_submitted|special-form)\.php [NC]
You would end with:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^blogs.example.com [NC]
RewriteCond %{REQUEST_URI} !^/private [NC]
RewriteCond %{REQUEST_URI} !^/public/(special-form_submitted|special-form)\.php [NC]
RewriteCond %{REQUEST_URI} !^/public/[^.]+\.(css|pdf|jpg|doc)$ [NC]
RewriteCond %{REQUEST_URI} !^/error404.php [NC]
RewriteRule ^ http://example.com/error404.php [R=301,L]
Just a side information, since you're redirecting anything that does not match the conditions and you're not passing along anything to the redirect, you don't need the (.*), merely using ^ alone will do the job.
I want to redirect this url
http://192.168.1.101/project/test/wordpress/wp-content/uploads/2013/06/Sunset.jpg
to this with main url as following.
http://192.168.1.101/project/test/wordpress/redirect.php?file=http://192.168.1.101/project/test/wordpress/wp-content/uploads/2013/06/Sunset.jpg
If url contain extension like .jpg, .png, .jpeg, .bmp, .png then move all it to redirect.php with file name like.
http://192.168.1.101/project/test/wordpress/redirect.php?file=imagefile
My current .htaccess code
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /project/test/wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /project/test/wordpress/index.php [L]
</IfModule>
# END WordPress
Please Help Me..
Insert this line just after RewriteBase line:
RewriteRule ^wp-content/.+?\.(jpe?g|png|gif|bmp)$ redirect.php?file=http://%{HTTP_HOST}%{REQUEST_URI} [NE,R=302,L,NC]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
How to redirect all pages (pages only) to index.html using htaccess file and not redirect the image files. For some reason I am using this code and an image file on the index.html page isn't showing up.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteRule .* /index.html [L,R=302]
Try this code :
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$
RewriteRule .* /index.html [L,R=302]
Keep your rules simple. Instead of filtering what shouldn't match, just match on the files.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteRule .*\.(php|html)$ /index.html [L,R=302]
A nicer way might be just to ignore existing files. For example:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* / [L,R=302]
The !-d says to ignore the rewrite if there is an existing directory that meets the match.
The !-f says to ignore the rewrite if there is an existing file that meets the match.
Everything else will get rewritten.
You need to add a .htaccess file inside the public directory, so whenever you build your app, it automatically gets copied to the new dist directory. The content of.htaccess should be like:
RewriteEngine On
RewriteBase /
RewriteRule ^index.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
Add the following before the last line
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.bmp)$
htaccess file to redirect my pages like
site/pageTitle to index.php?id=pt
and to show article from category
site/article/pageTitle
now .htaccess file considering my image folder as catgory directory and images as pageTitle and does not display images
even when I direct url to image it shows article page
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#show urls like site/author/pageno
RewriteRule ^author/(.*)/(.*)$ index.php?author=$1&page=$2 [NC]
RewriteRule ^author/(.*)?$ index.php?author=$1&page=$2 [NC]
#show articles like site/article/articleTagName
RewriteRule ^article/(.*)/(.*)?$ index.php?article=$1 [NC]
RewriteRule ^article/(.*)?$ index.php?article=$1 [NC]
#direst article site/articleTagName
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?article=$1 [L,QSA]
#set image folder and user folder to real from virtual
RewriteRule ^(.*)/images/$ /images/
Please replace your last line with
RewriteRule ^(.*)/images/(.*)$ images/$2
I was facing the same problem but I got a hint from your coding and changed the code a little bit; now it's working fine.