Apache2 Module Rewrite it is not working as it should - php

I created an MVC system in PHP and use the Apache Rewrite module to protect some folders and compose URL's.
I created the base on my development machine that I use Xampp and I even used the same system on an online server (client) and everything works fine.
But I want to have this same MVC system on the server that we set up in the company where I work for other developers to use it, but this server is not working properly.
On the server giving this problem, I have installed Ubuntu 20.04 (Desktop), Apache 2, PHP 7.4.13 and MySQL. I have already enabled the Rewrite module.
And I'm using the same .htaccess that I'm using both on my machine (Windows) and on the server that is online (public webserver) Linux.
Below the .htaccess files I'm using
That first .htaccess file I use to always redirect the client into the public folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
This second is inside the public folder and is to compose the URL
<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteBase /criate/public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>
The latter is inside the App folder and I use them to not have access to that folder
Options -Indexes
My Apache default.conf file
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride All
Order allow, deny
allow from all
Require all granted
</Directory>
But what is happening, when accessing the project url, let's assume it is: https://api.aplication.com
Works perfectly well, takes me to the application home with all right. But when I click the login button, for example. Give me the following error.
Not Found
The request URL was not found on this server
But there is, because I made a copy of the project that is working both on Windows and on the online server that is Linux too.
What configuration do I have to see and analyze to resolve this issue?
In phpinfo it says what module is active. Thanks

I already found what I was doing wrong in the .htaccess files, follow what I changed, that in all .htaccess files.
RewriteEngine On
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
Options -Multiviews
RewriteEngine On
RewriteBase /criate/public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
It was just removing the conditions that check if the module is active or not, I will study further to find out how to do these conditions in LINUX.

Related

Nette framework Url not working on localhost

The PHP Nette framework, Url is not working on localhost. On the live server it is working fine but when I installed it on localhost from the latest backup from hosting server it does not work.
My .htaccess file is as:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ /www/$1 [L,NE]
</IfModule>
My Localhost URL on Wamp which the home page is working properly
http://localhost/ongoingclients/www/www/
But other urls are not working.
what does it mean that it is not working? It shows 404 from apache?
try to check that
mod_rewrite in apache is enabled
AllowOverride in apache vhost settings is set to All
The above issue is resolved by .htaccess of rule RewriteBase
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ongoingclients/www/www/
# prevents files starting with dot to be viewed by browser
RewriteRule /\.|^\. - [F]
# front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(pdf|js|ico|gif|jpg|png|css|rar|zip|tar\.gz)$ index.php [L]
</IfModule>
Got success, thank you.

Phalcon framework displays index of page instead of application

I wanted to use learn phalcon so i tried installing it on ubuntu. I did everything like in tutorial and i can see phalcon in phpinfo(). I made sample project like in tutorial with following htaccess files but all I can see when typing localhost/tutorial is index of /tutorial instead of Hello! message
#/tutorial/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule ((?s).*) public/$1 [L]
</IfModule>
#/tutorial/public/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]
</IfModule>
Try removing the <br> tags from your .htaccess files. If the previous doesn't work check your apache configs for the localhost, maybe it doesn't AllowOverride.
Also you can use the steps from here to create a virtual host on apache with it's document root set to the path of tutorial folder.
Everytime you change the configs of apache, remember to do a reload or a restart of the apache service.

codeigniter rewriting of URLS not working

In the past, I have got this working no problems at all, but for some reason on my new server I just can't get it to work.
I have the following .htaccess file in the root of my application
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I have also enabled mod_rewrite and have confirmed this with php_info()
my site is located at /var/www/html/test
Is it possible that although mod_rewrite is enabled that it is not working and if so, how can I test it?
On some server implementations, you'll need to wrap it within <IfModule> tags.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Also check your httpd.conf file to ensure it has:
Options FollowSymLinks
AllowOverride All
It'd also be worth checking to makesure the .htaccess file isn't being overridden by another .htaccess file.
A simple way to test if .htaccess is working
Simply put the following in your .htaccess file:
deny from All
What this does is deny access to your site from everyone. If you are presented with a 403 forbidden when trying to access the site - the .htaccess file is being included. If not - see above.
I use this on my codeigniter setup
RewriteEngine on
# prevent these directories from hitting CI
RewriteCond $1 !^(index\.php|images|assets|robots\.txt|crossdomain\.xml)
# route everything else to the index.php
RewriteRule ^/(.*)$ /index.php/$1 [QSA]
(my setup is done in the virtualhost of .conf and not in .htaccess, though they are usually about the same configuration)

CodeIgniter removing index.php not working

