Making Apache2 loading index.html regardless which file the user targets - .htaccess

At the moment if I access my https://domain.tld it shows my index.html.
Now I want that regardless what file is accessed it should always load index.html .
So if you type in https://domain.tld/hellostackoverflow it should display index.html but not redirect since it would change the url.
Every answer to similar questions on stackoverflow suggests redirecting everything to index.html but that is not what I am looking for.

So basically you want the page to change but the URL to remain the same. There is a similar case which can be found here

Related

Redirecting using .htaccess

I've got an odd redirect request, and I'm not sure if it is possible.
On the server we have a series of PHP files with a name similar to /title-of-the-page-####.php, where #### is an unique integer ID of the page.
I want to be able to redirect users from /#### to the full title of the page (as listed above). Is this possible from within an .htaccess file, and if so, how? (I would like to avoid listing all of the pages from within the .htaccess file)

Making the Document Root dynamic

I have been searching and looking for the answer to this for SO long now without any joy, I hope someone can help.
Okay I have a web structure of:
Main folder - This is where you first land prior to logging in. After you log in you will be either directed to a number of websites that are resident in a sub-folder called websites. The problem I have is that I would like the document root, say the $_SERVER['DOCUMENT_ROOT'] variable, to default to the root of the website folder that it has been directed to. You see I have a lot of $_SERVER['DOCUMENT_ROOT'] in my php code and I use the '/' slash a lot in the href on my HTML tags.
I have seen that I could accomplish this by adding a .htaccess in the root of eiach website folder but 1, would like work for my situation and 2, could anyone please help me with the code. I understand I can use the RewriteRule command but I can't get my head around it. My folder stricture is like this:
login (index.html)
websites folder
website1 (index.html)
website2 (index.html)
website3 (index.html)
I have tried many versions of using RewriteCond etc but non of them work.
If anyone can help me out here I'd be very grateful.
Thanks
Nothing you can do in your htaccess file that's going to be able to change the document root. One thing that you can do is create separate domains that you can make their document roots point to each of the websites folders. Then proxy to one of those domains.
That way the URL on the browser doesn't change domain names, and internally, you're proxying requests back to yourself and the individual website domains will each have their own document root.

PHP, MVC: Rewrite rule for index page

I'm trying to make the index page to respect the rewrite rule defined in .htaccess (or to rewrite the path in index.php with the default route defined in my routes file : rewrite.php).
Why, you may ask? I'm creating a kind of MVC project in PHP (I said 'kind of', because I do not use a Framework like Zend, I just implemented the MVC idea into pure php). So, instead of pages I have views. I do not have an index.php file in my project (nor index.html... actually no index file at all). I'm rewriting all url-paths with the help of .htaccess file and then I use the controllers further to manipulate the model or display in views.
Why not using an index.php page, you may then ask? Like I said, I'm using controllers to do stuffs. By using an index page I do not have the ability to manipulate models and views via controllers, because the physical pages have priority and the url rewriting rules are ignored. So, if someone goes to root page (http://www.domain.com/) the server will automatically display the index page as it is and I do not want to duplicate the logic of first page (getting and displaying data), just for using an index page.
Maybe this is stupid, but I've tried to change the default directory page (DirectoryIndex in .htaccess) to nothing (didn't work, as I expected). :)
That being said, I've excluded the index page. It works well, it shows data like it should do, but at a closer look, I see the browser actually receive '403 Forbidden' on the root page.
In ASP.NET MVC the existence of Default.aspx is also mandatory (for the same reasons, I think). But in the Default page the http context path is rewritten with the default route defined in Global.asax. The question is how do I do that in PHP? (or any other suggestion is welcomed)
You can see the page that I'm talking about here : www.clubclio.eu (as you can see, the data is well displayed, yet you receive 403 Forbidden).
I resolved this by renaming the rewrite.php file (the route file defined in .htaccess) to index.php
So, to answer my own question but to help others too, make sure you have the route file defined as the index file (name it index.php or define DirectoryIndex in .htacctess as the name of your route file).
Maybe this is obvious for others. :)

