How to route through php script without .php extension? - php

I've uploaded a php script to my online server, how can I route to pages without using .php extension?
The script is running on AWS, Ubuntu 18, PHP 7.2.19, Apache 2.4.29 I've tried to use .htaccess, installed and enableed mod_rewrite, but it didn't work.
.htaccess file:
IndexIgnore * # prevent directory listing
Order deny,allow
Allow from *
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
apache2.conf file:
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
when i route to file like "https://domainname.com/login", I expect to load "login.php" file, which resides in the root application folder, but instead I get 404 not found error:
Not Found
The requested URL /login was not found on this server.
and when navigate to "https://domainname.com/login.php", it works fine.

Related

Running a core php project from apache instead of php -S localhost:4000

I'm trying to run a php project using apache configurations in LAMP but its not working*, whereas when I run it as php -S locahost:4000 its working really great.
Here is the link to the project if you need some info about the files or working of it Project
Here is my apache configuration -
<VirtualHost localhost:4000>
ServerAdmin root#localhost
ServerName localhost
ServerAlias localhost
DocumentRoot /var/www/html/dir
<Directory "/var/www/html/dir">
AllowOverride None
Options None
Require all granted
</Directory>
*not working means - when running it through apache i can only access the index page and when going to some other page of the project like localhost:4000/about It shows The requested URL /department was not found on this server. ie. Error 404.
I think that you expect "index.php" to receive all requests.
Now, Apache is trying to find "about" directory and "department" directory.
In order for Apache to run index.php on any URL, we need to use the Rewrite rule.
Although I have not verified it in detail, I guess it will work with the following rules.
RewriteEngine on
RewriteRule ^/(.*)$ /index.php
It now working after enabling a2enmod rewrite from apache and updating the contents of .htaccess file as follows
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]

How can i block everything except root PHP scripts

I want to block HTTP access to every file in my project directory except PHP scripts located in the root folder (not subfolders).
My current .htaccess file looks like this :
# Disable Directory Listings in this Directory and Subdirectories
# This will hide the files from the public unless they know direct URLs
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/(.*)$ api.php/$1 [QSA,L]
</IfModule>
# Deny all files from being accessed with Apache
Order Deny,Allow
Deny from all
# Allow specific PHP files at root
<FilesMatch "/(api|cron|status)\.php$">
Order Allow,Deny
Allow from all
</FilesMatch>
This mostly works, except for the URL rewriting on the api.php script. I've tried changing the FilesMatch regexp to /(api|cron|status)(\.php)?$, but it keeps on throwing me a 403 response.
Anyone can explain to me what I did wrong here ? I'm usually OK with regexp, but this has got me thinking Apache doesn't process them like everyone else...
Deny from all
<FilesMatch "^(api|cron|status)\.php$">
Order Allow,Deny
Allow from all
</FilesMatch>
And I guess make sure your .htaccess is on the root level.

Can't get .htaccess to work on localhost

I am trying to get mod_rewrite to work by using a .htaccess file on my localhost server.
My apache2.conf file contains:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
And:
Include mods-enabled/*.load
/etc/apache2/mods-enabled/rewrite.load file contains:
LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so
The .so file exists.
service apache2 restart shows no warnings/errors
My .htaccess contains:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /process.php?path=$1 [NC,L,QSA]
And it's located at /var/www/wp/wp-content/plugins/myplugin/
process.php contains print_r($_SERVER);
I am visiting http://localhost/wp/wp-content/plugins/myplugin/randomstring and always getting a 404 error.
I tried changing the .htaccess file to a random string trying to trigger a 500 error to make sure the .htaccess was being "executed"(?), but still getting a 404 not found error.
var_dump(in_array('mod_rewrite', apache_get_modules())); returns true
what am i doing wrong? This always works on my online servers.
Check if the module is enabled using php_info(). If there is some errors.
Try the following command.
sudo a2enmod rewrite
I think if you change (/etc/apache2/sites-available/default) AllowOverride none to AllowOverride All then it will work.
If still you are getting the error, then do the steps mentioned here.
The tutorial is bit old, but you can get the module working.
I hope this helps.

How to get access of other pages inside of index.php by PHP?

I installed PHP and Apache server in my computer.
So inside of "htdocs" I created 2 files(index.php, Contact.php) and a directory(MyClass), after that inside of "MyClass" I created a file(class.php)..
In web browser when I am using the url "http://localhost/MyClass/class.php", the result is : "class.php" sending data to the web browser.
In the same situation is there any way in PHP/Apache to take control of it from the "index.php" ??
Or
I want to be known about all the requests inside of "index.php" which came from web browser, is it possible ????
But I don't want to use any GET variable like "http://localhost/index.php?class=page2"..
Apology for my bad English.
Thanks..
You should use include, in your case you would use
include 'MyClass/class.php';
More information about include can be found right here
I'm not sure I understand correctly but a way to not use ?class=page2
is to create a .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This will rewrite all requests to non existing files or folders to your index.php
the use $_SERVER['REQUEST_URI'] to make your navigation.
for example you could use http://localhost/class/page/2
$_SERVER['REQUEST_URI'] would then be class/page/2
If your website is in a subfolder of htdocs be sure to edit
RewriteBase /dir/here/
[...]
RewriteRule . /dir/here/index.php [L]
to match it
My problem solved.
Which changes I did they are below :
In "C:\Apache24\conf" need to change file "httpd.conf"
Just active :
1)
LoadModule rewrite_module modules/mod_rewrite.so
2)
<Directory />
#AllowOverride none
AllowOverride All
Require all denied
</Directory>
3) I am using "Virtual Host", so in "C:\Apache24\conf\extra" need to change file "httpd-vhosts.conf"
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#test.com
DocumentRoot "E:/TEST"
<Directory "E:/TEST">
Allow From All
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
</Directory>
ServerName test.com
ServerAlias www.test.com
ErrorLog "logs/test.com-error.log"
CustomLog "logs/test.com-access.log" common
</VirtualHost>
If you are not using "Virtual Host", then I think you need to add some lines to the "Directory" inside of "httpd.conf" !!
<Directory>
Allow From All
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
</Directory>
Use the PHP include function. You can include your MyClass/class.php in you index file. You can then add an htaccess file to restrict files in the MyClass directory from being viewed directly.

Laravel 4 - 403 Permissions error when trying to access css/js files

I'm running on an Ubuntu 13.10 dev environment with Apache2 and after hours of trying to figure out permission errors to access routes, I was able to fix it. Now I can successfully browse through my application, but the problem that now exists is that I cannot access my css/js files within my public directory - it kicks back with a 403.
I've tried modifying the .htaccess file, the virtual host config file, and ran chmod on the entire site directory.
Here's a copy of my .htaccess file within the Public folder:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
And here's a copy of my virtual host file:
<VirtualHost *:80>
DocumentRoot "/home/casey/Sites/caseyhoffmann.me/public"
ServerName caseyhoffmann.me.dev
<Directory "/home/casey/Sites/caseyhoffmann.me/public/">
AllowOverride all
Require all granted
DirectoryIndex index.php index.html
Options -MultiViews
</Directory>
</VirtualHost>
Any ideas?
Solved. Reapplied chown and redid chmod on the directory. File system permissions issue and had nothing to do with Apache.
Solution 1: (Recommended)
use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
Solution 2:
You can try adding this in your page's header:
<base href="/" />

Categories