At this moment I have the following files in my directory:
index.php
about.php
help.php
I'm using these lines in .htaccess to call the files using no extension.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
So sitename.com/index works as sitename.com/index.php
But I want to remove extensions completely. My directory would then look like this:
index
about
help
How to modify .htaccess to read some of these files as PHP and leave others untouched?
One possible solution would be to catch 404 errors.
You need to configure your web server to trigger a specific PHP page on error. That PHP page would then read the requested URL and include a file with the same name.
Here is some help to get started: onlamp.com
You probably can't do it with htaccess alone. You'll need to set your webserver to treat no-extension files as php files, and i'm not sure it can be done at all.
Related
I didn't write this web code. I just have to deploy it....
The $WEBROOT directory includes index.php and login.php, among others.
The automatic redirect URL from index.php (which successfully executes when just the domain URL is requested) is http://$HOST.$DOMAIN/login?p=$VALUE
This returns the code 404.
If I manually change the URL to http://$HOST.$DOMAIN/login.php?p=$VALUE
the login page successfully appears.
My first problem is I don't know what keywords to search for in the Apache documentation for this. This question seems close. But, my actual files have the .php extension. My problem is that I have to assume all of the URLs will just request file and not file.php.
How do I tell Apache to look for file.php before it returns a 404 for not finding file?
Create a file named .htaccess with the following content inside the folder your .php files are in:
#turn on url rewriting
RewriteEngine on
#remove the need for .php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
How am I able to hide a .php extension in an URL address, so that this address:
http://www.thesite.com/somefile.php
would look like:
http://www.thesite.com/somefile
without the use of the .htaccess file. The reason for that being because I have many directories and would want to hide the extension on all those files in every directory. I have tried to set expose_php to off, and this still fails with error 404.
I am using PHP 5.3.10 and Apache server.
Although you specifically said no, using the .htaccess file would remove the .php extension from all PHP files in all subdirectories in a site. I think that is what you are going for.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>
Putting that into the .htaccess file will remove all the .php file extensions. Or you could put it directly into the webserver's configuration files.
You can achieve this with URL rewriting. If you don't want to use .htaccess, you can write the rule in your host configuration file.
I have written a php script which lists all files and directories from the current directory where the php file is located. I want to use this script for many different directories and it would be great if I can reuse it for all of them. Is this possible without to put a new php file in all these folders? I think of something like htaccess redirect so if the user visits an URL to a specific folder the script is executed with the folder as parameter but it does not lie in the directory itself.
I hope you understand what I want and have any ideas for this?
Yes you need to make an .htaccess file.
So basically your code will reside in index.php which is in the root folder.
Now use the below code in .htaccess file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?path=$1 [NC,L,QSA]
now when the user tries to access sub folders, the folder path will come as a parameter named "path" to index.php
Is it bad to use:
ForceType application/x-httpd-php
and to save files without a file extention (e.g. index instead of index.php)? The intention is to hide/remove .php from the URL and to stop users from manually putting e.g. /example.php.
To remove the file extension, add this to the .htaccess :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
change .php to the proper file extension
new link :
link text
Edit :
Save your files as index.php, about.php, and so on
Yes. Yes it is bad.
The right way to do that is by using mod_rewrite and .htaccess files.
Its not good practice to change extension as it will need configuration for web server each time and so its a portability issue.
You should use .htaccess directives to setup any level of customization.
And in best practice you can route all requests to index.php to avoid direct access of php files.
How am I able to hide a .php extension in an URL address, so that this address:
http://www.thesite.com/somefile.php
would look like:
http://www.thesite.com/somefile
without the use of the .htaccess file. The reason for that being because I have many directories and would want to hide the extension on all those files in every directory. I have tried to set expose_php to off, and this still fails with error 404.
I am using PHP 5.3.10 and Apache server.
Although you specifically said no, using the .htaccess file would remove the .php extension from all PHP files in all subdirectories in a site. I think that is what you are going for.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>
Putting that into the .htaccess file will remove all the .php file extensions. Or you could put it directly into the webserver's configuration files.
You can achieve this with URL rewriting. If you don't want to use .htaccess, you can write the rule in your host configuration file.