Why we use .htaccess file? [duplicate] - php

This question already has answers here:
What is .htaccess file?
(9 answers)
Closed 5 years ago.
I am creating small data entry software using php with ajax code. But I didn't create .htaccess file. I have one doubt. must we create .htaccess file ? why we create .htaccess file. Please suggest.

A .htaccess file is not mandatory.
It has many uses and if there is a specific need, you have to use a .htaccess file. If not, you can ignore it.
For example with .htaccess you can do redirections, you can do authentication with username and password, you can restrict specific directories and many more uses.
In your case, if your needs dont forward you to the use of .htaccess, just dont use one. Although most websites nowadays have at least 1 need that .htaccess covers.

.htaccess is an Apache config file, it works in hierarchical mode, if you load it in a folder, it apply the configuration to all subfolders.
.htaccess is helpful to set timezone, redirect, customize error pages and enable or disable cache.
It is not mandatory however it is recommended its use.

Related

Execute .php files code without adding .php after url [duplicate]

This question already has answers here:
Reference: mod_rewrite, URL rewriting and "pretty links" explained
(5 answers)
Closed last month.
Recently i have installed a script. After successfully install. I got in homepage . But then i face the real problem. In navigation or in any link whichever i click i get 404 error. I tried multiple time why getting this. A hour later i found out that, site homepage+ only admin area can be browse. But if i Were to visit other options like settings or sign up/in. Pages i have to manually add .php after urls.
Basically i have lots of this type of internAl code/links. That needs to have php extention after url. So is there any code or tricks to execute .php file without typing it manually? I am on shared hosting. I have access .htaccess file and apache.
Example: site.domain.com/admin/settings = error 404
But
site.domain.com/admin/settings.php
Successfully show settings page.
menu/internal url pointing direct page without .php at the end.
And most importantly i am completely noob in this programing. So any tricks would be great. Although i can edit settings file to add .php after each links, but that is really time consuming+ if i forget to add .php in code. I have to check all of them manually 🥲. Thanks in Advance.
if it's available on your shared hosting platform (it normally is) mod_rewrite could help you here.
See: Removing the .php extension with mod_rewrite
HTH :-)
You have to put *.php in pure php or configure the .htacesse.

How can I remove .php extension while using ISPCONFIG 3?

I'd need to remove the .php extension from the browser, I used to do it from apache2.conf in the last server I was running the website on, but on this new one I need to use ISPCONFIG3 and I don't know how to use set it to remove the .php extension, since the website was already running on a server which was rewriting the extension the links in the html are all without .php which obviously causes on this new one that pages are not loaded.
Many thanks in advance
You can solve this with mod_rewrite which is available by default in ISPconfig.
As admin you can add these RewriteRules on the Options tab of a web domain. But you can also store them in a .htaccess file together with the php files.
The needed rules are already explained nicely on put links without file extension (.php)

View Some PHP Pages Exclusively [duplicate]

This question already has answers here:
Allowing only localhost to access a folder where all inclusive php files exist
(5 answers)
Closed 5 years ago.
I have lot's of PHP pages in public_html folder. But I want nobody to open them except me. I don't have any registration or anything else. I just don't want anybody access them. Is this possible? Those pages are for getting reports form database.
I've tried .htaccess but not a useful try.
Just include
Option -Indexes
in .htaccess file and save it.
Then nobody will get access to that. Thanks.
The Apache .htaccess file is meant to rewrite responses by either granting or denying access to users based off the request made.
You can use an .htpasswd file in conjunction with you .htaccess file to selectively grant or deny users access to your resources by requiring authentication via a password.

How to restrict folder content listing in php [duplicate]

This question already has answers here:
How do I disable directory browsing?
(14 answers)
Closed 5 years ago.
I have a web application developed in PHP.
My requirement is to block a folder which contains uploaded documents.
For example, if a user click a document link on the application, it should open
http://localhost:1987/enterprise_resource_planning/uploads/students/documents/1499866795_100_100.pdf
if a user execute below url, it should block
http://localhost:1987/enterprise_resource_planning/uploads/students/documents/
I've tried .htaccess with Deny from all, but it is blocking everything.
Any solution for this senario ?
possible solutions:
place a file index.html or index.php in the directory
define in your httpd.conf Options -Indexes
place that line in .htaccess
personnally I prefer the 2nd line. There hardly ever is use for automatical listing of the files to a browsing visitor.

How to configure URLs using PHP [duplicate]

This question already has answers here:
How to Implement URL Routing in PHP
(2 answers)
Closed 9 years ago.
Is there a way to make this URL:
http://readme.com/read?blog=10092
look like this:
http://readme.com/read/blog/10092
Using PHP?
It is not possible via PHP itself. Even the above example (read?blog) is not a php-only solution.
PHP is a parsed file. The webserver parses a .php script and displays it to the viewer. So you have to configure your server (Apach for example) to use the correct php file for the request. The most common solution is mod_rewrite (http://httpd.apache.org/docs/current/mod/mod_rewrite.html an lots of tutorials). You will have to edit yout .httaccess file and create configuration options.
One technique is by rewriting the url using .htaccess. Create a .htaccess (There's a dot in front of the file name!) file inside of your root folder and place the following code inside:
Options +FollowSymLinks
RewriteEngine on
RewriteRule /blog/(.*)/ read?blog=$1
Now you can go to the same page using the url :
http://readme.com/read/blog/10092
It looks like you need a PHP framework, like CakePHP or CodeIgniter. One of their main feature is the routine mechanism. However, you must first understand some Object-Oriented Programming in PHP.
Its more complicated with plain php, you have to use htaccess too. See this example:
RewriteEngine on
RewriteRule ^/read/blog/([0-9]+)$ /read?blog=$1
Url rewriting is powered by .htaccess and inside it, regular expressions. If you want to know more about regular expressions, see wikipedia: https://en.wikipedia.org/wiki/Regular_expression

Categories