single-page application with clean URLs without .htaccess file?

My question pertains specifically to the two pages below, but is also more generally relating to methods for using clean URLs without an .htaccess file.
http://www.decitectural.com/
and
http://www.decitectural.com/about/
The pages above are hosted on Amazon's S3, which does not allow for the use of htaccess files. As a result, I have found no easy way to create a clean url rewrite scheme that sends all requests to an index file which, in turn, interprets the URL using javascript and loads up the correct page (with AJAX, or, as is the case with decitectural, with simple div visibility toggling).
In order to circumvent this problem, I usually edit the amazon S3 bucket properties and set both the index page and the error page to the index.html file. In this case, the index.html file is served even when an invalid path (such as /about/) is requested. This has, for the most part, been a functioning solution... That is, until I realized that I was also getting a 404 with the index.html page which would stop Google from indexing it.
This has led me to seek out an alternative solution to this problem. Currently, as a temporary fix, I am actually creating the /about/ directory on the server with a duplicate of the index.html file in it. This works, but obviously is not a real solution to the problem.
I would appreciate any advice on how to set up a clean URL routing scheme on S3 or in any instance where an .htaccess file can't be used.
Here's a few solutions: Pretty URLs without mod_rewrite, without .htaccess
Also, I guess you can run a script to create the files dynamically from an array or database so it generates all your URLs:
/index.html
/about/index.html
/contact/index.html
...
And hook the script on every edit, in a cron or run manually. Not the best in terms of performance but hey, it should work.
I think you are going about it the wrong way. S3 gives you complete control of the page structure of your site. If you want your link to be "/about", just upload a file called "about", and you're done. (Set the headers so that the browser knows it's HTML.)
Yes, it will break if someone links to "/about/" or "/about.html". But pretty much any site will break if you mess with their links in odd ways. You will have to be vigilant when linking to your own site, because you won't have any rewrite rules to clean up for you. But you should have automation doing that.

How does Concrete5 arrange it's absolute paths?

I've been asked to figure out how the Concrete5 system works for an employer, and I can't figure something out.
I have Concrete5 installed to a directory on the server called /realprofessionals. When the Concrete5 system makes new pages, it gives them their own absolute paths, for instance:
http://www.wmcpartners.com/realprofessionals/footer
However, it hasn't actually made a folder in the /realprofessionals directory called footer. So how does that work? How can http://www.wmcpartners.com/realprofessionals/footer be a working link?
Short answer: All page requests are actually going through the one and only index.php file. Page content is stored in the database, not in files on the server.
Long answer:
Concrete5 (and most PHP-based CMS's for that matter) work like this: all requests are routed through the index.php file. This routing is enforced with some mod_rewrite rules in the .htaccess file. The rules say "for any request, don't actually go to that page, but instead go to index.php and pass the rest of the requested path as $_GET parameters". Then in the index.php code (or some other code that is included by the index.php file), the requested page is determined based on the path that was put into the $_GET parameters by Apache (as per the mod_rewrite rule in .htaccess), and the appropriate content is retrieved from the database.
Storing content in the database as opposed to files on the server has several advantages. For example, you can re-use the same html template -- header, footer, sidebar -- on every page, and if you change the template it will automatically be reflected on all pages it's used on. Also, it makes it easier to shuffle pages around and to give them whatever URL you want (e.g. no ".php" extension at the end, or /2010/11/date/based/paths/for/blog/posts).
The disadvantage of course is that every request requires many database queries, but for most sites (those without zillions of page views), the trade-off is well worth it (and various types of caching can help reduce the performance hit).
Jordan's answer is excellent, I would add that you probably don't see index.php in the url because you've enabled pretty URLs (type 'pretty' on concrete5's searchbox to check that).
Anyhow, the best way to programmatically add link to internal pages is:
<a href="<?=$this->url('page-name');?>">
page name
</a>
It works both on localhost and online, with or without pretty URLs.
(For the page-name go to dashboard/full sitemap/page-name/properties/page paths and location.)

Resources