Domain repeated twice in 404 error page when using htaccess - apache

I am developing a .htaccess file for my site to clean up the urls. I am trying to rename my contact page to make sure its working but I keep getting a 404 error page saying that my url missing. The .htaccess files appears to be working fine but my domain is being repeated in the 404 error page. Here is my .htaccess file so far.
# Turn Rewrite Engine On
RewriteEngine on
# Rewrite for contact.php
RewriteRule ^message$ contact.php [NC,L]
Here is the 404 error
The requested URL /domain.com/domain.com/contact.php was not found
on this server.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
My .htaccess file is in the root of my site. It is probably a simple problem but I have been trying all the solutions I could find but to no avail.

What happens is, you are using relative links when rewriting URL. This causes the server to look for /domain.com/domain.com/contact.php.
Use absolute URL in rewrite:
RewriteRule ^message$ /contact.php [NC,L]

Related

.htaccess RewriteRule not working in different host

My Current .htaccess rule's example
RewriteRule ^([^/\.]+)-([0-9]+) my_article.php?article_permalink=$1&article_id=$2 [NC,L]
ErrorDocument 404 /404errorpage.php
Output:
https://example.com/my-article-example-1
In my localhost and current host these rules working fine. But in other host
10 day ago when I had changed my hosting the first rules was not working. it's showing 404 page (returns 404).
Today in new host the second rule ErrorDocument not working. It's showing
Not Found
The requested URL was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
Expected output:
for any 404 error show 404errorpage page
Now I can't understand what is the problem.
With your shown samples, please try following htaccess Rules. Please make sure your htaccess rules file, my_articile.php file both are in same folder(root folder). Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteRule ^(.*)-(\d+)/?$ /my_article.php?article_permalink=$1&article_id=$2 [QSA,NC,L]
ErrorDocument 404 /404errorpage.php

.htaccess issue when including custom 404 error page

This is my first time using .htaccess and currently my .htaccess file is redirecting to my custom 404 page. This is being handled with the following line in my .htaccess file:
ErrorDocument 404 /404.html
On top of this, I need to rewrite a few urls so that /addition/ points to /includes/addition.html (this is one example). So I add the following:
RewriteEngine On
RewriteRule /addition/ /includes/addition.html
But this then serves me a 500 error when Is hold be getting 404 error. On top of this, when I point to mysite.com/addition/ the browser isn't fetching addition.html from my includes folder.
Would someone please explain to me how to have these two rules working without effecting the other, and correct my secondary rewrite rule?
Danke.
Have it this way in your site root .htaccess:
ErrorDocument 404 /404.html
RewriteEngine On
RewriteRule ^/?([a-z]+)/?$ /includes/$1.html [L,NC]

Loading site's 404 page instead of redirecting to it based on URL starting part

i'm trying to figure out how to load a chosen page (a 404 page) on the condition that the URL starts with img/galery/. We changed our image repositiory when we changed an eshop backend solution and a lot of crawlers are still trying to index our old images. Like an year+ old URLs. There's no way we could map old img URLs onto the new ones so i want to force a 404 there, because there are some htaccess rules that make those URLs a 301 and the crawler may think those images still exists, but they don't.
So basicly what i want to do is for any URL that starts with img/galery/ to load our site's /404/ page with no redirect. I just want to skip any redirect or any URL manipulation whatsoever so the crawers are given clear answer. I've tried hard, but as usual htaccess is really just black magic to me:
RewriteEngine On
# does not load the site's 404 page, just shows a plain (apache?) 404 page, but reacts on any URL starting with img/galery/
RewriteRule ^img/galery/ 404/ [R=404,NC,L]
# redirects to 404 page, but that's not what i wanna do
RewriteRule ^img/galery/ 404/ [R=301,NC,L]
# also redirects
RewriteRule ^img/galery/ 404/ [R,NC,L]
# does nothing
RewriteRule ^img/galery/ 404/ [NC,L]
# does nothing
RewriteCond %{REQUEST_URI} ^img/galery [NC]
RewriteRule ^.*$ 404/ [L]
I've tried many solutions from SO or other sites, none is working. What am i missing here? Thx!
It seems it's not possible to use RewriteRule to load dynamic 404/ page, so i made new 404.php file that imitates its mechanics.
I haven't found any other more direct solution than this, but maybe it will help somebody with the same struggle i had.

How to rewrite to nonexisting directory

On my webpage I've an Joomla Installation in /joomla, now I want to move the complete /joomla directory to / but old links with the joomla inside should be redericted to the new site at /. How is this possible?
In my .htaccess I've tried some stuff from somewhere out of the Internet, but the rules doesn't work, throws everytime an 404 HTTP Error.
Place this rule as your very first rule in /joomla/.htaccess:
RewriteEngine On
RewriteRule ^ / [L,R=301]

.htaccess modRewrite - find out the underlying url that is trying to be resolved?

I am having trouble with an htaccess file that works on my local testing setup but not when uploaded to my production server, even though I have other sites on that server which have working .htaccess setups.
Is there any way (perhaps with a custom 404 page) that I can find out the url that .htaccess has re-written to, so that I can debug the problem?
what is your trouble?
get you an internal server error or didn't execute any of your rules?
a shoot in the dark ... if your rules doesn't match it could be that you have something like this in your .htaccess
RewriteRule /home/ /index.php?s=1&l=1 [L]
... then try the change in
RewriteRule home /index.php?s=1&l=1 [L]
without the leading slash "/"