Apache, how to handle unknown php file like standard URLs? - php

I am have a Symfony website where I have renamed the main entry point so "index.php" can't accessed nor being indexed by search motors. It works well. But, when I try to access this file I get a 404 file not found, this 404 page is handled by Apache and not by the application, it's the standard 404 page:
Not Found
The requested URL was not found on this server.
I'd like URLs like /index.php (*.php in fact) to be handled by the Symfony application to display the customised error page with the good layout. My vhost look like this, I tried to add a "directory index" directive but without success.
# configuration/vhosts/prod/mywebsite.com-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName www.mywebsite.com
DocumentRoot /var/www-protected/mywebsite.com/public
<Directory /var/www-protected/mywebsite.com/public>
AllowOverride All
Require all granted
FallbackResource /my-secret-entry-point.php
</Directory>
RedirectMatch 404 /\.git
ErrorLog /var/log/apache2/mywebsite.com_error.log
CustomLog /var/log/apache2/mywebsite.com_access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =mywebsite.com
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
SSLCertificateFile /etc/letsencrypt/live/www.mywebsite.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.mywebsite.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

This can be done with mod_rewrite, replying with 301:
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Default error documents can be used to dispatch the error-codes to PHP:
ErrorDocument 404 https://mywebsite.com/error/page/404
ErrorDocument 500 https://mywebsite.com/error/page/500
Then one still can send whatever header() one wants - or just show a page with the same headers.

Related

Remove index.php from URL after query params without using .htaccess

I want to remove index.php from my URL after query params.
This is my URL:
http://127.0.0.1/user/report?user=USERNAME
I have removed query params and convert it into pretty URL using:
RewriteCond %{QUERY_STRING} !user=
RewriteRule ^([a-zA-Z0-9\-]+)/(.*)$ $2?user=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9\-]+)$ ?user=$1 [L,QSA]
Now, my URL looks like this:
http://127.0.0.1/user/report/USERNAME
So all the requests to this URL will point to the entry script of my project i.e. web/index.php.
When I use below routes to get data, it works:
http://127.0.0.1/user/report/Default/index.php/api/registration/user-registrations/
But when I remove index.php from URL and access it like below, it throws 404:
http://127.0.0.1/user/report/Default/api/registration/user-registrations/
Apache config file:
Alias /user/report /path/to/project/web/
<Directory /path/to/project/web/>
AllowOverride All
Require all Granted
RewriteOptions AllowNoSlash
RewriteEngine On
RewriteBase /user/report/
RewriteOptions AllowNoSlash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^.]+[^\/]$ $0\/ [R]
RewriteCond %{QUERY_STRING} !user=
RewriteRule ^([a-zA-Z0-9\-]+)/(.*)$ $2?user=$1 [L,QSA]
RewriteRule ^([a-zA-Z0-9\-]+)$ ?user=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.(\d+)\.(bmp|css|cur|gif|ico|jpe?g|m?js|png|svgz?|webp|webmanifest|pdf)$ $1.$3 [L]
</Directory>
I am using Symfony for routing all my routes.
If you are using Apache 2.4, and do not want to to use .htaccess files (e.g. for performance reasons), the solution is simply to use a oneliner: FallBackResource.
You would only need this:
<VirtualHost *:80>
ServerName domain.tld
ServerAlias www.domain.tld
DocumentRoot /var/www/project/public
DirectoryIndex /index.php
<Directory /var/www/project/public>
AllowOverride None
Order Allow,Deny
Allow from All
FallbackResource /index.php
</Directory>
# optionally disable the fallback resource for the asset directories
# which will allow Apache to return a 404 error when files are
# not found instead of passing the request to Symfony
<Directory /var/www/project/public/bundles>
FallbackResource disabled
</Directory>
</VirtualHost>
This is even shown in Symfony's documentation.

Codigniter htaccess error 404 at production

