php htaccess redirect 404 error page - php

I am building a LAMP application www.testing.com and I have an image url www.testing.com/image/folderA/a.jpg.
I want to have a rewrite rule in .htacess such that if user navigates to images that do not exist in /image/foldera, he will be redirected to another image with the url www.testing.com/image/folderB/b.jpg.
This is my code.
RewriteRule ^image/folderA/(.*\.(jpg|gif|png))$ http://%{SERVER_NAME}/image/folderB/b.jpg [R=404,L]
However, this is not working as www.testing.com/image/folderA/a.jpg, which is supposed to return an image, gives an 404. Any idea why?

When using the R flag you need to keep the following in mind:
However, if a status code is outside the redirect range (300-399) then the substitution string is dropped entirely, and rewriting is stopped as if the L were used.
http://httpd.apache.org/docs/trunk/rewrite/flags.html#flag_r
This means that your rewrite rule is dropping the substition string and redirecting all requests to specified files to the built-in 404 page.

Related

Redirect a image URL request to another without changing the URL

After the vulnerability audit on my site, I found a wrong image having an extension like ABC.php.jpg
I have renamed it to ABC.jpg
Now the problem is, around 100k mails already sent and we keep the log (by saving the whole content in DB) of the mail and provide whenever required to a higher authority (By trigger mail with content which is in DB).
So I can't go in every mail and change the URL. What I want is, to access the URL (ABC.jpg) with URL ABC.php.jpg.
I have implemented some rules with htaccess file but they are simply redirecting the URL, and with this, image in mails will not load.
I guess you have the redirect flag in your mod_rewrite rules [R].
As in:
RewriteRule ^ABC.php.jpg $1/ABC.jpg [R=301,L]
Just drop that flag and it should load the content without the redirection.
RewriteRule ^ABC.php.jpg $1/ABC.jpg [L]
If you want the script to load image, you can do in your ABC.jpg.php:
<?php
echo file_get_contents("path/to/abc.jpg");
Note that you can't use the extension .jpg, if you want the server to execute your script. So it should end with .php
So in your image tag you could use this as URL:
<img src="http://host/ABC.jpg.php" />
Hope that helps
As per jeprubio's answer, I have found that we generally use this htaccess rule.
RewriteRule ^ABC.php.jpg $1/images/ABC.jpg [R=301,L]
in which R uses for Redirect and L is for to stop further processing the ruleset, So basically if we remove R rule from rule, it won't redirect to new URL.
Instead of it, it will serve response from new URL while hitting the old URL, So the final rule will be like this.
RewriteRule ^ABC.php.jpg $1/images/ABC.jpg [L]

RewriteRule for blog posts with a subdomain in the URL

working on a writing a redirect rule but I'm not too familiar with them or with the syntax.
Basically I have Wordpress URLs that look like this:
https://example.com/blog/blog-title/garbagetext
I need to be able to redirect URLs like this to our 404 page, and my first attempt was with:
RewriteRule ^blog/(.`*)/(.`*)/$ https://www.example.com/404.php [R=301,L]
This worked, but it also made it impossible to access actual blog posts, and is also redirecting URLs that look like this:
example.com/blog/blog-title
Any ideas how to make this work properly?
RewriteRule ^blog/(.`*)/(.`*)/$ https://www.example.com/404.php [R=301,L]
:
example.com/blog/blog-title
The directive you posted could not redirect the example URL that you say is being erroneously redirected. So, either you have other directives (possibly in other .htaccess files, or the server config) or there is a conflict with existing directives, or you are seeing a cached response? If you've previously experimented with 301 redirects then these will have been cached aggressively by the browser. (It can be easier to test with 302 - temporary - redirects for this reason.)
Since you are using WordPress (which uses a front controller) then any redirects should appear at the start of the file, before the front controller.
You also appear to have an erroneous slash at the end of the RewriteRule pattern that will fail to match an intended URL of the form /blog/blog-title/garbagetext.
I'll ignore the backticks for now - I assume they are probably just a styling typo in your question?
I need to be able to redirect URLs like this to our 404 page
You shouldn't be redirecting to a 404 page. It should be served with an internal subrequest instead. If you redirect then you first send a 3xx response back to the client, the client then issues a second request for your 404.php page (exposing the URL) and then you have to make sure you are manually setting the appropriate 404 HTTP response header.
So, you need to define the appropriate ErrorDocument and R=404 the response.
The posts that need to be redirected to the 404 are any posts that have text following that trailing slash, so far example: example.com/blog/customer-reviews/asdfa.
Try the following instead, at the top of your .htaccess file:
ErrorDocument 404 /404.php
RewriteRule ^blog/.+/. - [R=404]
This will match /blog/customer-reviews/asdfa, but it won't match /blog/customer-reviews/ (or /blog/customer-reviews). The R=404 flag triggers an internal request to the defined error document. The L flag is not required when using a none 3xx code. When there is no substitution text (ie. no target URL) then specify a hyphen (-) instead.
And make sure you've cleared your browser cache.

Display the 404 page in a multi-language website

