Loading a single file for all pages - php

I am running a Apache webserver, my goal is to have a loader (In PHP) that gets loaded from all URLs. For example if you head to website.com/test/, website.com/page.php or just website.com/ it will always load website.com/index.php
How can i achieve this?

You can create a .htaccess file that will rewrite the URL for anything this is not a file or directory on your server and redirect it to the index.php file. Additionally you can have the original URL attached as the query string so you can work with it in your script if need be.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
</IfModule>

Create a .htaccess file with the following lines and put it in the root directory of your website:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
If a .htaccess file already exists, you can try appending those lines if there's no conflicting rules.

Related

how to change default page index.php to url

i have my .htaccess file here..
RewriteEngine On
RewriteBase /faith/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]
RewriteBase /faith/ is the main folder on my local... but my problem is when i upload it in the cpanel i dont have any idea on how to use the url as my index.php .. i tried to search for it and it says that i need to change the rewritebase in a url.. but that doesn't seems to work cause what i want is my index.php will be a url.
my login page is index.php
and i want it to become sample.com so whenever i search it the only thing that i need to do is to type sample.com
Just add this line at top of your .htaccess:
DirectoryIndex index.php
This will load index.php by default when you visit http://example.com
your script seems ok in .htaccess file, you just need to use the redirection file name just like you are using index.php , you can use sample.php overthere.
RewriteEngine On
RewriteBase /faith/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ sample.php?uri=$1 [QSA,L]

How to restrict PHP file access using .htaccess file?

Here is my .htaccess file
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?r=$1 [QSA,L]
My base folder contains one "index.php" file and "app" folder and the app folder contains some php files and i don't want to direct access these php files inside app folder using browser.
As worked out in the comments to your question your requirements are actually a little more complex than what you wrote in your question.
I guess this is what you are actually looking for:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteRule ^(.*)$ index.php?r=$1 [QSA,L]
This will rewrite requests to index.php when the file name in the request ends in the .php file name extension.

can another .htaccess file can work in subfolder

I have a codeigniter site http://piyukarts.in/mss/ where in .htaccess file under mss contains
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
# Send request via index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
but i cant access a link http://piyukarts.in/mss/posts, but i can access piyukarts.in/mss/index.php/posts, with this index.php/ after mss/
piyukarts.in/ is wordpress site, also having a .htaccess file
Please help.. thanks a ton in advance...
I made this change in .htaccess file and its working fine now.
RewriteEngine On
RewriteBase /mss
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|system)
RewriteRule ^(.*)$ /mss/index.php?/$1 [L]
To answer:
can another .htaccess file can work in subfolder
is:
Yes.
.htaccess files in any folder in the file path tree to the file, will be applied to the file.
so file as /root/home/site/includes/css/horses.css the htaccess in any of those folders would be applied to the file horses.css .

redirect all files to index.php

I try to redirect all files and folders to index.php for an MVC routing but it working only from index.php like learn.delapiata.ro/index.php/i/can/put/anything/here/and/redirect
I wish to work without index.php: learn.delapiata.ro/controllers/any_php_file.php
I am using this in .htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php [L]
Thank you.
What you have would already work - but you only need the -f rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
These rules simply mean: If the request does not point at a file that exists - send the requeset to index.php
Note that htaccess files only take effect at all if they are enabled in the apache config. If for example the htaccess file is modified to contain an error (put "asdf" in it) - and you don't get a 500 response - Apache isn't reading the .htaccess files and this is why it "doesn't work". To fix that, put AllowOverride All somewhere appropriate in your apache config file and restart apache.

mod_rewrite: Redirect to cache or script

I'm having some troubles with my mod_rewrite configuration, for a "cache" solution that I'm working on. I have directory called "cache" with compiled html files. I also have an index.php file which handles all the requests.
Right now all requests are rewritten to the index.php file like: "/articles" => index.php?q=articles.
But now I want it to rewrite to "cache/articles/index.html" if it exists, or else just fall back and rewrite to the index.php script.
I have messed around with the rewrite for a while know, but I can't get it to work. The rewrite code looks like this now (without anything for the cache):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
Thanks in advance :)
Try adding the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
#if the index.html exists in the cache
RewriteCond %{DOCUMENT_ROOT}/cache%{REQUEST_URI}/index.html -f [NC]
#then display it
RewriteRule ^ cache%{REQUEST_URI}/index.html [L]
#other existing rules go here

Categories