i am very begainer to server technologies.i know this question asked too many times but i really don't get it run.
it runs with domain.com/2.0/index.php/testbut when you remove index.php it throws
The requested URL /2.0/test was not found on this server.
Apache/2.2.15 (CentOS) Server at domain.com port 443
my project stucture
-
Public_html
--2.0(this is my project folder where ci can be located.it has sys,app,.htaccess)
--.htaccess
My domain
https://domain.com/2.0/
about the problem
i have two htaccess one at root mention above one is inside 2.0 folder
and the strange thing is what ever i write in the htaccess file it never effect anything
public_html/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
public_html/2.0/.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
my httpd.conf
<VirtualHost *:443>
ServerAdmin info#domain.com
DocumentRoot /home/natural/public_html
ServerName api01.domain.net
ErrorLog /var/log/httpd/error_log
CustomLog /var/log/access_log common
SSLEngine on
SSLCertificateFile /home/natural/api01.domain.net.crt
SSLCertificateKeyFile /home/natural/api01.domoin.key
<Directory />
Options Indexes FollowSymLinks
Allow from all
AllowOverride None
</Directory>
</VirtualHost>
i search alot and follow all the answers by Stack overflow but no luck.
The directive "AllowOverride None" in httpd.conf will disable parsing of the .htaccess file within the directories specified (in this case /) -- Can you confirm that either of your .htaccess files are in fact being parsed?
If not, does modifying the Directory subsection within your httpd.conf file to:
<Directory />
Options Indexes FollowSymLinks
Allow from all
AllowOverride All
</Directory>
Solve the issue?
For more information: http://httpd.apache.org/docs/2.4/mod/core.html#allowoverride
I assume 2.0 is the new version of the site. It looks like /2.0/.htaccess is written as though it expects to be in the root folder. Try adding RewriteBase /2.0/ after engine on. You can remove it later when you replace the contents of the root folder with that of 2.0's.
The main problem is your # Handle Front Controller... rule. It'll capture any virtual URLs before they ever get to the 2.0 folder. Try changing it to this:
RewriteRule ^(?!2\.0(?:/|$)) index.php [L]
That will exclude the 2.0 folder from the current site's virtual URLs.
You do still need AllowOverride all as recommended by the other answer, and the directory should be the same as your DocumentRoot, not /.
Add this code inside root->.htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^(?!2\.0(?:/|$)) index.php [L]

CodeIgniter Route works over CIFS mount but not with local files

