Get the site root host name dynamically in .htaccess - php

I am trying to make 404, 500 etc pages in ErrorDocument in php .htaccess file, it works fine if give
ErrorDocument 404 http://localhost/project/errordocs/404.html
but i do not want to hard code the url here, instead i want to get the root url site name dynamically so that i do not have change it again and again as the hostname changes.
Basically i want to get this root url like: http://localhost/project can change to http://www.example1.com/project or http://www.example2.com/project etc. This url must come from projects root folder.
So that will dynamically become:
ErrorDocument 404 http://localhost/project/errordocs/404.html
ErrorDocument 404 http://www.example1.com/project/errordocs/404.html
ErrorDocument 404 http://www.example2.com/project/errordocs/404.html
Any help please?

The asker asks incorrect. All what he writes
ErrorDocument 404 http://localhost/project/errordocs/404.html
ErrorDocument 404 http://www.example1.com/project/errordocs/404.html
ErrorDocument 404 http://www.example2.com/project/errordocs/404.html
may be done by
ErrorDocument 404 /project/errordocs/404.html
But really he want: while moving site from project folder to project1, he should not change the rule
I think it may be done by htacces placed in /project with code
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ errordocs/404.html [L]
It will work if AllowOverride set to All (it is default).
The problem only that responce in this case will be 200 but not 404

Based on the conversation you had, you would like to not have to change the path to the error document when you move your project to another folder with a different name.
Firstly, you cannot use variables when using ErrorDocument. The path that you provide must be static. The path that you specify must be either to an external URL (in which case your browser will be redirected) or to a file relative to your document root (i.e. localhost).
Unfortunately, ErrorDocument won't be able to find the file relative to the current directory (i.e. project). The only logical way of doing this would be to remove the leading slash, but this would cause Apache to render that as a string in the browser.
This brings us to the only other possible solution: mod_rewrite. The only problem with using it, however, is that it is processed early on in the mapping pipeline which may allow other modules (such as mod_proxy) to affect the process.
That said, you can try with the following:
/project/.htaccess:
RewriteEngine on
# Determine if the request does not match an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If so, send the request to the applicable file, relative to this directory
RewriteRule ^ errordocs/404.php [L]
# Per your comment and suggested edit, add the following.
# Note: This should not make any difference as mod_rewrite and PHP should
# already handle the error document.
ErrorDocument 404 /errordocs/404.php
/project/errordocs/404.php:
This file will send the 404 header as .htaccess won't be able to do that.
<?php header("HTTP/1.0 404 Not Found"); ?>
<h1>Sorry, we couldn't find that...</h1>
<p>The thing you've requested doesn't exist here. Perhaps it flew away?</p>

Related

