.htaccess rewrite index GET request - .htaccess

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 /

Related

Redirect url contains both numbers and letters with Htaccess

Im trying to Rewrite my url with this htaccess code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
ErrorDocument 404 mysite
RewriteRule ^([a-z-]+)$ /name.php?n=$1 [NC]
RewriteRule ^([0-9]+)$ /age.php?e=$1 [NC]
</IfModule>
when i tried open link mysite/abc or mysite/20. Both of them are working but if try this mysite/abc123 or mysite.com/a2b2c the page show error message. This page isn’t working
How can i redirect those link mysite/abc123 , mysite/a2b2c to my home page mysite with htaccess ?
Thank you!
You may try these rules:
ErrorDocument 404 /
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteRule ^([a-z-]+)/?$ name.php?n=$1 [L,QSA,NC]
RewriteRule ^([0-9]+)/?$ age.php?e=$1 [L,QSA,NC]
ErrorDocument 404 / will forward all not found URLs to home page without changing the URL in browser. If you want to redirect those 404 URLs and want to change them to home (landing) page then use:
ErrorDocument 404 https://example.com/

.htacess to perform rewrite if file does not exist

I have two separate .htaccess files that I'd like to be fused together so that the first rewrite always takes precedence, which redirects some traffic to https base which domain. Then if the file at the url does not exist, then it sends the traffic to a php file. It is a url shortener, but if that returns at 404, then it shows a 404 error page.
Here are the bits and pieces of the .htaccess files:
This, below, I believe should redirect all http traffic except kore.tt and korett.com to https:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)?(kore\.tt|korett\.com) [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
This is something from the url shortener that is supposed to send traffic that doesn't exist to loader.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /loader.php [L]
</IfModule>
But then if that returns a 404 error. Then this is the simple 404 error catch.
ErrorDocument 404 /404.html
You can use a condition in your htaccess. There are many more ways to create rules. See if this can work for you.
#check to see if loader.php exists on the filesystem, then do rewrite
<If "-f %{DOCUMENT_ROOT} . '/loader.php'">
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /loader.php [L]
</If>
#otherwise redirect to 404 page
<Else>
RewriteEngine On
RewriteRule ^ /404.html [R=404,L]
</Else>

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

.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();
}

Exception with .htaccess (image broken)

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 :-).

Resources