our dev environment at work uses a single centos machine with apache/php that serves different virtual hosts for both CIFS mounted and local directories. our application is built using CodeIgniter 2.0
we recently made a change to our Routes that is working fine for the virtual hosts serving the mounted drives, but the routes cannot be resolved for local files, resulting in a 404.
$config['base_url'] = "http://production.host.com"; // the production value of $_SERVER['SERVER_NAME'] essentially
routes:
$route['companyone'] = 'sso/platformsso/requestgenericsso/companyone';
$route['companyone/(:any)'] = 'sso/ptlatformsso/requestgenericsso/companyone/$1';
this works for the following scenario:
<VirtualHost 192.168.1.1:80>
ServerAdmin me#host.com
DocumentRoot "/var/mnt/a"
ServerName "a.tmbc.com"
ErrorLog logs/a.error.log
Alias ssaml /var/simplesamlphp/www
</VirtualHost>
<Directory "/var/mnt/a">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Directory populated using CIFS mount: mount -t cifs //1.2.3.4/a /var/mnt/a -o rw,username=un,password=pwd,uid=48 --verbose1
but does not work for this scenario:
<VirtualHost 192.168.1.1:80>
ServerAdmin me#host.com
DocumentRoot "/var/www/html/b"
ServerName "b.host.com"
ErrorLog logs/b.error.log
Alias ssaml /var/simplesamlphp/www
</VirtualHost>
<Directory "/var/www/html/b">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Directory populated using SVN checkout: svn checkout file:///var/svn/repo/trunk /var/www/html/b
.htaccess:
AddType text/x-component .htc
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Restrict unused HTTP methods
RewriteCond %{REQUEST_METHOD} !^(GET|POST|HEAD)
RewriteRule .* - [R=405,L]
# Forces SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Removes access to the system folder by users.
RewriteCond %{REQUEST_URI} ^_sys.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_URI} ^_app.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^(.*).(pdf|exe|doc|mp4)$ /externaldownload/?fileLocation=$1.$2 [R,L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
any ideas of why this is so?
I think I understand your issue here. Problem seems to be on (:any) your route rule.
$route['companyone/(:any)'] = 'sso/ptlatformsso/requestgenericsso/companyone/$1';
A URL with companyone as the first segment of URL and anything for the second segment will be remapped to sso/ptlatformsso/requestgenericsso/companyone/$1
It seems like codeigniter passing $1 as a variable to the function companyone which means you are actually matching any route there to forced to rewrite.
How about condensing those to route rules and make a
$route['(.*)'] = "sso/ptlatformsso/requestgenericsso/companyone/$1";
or have only one rule there for routes such as
$route['companyone/(:any)'] = 'sso/ptlatformsso/requestgenericsso/companyone/$1';

Symfony 404 errors

On my local machine, the symfony project I have created works perfectly.
I have installed symfony on my testing server and it is working fine. When I go to the URL in the browser I get the "Symfony Project Created" page. I ran the check_configuration.php and everything checks out.
The problem I am having is: I have created my database and uploaded the table data. I have uploaded the files from my local machine to the server with FTP. When I try to access the module I created, it throws a 404 Error:
Not Found
The requested URL /mymodule was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Here's some examples:
On my local machine using MAMP, I use the following URL that works fine:
http://localhost:8080/frontend_dev.php/mymodule
After I have uploaded everything, if I go to the testing server URL:
http://my.url.com:8090/
I get the "Project Success" page with the /sf images and styles.
BUT If I try
http://my.url.com:8090/mymodule
I get the 404 error as if the page does not exist, which makes me thik smfony does not know about the frontend module.
Any Help?
You'll need to do two things:
1) Ensure that mod_rewrite is enabled in Apache.
2) Double-check the .htaccess file in the document root to make sure it is pointing to your app.php. Something along the lines of:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
I used to have this problem too, please take a look at my configurations and try them out
inside my .htaccess:
Hi all, I have been having this problem too. And it seems that the "stock" .htaccess is the problem. I have solved this in my environment and here is my .htaccess with notes:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app_dev.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L] ##### this is the part that you should tweak, have the .htaccess point the request to app_dev.php, since the routing.yml is empty initially
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app_dev.php [L] ##### this is the part that you should tweak, have the .htaccess point the request to app_dev.php, since the routing.yml is empty initially
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
# When mod_rewrite is not available, we instruct a temporary redirect of
# the startpage to the front controller explicitly so that the website
# and the generated links can still be used.
RedirectMatch 302 ^/$ /app.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
inside my "symfony" entry in /etc/apache2/sites-available:
<VirtualHost 127.0.1.15>
ServerAdmin webmaster#localhost
ServerName symfony
DocumentRoot /var/www/Symfony/web
DirectoryIndex app.php
<Directory /var/www/Symfony/web>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Problems with codeigniter on ubuntu

I’ve got Codeigniter on Ubuntu 10 (LAMP). I have an htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /dort
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php
#controller, previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /dort/index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends
#the request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /dort/index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
Up until now, we connected the remote server with an IP address,my base site is sitting under a folder called
‘dort’, so we used to call it like this: http://some_ip/dort
now we mapped a virtual host to dort, so we call it like: http://demo.dort.com/ and suddenly nothing
works except for the login page (index.php). I’ve changed $config[‘base_url’] in config.php but i
keep getting 404.
Please help :(
Did you use virtual host?
I have same problem and solve with this
1) Make virtual host in my case is like this: file /etc/apache2/sites-available/default
<VirtualHost *:80>
ServerAdmin admin#email.com
DocumentRoot /var/www/dort
ServerName yourdomain.biz #this domain must be define tld info.
ErrorLog /var/log/apache2/error_2.log
<Directory /var/www/dort>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/
</Directory>
</VirtualHost>
2) Setting host in file /etc/hosts
127.0.0.1 localhost
127.0.0.1 yourdomain.biz
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
3) .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Send admin URL's to the admin controller (then skips the rest of the redirect rules)
RewriteCond %{REQUEST_URI} ^/admin(.*)
RewriteRule ^(.*)$ /index.php/admin/$1 [L]
# Redirects any request thats not a file or directory through to the main controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/main/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
ErrorDocument 404 /index.php
</IfModule>
4) Make sure when restart apache not show error
Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
5) Go to browse and run this url http://yourdomain.biz
Above solution worked for me. I just want to highlight one thing. Make sure you have AllowOverride All instead AllowOverride None in virtual host file at /etc/apache2/sites-available/default

Categories