I'm using Ubuntu 13 with the following setup for a local codeigniter site.
Apache/2.4.6 (Ubuntu)
5.5.3-1ubuntu2.2
'CI_VERSION', '2.1.2'
And URLs are no longer working without index.php. They used to work, but after upgrading from Ubuntu 12.x to 13.x and a few apache updates over the past year, the localhost sites no longer work right.
if I go to localhost/index.php/controllername/ it works but if I go to localhost/controllername/ it does not.
mod_rewrite is enabled.
CodeIgniter config has:
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO'; // tried all available options here and
Nothing worked
in the .conf file for the domain I have this:
<Directory />
Options -Multiviews +FollowSymLinks
AllowOverride All
</Directory>
and here's the .htaccess file commented lines are ones I tried that didn't work.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# RewriteCond $1 !^(index\.php|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
# RewriteRule .* index.php/$0 [PT,L]
# RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
I’ve Googled and read everything I can find and tried everything I could find including several posts here on Stack Overflow, including the ones in the “Questions that may already have your answer.” Still nothing seemed to work. But like I said, this worked in the past, but only after multiple updates to the OS and Apache did I first notice it stop working.
I’ll be moving away from CodeIgniter with future projects, but these projects already existed. Baffled as to what could be the issue.
SOLUTION:
turns out it was not a codeigniter issue at all. It was an apache issue but not with the rewrite rules.
in my apache2.conf I had to alter the block for /var/www/
Require all granted seems to have done the trick.
DirectoryIndex index.php index.html
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
just for good measure, I made the change here as well:
Options FollowSymLinks
AllowOverride All
Require all granted
found on askubuntu
https://askubuntu.com/questions/421233/enabling-htaccess-file-to-rewrite-path-not-working
copy following code to .htaccess in your root folder
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Options All -Indexes
This works fine for me to remove index.php in CodeIgniter.
It doesn’t seem like CodeIgniter has a default .htaccess, so unclear where that came from. But for debugging purposes, I would recommend you do the following. First, here is your .htaccess all cleaned up:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Now replace your index.php with this. It just dumps the $_GET values passed via the .htaccess like so:
echo '<pre>';
print_r($_GET);
echo '</pre>';
Now with that in your .htaccess load the index page of the site/app/project in question. The output should be something like this:
Array
(
[/controllername/here/] =>
)
Which appears to be correct. But again, you would know better than us.
The purpose of doing this is to assess whether the issue is either in Apache passing proper $_GET values to your CodeIgniter setup, which is one issue. Or whether the issue is within your CodeIgniter controller logic itself.
My gut tells me the issue is in the CodeIgniter logic in your codebase since the upgrade from Ubuntu 12.x to Ubuntu 13.x includes an upgraded version of PHP from version 5.3 in Ubuntu 12.x to version 5.5 in Ubuntu 13.x. I am using lots of code that works in PHP 5.3 and PHP 5.4 but breaks at times in PHP 5.5 since there are major changes in that codebase that will cause code to break if you don’t keep on top of non-depreciated functions & such.
I would try something like:
RewriteRule ^(?!index)(.*)$ index.php?/myvariable=$1 [L]
If you know what GET variable the script is expecting for controllername in localhost/controllername, then in the rule replace myvariable with that variable name.
Place the .htaccess file in the project folder like this:
htdocs/your_project/.htaccess
Try this new code for .htaccess:
RewriteEngine on
RewriteBase /your-project-directory-name/
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

CakePHP application cannot start in localhost

I'm trying to get a CakePHP application to work. I use xampp and put my application files in /xampp/htdocs. For .htaccess configuration I refer to http://book.cakephp.org/1.2/view/37/Apache-and-mod_rewrite-and-htaccess . After that I'm trying to run my application, but I've got this message in browser.
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.
All my other applications in which I have not used the CakePHP, runs well on localhost, without any errors. I think the problem connected with .htaccess. Any suggestions ?
EDITED
My .htaccess files
**/xampp/htdocs/app/webroot/.htaccess**
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
**/xampp/htdocs/app/.htaccess**
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
**/xampp/htdocs/.htaccess**
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
I think your .htaccess files look correct. You might need to tell Apache where the webroot of your app is. Try adding a vhost entry in your Apache configuration
inside \xampp\apache\conf\extra\httpd-vhosts.conf
try adding something like:
<VirtualHost *:80>
ServerName localhost
DocumentRoot c:\xampp\htdocs\app\webroot <-(use the actual path here)
</VirtualHost>
The you should then be able to access the CackPHP app by visiting http://localhost
Be sure to remove any other VirtualHost blocks that might conflict, although if you are working with a fresh copy of XAMPP everything in this file will be commented out.
Don't forget to restart Apache for the change to take effect.

Categories