Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the site root host name dynamically in .htaccess

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?

like image 392
Sourabh Kumar Sharma Avatar asked Jun 07 '15 12:06

Sourabh Kumar Sharma


People also ask

How to create a htaccess file for my website?

If there is no .htaccess file in the public_html folder you can easily create one using cPanel->File Manager. Then, type your domain name in a browser and you should see the website which is in your sub folder.

Can I put an htaccess file in a subdirectory?

However, you can also put an .htaccess file in a particular subdirectory farther down in your site’s structure. This is useful if you only want to affect a portion of your website, as the file will only affect the directory it’s in and those beneath it.

Where is the htaccess file located in WordPress?

An.htaccess file is most often located in the root of the website it controls. From there, it will affect all subfolders in the site. However, you can also put an.htaccess file in a particular subdirectory farther down in your site’s structure.

How to add custom configuration rules to the htaccess file?

Locate .htaccess file, right-click >> Edit: 5. If there is no .htaccess file located in your File manager, feel free to create a new one using File option: You are ready to add your own configuration rules and save them.


2 Answers

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

like image 66
splash58 Avatar answered Sep 19 '22 16:09

splash58


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>
like image 28
Mike Rockétt Avatar answered Sep 20 '22 16:09

Mike Rockétt