Exception with .htaccess (image broken) - .htaccess

I'm using:
RewriteRule ^(.*)$ index.php?abrir=$1
RewriteRule ^(.*)/(.*)/(.*)$ index.php?abrir=$1&id=$2&pagina=$3
and I have the image link:
/data_livro/capa_livro/af58e898f91509b.jpg
How I can add a exception for show the image? Its broke...
My full .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ index.php?abrir=$1
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?abrir=$1&id=$2&pagina=$3
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
ErrorDocument 400 index.php
ErrorDocument 401 index.php
ErrorDocument 403 index.php
ErrorDocument 404 index.php
ErrorDocument 504 index.php
ErrorDocument 505 index.php
DirectoryIndex index.php

If I'm understanding this correctly, ading the following ahead of your RewriteRules should solve the problem:
RewriteCond %{REQUEST_FILENAME} !-f
However, I'd also suggest modifying your RewriteRules to look like:
RewriteRule ^([^/]+)$ index.php?abrir=$1
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?abrir=$1&id=$2&pagina=$3

I find that using .* in a rewrite rule is too general. I tend to use more specific character classes:
RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?abrir=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?abrir=$1&id=$2&pagina=$3
This doesn't rewrite any request with a dot in it, so specific file names (/styles/my.css, /images/example.jpg) get ignored. Also, I like to add /? to allow an optional trailing slash, but that's a personal preference :-).

Related

Rewrite URL after redirecting 404 error in htaccess

How to redirect 404 , in htaccess I have add this
ErrorDocument 404 http://example.com/404/
ErrorDocument 500 http://example.com/500/
# or map them to one error document:
# ErrorDocument 404 /pages/errors/error_redirect.php
# ErrorDocument 500 /pages/errors/error_redirect.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /pages/errors/404.php [L]
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /pages/errors/500.php [L]
# or map them to one error document:
#RewriteCond %{REQUEST_URI} ^/404/$ [OR]
#RewriteCond %{REQUEST_URI} ^/500/$
#RewriteRule ^(.*)$ /pages/errors/error_redirect.php [L]
It has reduce but they are increasing, is there a way how can I redirect properly or maybe I'm mising something. How can I fix all of them ?
Thanks
You dont need to use RewriteRules. You can simply use an absolute path instead of using full url as your errordocument target :
ErrorDocument 404 /pages/errors/404.php

RewriteRule - Give access to folder

I use RewriteRule to shorten the links from ?s=xxx to /xxx - but I have some problems that I cant access files in some folders. To help with that I have make an exception in the rule as:
RewriteRule ^(inv.*) $1 [L]
This because I have .pdf files in the folder "inv".
But the problem is that I also have some sites that is called /invitation. The problem is that this link gives 404 error.
If I remowe the line above, the /invitation works - but then I get 404 error on the /inv/xx.pdf.
How can I change the RewriteRule that makes both works.
Thanks.
This is my entire .htaccess file:
DirectoryIndex i.php index.php index.html
RewriteEngine On
RewriteRule ^robots.txt - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule ^(inv.*) $1 [L]
RewriteRule ^([^/]+)/?$ /?s=$1 [NC,L]
RewriteRule ^(nyhet)/([^/]+)/?$ /?s=$1&n=$2 [NC,L]
RewriteRule ^([^/]+)/([^/]+)/?$ /?s=$1_$2 [NC,L]
ErrorDocument 404 http://www.domain.com/404
ErrorDocument 403 http://www.domain.com/404
disable reportory contenu by
Options All -Indexes
then fix rules of files
<Files ~ "([.][^][*])^.*\.">
order allow,deny
deny from all
satisfy all
</Files>

.htaccess rewrite index GET request

So, I want to rewrite my url from this:
http://example.com/l1
to
http://example.com/?loc=l1
Currently I have this:
RewriteEngine On
RewriteRule ^/(.*)$ ./?loc=$1 [NC,L]
ErrorDocument 404 /errors/404.php
But each time I type http://example.com/l1 it gives me a 404 error! Please help!
Use this .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /?loc=$1 [NC,L]
ErrorDocument 404 /errors/404.php
I do not rewrite the links to existing files with !-f.
And URI in RewriteRule does not begin with a /

ErrorDocument 404 not working with routing

I tried using this code on .htacces to have my own custom 404 on my page,.
ErrorDocument 404 http://mysite.com/404.shtml
when i visit the page that does not exist on the site. it must show something like this
but this is the one it shows
here is my complete htaccess
<files .htaccess>
order allow,deny
deny from all
</files>
php_value memory_limit 170M
RewriteEngine on
Options +FollowSymlinks
Options -Indexes
RewriteCond %{REQUEST_URI} (.+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
AddDefaultCharset utf-8
RewriteRule (.*) index.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.mysite.com.*$ [NC]
RewriteRule ^(.*)$ http://mysite.com%{REQUEST_URI} [L,R=301]
ErrorDocument 404 http://mysite.com/404.shtml
The location of my htaccess is located along with the 404.shtml and the index.php of the site
Removing this code would fix the problem. How can i make it work with routing.
RewriteCond %{REQUEST_URI} (.+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
AddDefaultCharset utf-8
RewriteRule (.*) index.php
Instead of referencing the entire url with ErrorDocument 404 http://mysite.com/404.shtml, name the document relative to your web root. In this case, /404.shtml.
Use these codes in your .htaccess for capture 404 error pages.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.html [L]
</IfModule>
Now your all 404 requests will goto /404.html. You can also use /404.php to code more dynamically. Also make sure that rewrite module is enabled in your apache.

.htaccess redirect of index.php is breaking 404 pages

I have recently setup a index.php redirect through .htaccess. The idea here is to negate duplicate content issue that crops up when a site has both an index.php and / (homapage) getting indexed.
Here is my original .htaccess
Options -Indexes
ErrorDocument 403 /customerrors/403.html
ErrorDocument 401 /customerrors/401.html
ErrorDocument 400 /customerrors/400.html
ErrorDocument 500 /customerrors/500.html
ErrorDocument 404 /customerrors/404.html
Pretty basic.
I used the technique listed here to redirect the index.php to /.
http://www.askapache.com/htaccess/redirect-index-blog-root.html
It works great too. One issue is, it breaks the 404 pages.
This is the modified .htaccess that is breaking the 404 pages.
RewriteEngine On
RewriteBase /
DirectoryIndex index.php
RewriteCond %{http_host} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://example.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Options -Indexes
ErrorDocument 403 /customerrors/403.html
ErrorDocument 401 /customerrors/401.html
ErrorDocument 400 /customerrors/400.html
ErrorDocument 500 /customerrors/500.html
ErrorDocument 404 /customerrors/404.html
So if a user types in or goes to www.example.com/dafjkadbfda instead of serving 404 page, the URL remains the same(in this case the broken one) and severs the index.php page.
This in turn is opening another can of worms. All those broken pages are coming up as duplicate content and meta.
Is there another way to write the .htacess redirect that will take into account the 404 pages? Seems like that is the conflict here.
Thanks in advance.
This part of the .htaccess is the one that "breaks" the 404:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
You could manage the problem by sending a 404 error from your PHP script:
if ($not_a_valid_page){
header("$_SERVER[SERVER_PROTOCOL] 404 Not Found");
readfile("./customerrors/404.html");
die();
}

Resources