How can i redirect unstructured link like (https://example.com/digital-marketing.php/ercbierubcrei) using .htaccess?

There is a issue of unstructured link like (https://example.com/digital-marketing.php/ercbierubcrei)
How can I redirect its to main root?
My .htaccess not working, I'm using below code in .htaccess:-
RewriteEngine On
ErrorDocument 404 /example.com
There is a issue of unstructured link like (https://example.com/digital-marketing.php/ercbierubcrei)
How can I redirect its to main root?
I assume /digital-marketing.php is a file on your filesystem. In which case /ercbierubcrei is additional pathname information (or path-info). By default, the PHP handler allows path-info, so /digital-marketing.php is still served but at the different URL (if that's what you mean by "unstructured link"?).
You can simply disable path-info on these URLs by setting the following at the top of your .htaccess file:
AcceptPathInfo Off
Now, any URL that contains path-info will trigger a 404 instead and your custom 404 error document will be called. (You can still override this behaviour with mod_rewrite if you wish.)
Alternatively, if you wanted to redirect /digital-marketing.php/ercbierubcrei to /digital-marketing.php (is that what you mean by "redirect its to main root"?*1) then you can do something like the following near the top of your .htaccess file using mod_rewrite:
RewriteEngine On
RewriteRule ^(.+?\.php)/ /$1 [R=302,L]
This redirects a URL of the form /<something>.php/<anything> to /<something.php, effectively discarding the path-info portion of the URL.
(*1 - If by "main root" you do literally mean the document root of the site then that is not recommended. Google would likely treat this as a soft-404 anyway. A custom 404 with a meaningful message would be preferable.)
ErrorDocument 404 /example.com
This does not look "valid". The 2nd argument to the ErrorDocument directive should ideally be a local URL-path to the document that handles the error response.
For example:
ErrorDocument 404 /error-documents/my-404-error-document.php
A simple 301 redirect will do the job.
redirect 301 /old_url http://www.domainroot.com/
use Complete url of your site. Like this
ErrorDocument 404 https://www.yourdomain.com/example.php
Please refer to below .htaccess examples:
Redirect http://yourdomain/digital-marketing.php/apple to http://yourdomain/:
RewriteEngine On
RewriteCond %{REQUEST_URI} /digital-marketing.php/*
RewriteRule ^ / [L,R=301]
Redirect http://yourdomain/digital-marketing.php/orange to http://yourdomain/ref?code=orange:
RewriteEngine On
RewriteCond %{REQUEST_URI} /digital-marketing.php/*
RewriteRule ^digital-marketing.php/(.*)$ /ref?code=$1 [L,R=301]

htaccess not redirecting to custom 404 page

I have a custom 404 page called 404page.php , and I have the htaccess file with the line that I found should work (this is the only line I have in the .htaccess file)
ErrorDocument 404 /404page.php
It was working fine yesterday, but today I was optimizing images and some other stuff on the website, and when I reuploaded it, the htaccess didn't redirect to 404page.php anymore, but to a page that has this written:
Not Found
The requested URL /asfsdtg was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
(I just typed asfsdtg to test if it was working).
The website is uploaded online and I'm managing it through cpanel (I'm still a beginner). I searched for some solutions, I tried adding another line to .htaccess, then it looked like this:
RewriteEngine on
ErrorDocument 404 /404page.php
I even tried putting the location of the 404page.php as a local link, and the internet link, and it still gave me the weird error page.
Does anyone have some idea whats happening? If you need more info that I didn't supply please tell me what more I can supply
Try out this:
RewriteEngine on
ErrorDocument 404 http://yoursitename.com/404page.php
And be sure that 404page.php exists on the root of your server and is trully called 404page.php not 404Page.php
Be carefull at the characters, this is key sensitive!
Try with like this:
RewriteEngine on
ErrorDocument 404 http://www.sitename.com/404.php
Make to handle any 404 errors.
ErrorDocument 404 /404page.php
If above setting does not works you have to write addition line of code as mentions below.
Which identifies for non existing files and directories and redirect to 404.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ - [R=404,L]

htaccess renaming .php files to .html files giving file not found errors?

I'm using this code to rewrite my php files to be html files, so technical.php would appear as technical.html
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php [nc]
but it gives me a 404 error when visiting technical.html
Not Found
The requested URL /technical.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.
Did I get the order wrong in my htaccess?
Probably you'll have to specify the app folder as well:
RewriteRule ^(.*)\.html$ appFolder/$1.php
Adding an .htaccess rule in the subfolder the files were in fixed this! I thought one .htaccess in the root, controlled the entire site.

Redirecting all request to the root directory somewhere else?

Alright, so I've downloaded a CMS for a server I'm setting up and I have some difficulty with the path the files are on.
I have put the CMS in a subdirectory on my server as the root directory is already being used by another CMS. However, the CMS repeatedly uses "/" in the code to link to their files. For example, an image is found using this code:
<img src="/images/banner.png" />
As you know this will not work, because the link above redirects the request to the images folder in the root of the server, not the subdirectory. The CMS also came with a ".htaccess" file, so I immediately thought that I could redirect all requests made using the "/" character to the subdirectory on the server. This is the original ".htaccess" file:
RewriteEngine On
RewriteRule ^index.php(|/)$ content/.php
RewriteRule ^error.php(|/)$ content/error.php
RewriteRule ^housekeeping(|/)$ housekeeping/index.php
RewriteRule ^(|/)$ content/$1.php
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ content/$1.php
RewriteRule ^rd/([^/]+)(|/)$ /rd.php?id=$1
RewriteRule ^quickregister/start(|/)$ /register/start.php
RewriteRule ^quickregister/step2(|/)$ /register/step2.php
RewriteRule ^quickregister/step3(|/)$ /register/complete.php
RewriteRule ^community/online(|/)$ content/online.php
RewriteRule ^community/vip(|/)$ content/vip.php
RewriteRule ^credits/goldvip(|/)$ content/goldvip.php
RewriteRule ^home/(..*)$ content/home.php?u=$1
RewriteRule ^articles/(..*)$ content/articles.php?story=$1
ErrorDocument 403 /content/error.php
ErrorDocument 404 /content/error.php
I thought that by adding
RewriteRule ^/$ /subdirectory/
the requests to "/" would be redirected, but to no avail. I have checked the apache configuration and it seems that overwriting the config using htaccess files is enabled, so if anyone is able to help me out I'd be very happy.
Thanks in advance!
EDIT:
I came real close to a solution. I inserted this code just below "RewriteEngine on":
RewriteRule ^(.*)/(.*)$ /private_servers/strebbohotel/$2
This returned the following error page when visiting the url "http://mysite.com/private_servers/strebbohotel/content/.php":
Not Found
The requested URL /private_servers/strebbohotel/.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.
As you can see, it skips the "content" subdirectory for some reason. I believe it has something to do with the other lines of the .htaccess file but I can't figure out which ones. It also could be that I need a more complex regex, but I'm not particularly good with it so if someone could help me further I'd be thankful.
You have to change the .htaccess file on yout root directory - and that will mess with the other CMS. The only solution that comes to my mind is to use combination of RewriteCond's:
RewriteCond %{HTTP_REFERER} my_better_cms_url_regex
RewriteCond %{REQUEST_URI} ^/images/.*$
RewriteRule ^.*$ /subdirectory/%{REQUEST_URI}
That should do the trick. If you want to customize it, refer to this cheatsheet (or simmilar). But be aware, that .htaccess in root directory is checked against ALL requests made to your subdir cms - therefore the need for second condition.
The .htaccess file in your CMS subdirectory would have no effect on requests made against the server root directory. You would need to add an .htaccess there.
Even then though, I don't know how you would differentiate requests from the CMS (like the image request in your example) from those intended for the application in the web root.
Does your CMS not have any configuration setting that allow you to specify a path the the files other than the web root?
You want to use:
RewriteBase /another_root_dir/
in the beginning of your .htaccess file

Two htaccess files

I have a question about using multiple .htaccess files - I couldn't find the answer to this after looking elsewhere on stackoverflow, so I hope you guys can help.
I currently have one .htaccess file in the root of my site, which performs a simple url rewrite:
Options -MultiViews
# CheckSpelling off
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1 [L]
ErrorDocument 404 /index.php
I'm currently working on the second phase of development of this site, and I've made a replica in a subfolder (e.g. www.abcdef.com/new/). The trouble is, at the moment if I click a link on this replica site, it redirects me to the root, original page, whereas I want it to go to the equivalent page in the new/ folder. I've put another .htaccess file in this new/ folder, which however doesn't have any noticeable effect:
Options -MultiViews
# CheckSpelling off
RewriteEngine On
RewriteBase /new/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /new/index.php?url=$1 [L]
ErrorDocument 404 /index.php
So my question is: is it permissible to have another .htaccess file in a subfolder like this? And if so, why aren't the above lines working?
Thanks in advance for any ideas on this!
It's possible to have multiple .htaccess files, and the system is designed to work the way you want it to.
You're setting RewriteBase, which explicitly sets the base URL-path (not filesystem directory path!) for per-directory rewrites.
So it seems like your requests would be rewritten to /new/new/index.php, a path and directory which probably doesn't exist on your filesystem (thus not meeting your RewriteConds) and such is being redirected to your /index.php 404.
As a test, perhaps try changing the ErrorDocument to:
ErrorDocument 404 /new/index.php
If you see rewritten calls go to this then it might indeed be your RewriteBase.
You say
The trouble is, at the moment if I click a link on this replica site,
it redirects me to the root, original page, whereas I want it to go to
the equivalent page in the new/ folder.
Could it be that you are using absolute links in your pages and not relative ones? For instance if a link looks like "/sample", when in your main site it will link to http://.../sample and the same is true if the link is inside a page under "/new/". If you'd use just "sample" then that would resolve as http://..../sample or http://...../new/sample, depending on the URL of the page.
Having a second htaccess file in a subdirectory shouldn't be an issue, and as far as I can tell, your two look okay.
Are you sure the links in the site are correct? (ex, they are /new/foo, not just /foo)?

Categories