I created a 404.php page for a website. Also, there is the .htaccess file ( in the /root ) having the ErrorDocument 404 /404.php line.
The website is having a multi-language functionality, something like this:
sitename.com/it/article1
sitename.com/en/article1
and so on ... There are numerous articles.
The 404.php page appears when I'm trying to access something like sitename.com/adsdasaerera but it doesn't appear when I'm trying to access sitename.com/en/adsdasaerera, adsdasaerera not being, obviously, an existing article.
How can I achieve this?
Ideally you would have one error 404 document page. On that page you would first set a default locale of en, and then determine the users browser locale/language they are using, and proceed to the next step which is displaying the error page in that language.
Since we are talking PHP, here is an answer how to detect the locale in PHP.
Simplest way to detect client locale in PHP
From there, the next step is basically using the strings associated for that locale from an array or a file and displaying them.
On this page you could also have some links to other translations that could be viewed(hidden elements that show div on click), if the user doesn't wish to read the error in the language their browser is set to use.
Did you try giving it a full path?
ex ErrorDocument 404 /site/error/404.html
Have you tried put the line ErrorDocument 404 /404.php into the apache global config file, maybe in /etc/httpd/.
Or is there an exising .htaccess file under somesite.com/en/ folder?
how do you archieve the multi-language functionality? rewrite in apache or .htaccess? and what does your PHP code?
in addition to Branimir Đureks answer, you may analyze the $_SERVER['REQUEST_URI'] string i.e. using explode() and if the article dosn't exists in the choosen language, send header("HTTP/1.0 404 Not Found"); otherwise display the article.
If you are NOT using a CMS etc, so the Language Folders are really existing, you can use this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(ie|en|de)/ /404.php/$1 [R=404,L]
Explanation
The three conditions check that the requested file or folder does not exist
The rule checks that the requested url starts with one of the three countries then a /, captureing the country code to Group 1
It redirects to /404.php?lang=$1, e.g. /404.php?lang=en with a 404 code
In that 404.php you just need to Use $_GET['lang'] to get the requested Langugage.
But you already said, that the multi-language functionality is archieved in .htaccess. It would be helpfull to know whats in that file.
Anyway: If all Requests get redirected to one page (e.g. index.php), somethere in that file the Content of the site gets included. That hapens either with an include or similar of a file or with a Query to the Database.
Thats the point there you need to expand your code. If that file isn't found or if there is no record in the Database, you need to include your 404 File.
Please use this code at the bottom of .htaccess. Replace Path with your path.
RewriteRule ^pagenotfound$ 404.html
ErrorDocument 404 path/pagenotfound
It doesn't redirect you to the 404.php file because you accessed the existing file. Look at this URL
sitename.com/en/adsdasaerera
"adsdasaerera" is probably only a parameter value that is rewrited by htaccess. It's not a file. I suppose you have a "en" folder and index.php in it. So when you enter url above, you access that index.php in "en" folder with parameter "adsdasaerera" and that's the reason why you don't get 404 error. You can solve it by adding little code that searches trough the database for "adsdasaerera" and if it doesn't exist, in your code manually redirect it to 404.php file.
The reason why you get 404.php on this url
sitename.com/adsdasaerera
is because you don't have file named "adsdasaerera".
Hope it will help you :)
It should be possible to define an ErrorDocument like this in htaccess file:
ErrorDocument 404 http://sitename.com/404.php
and then do a language specific redirect check for requests in 404.php file.

htaccess url rewrite but executes the same page

i have an url like
http://example.com/folder1/folder2/admin_login.php
the above is active page. but i want to show the below url instead of above but functioning the same page.
http://example.com/folder1/folder2/login
So the question is display url-2 in browser but exeuctes url-1
i have tried this code in htaccess file
RewriteEngine on
RewriteRule ^folder1/folder2/admin_login.php$ http://example.com/folder1/folder2/login [NC,L]
When i put url-1 in browser address bar. it will convert into the url-2 but shows Page Not Found
You have it turned around. You want the second url to be in the address bar. That means you have to link to that url. When a request with that url comes to the server, the server has to internally translate that url to a working url. Besides that, you should not have the full url as a rewrite target, because this causes the rule to function as a redirect instead of an internal rewrite.
The following rule should do the trick:
RewriteRule ^folder1/folder2/login/?$ /folder1/folder2/admin_login.php [L]

htaccess redirect url with var from no file extension to file extension

the problem is that for some reason googlebot gets a bunch of error 404s when crawling a certain page but everything works fine on my end, I think...
using htaccess, I have rewritten a page with vars for seo purposes, so:
RewriteRule ^feeds/([^/]*)\.xml/?$ /rss/feeds.php?cat=$1 [QSA,NC,L]
so I get www.url.com/feeds/category.xml
instead of
www.url.com/feeds.php?cat=category
I've also ajusted all urls on the page to follow this rule and like I said everything works fine on my end... googlebot for some reason only sees www.url.com/feeds/category and does see the .xml extention.
not sure if it would work but I was hoping to put a rule in htaccess to redirect www.url.com/feeds/category to www.url.com/feeds/category.xml in the even that this happened, but was unsure of how to do so.
Maybe try
RewriteRule ^feeds/([^/]+)/?$ /feeds/$1.xml [R,NC]
Which would redirect www.url.com/feeds/category to www.url.com/feeds/category.xml, using a HTTP 302 Redirect. Remove the R flag if you don't want it do use the HTTP 302 Redirect, but that would make www.url.com/feeds/category a valid URL as far as google is concerned. Just in case: mod_rewrite.

Categories