RewriteRule isn't working - apache

I'm trying to remove .php extension from .htaccess . I'm running Apache web server on Ubuntu . Have enabled mod_rewrite.c (module) and from virtual host configuration AllowOverride option as well
In .htaccess I've following
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
</IfModule>
Which goes to 404 , but if just try to redirect from specific file to anywhere it is working , that's makes me think that I've problem with code written above , any suggestions ? thanks ...

Options +FollowSymLinks
RewriteEngine on
Options -MultiViews
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Above code perfectly works for me. I am also using Apache web server on Ubuntu.

Related

Htaccess RewriteRule not passing parameters

I had a Plesk Onyx server, running on Centos 7.2, Apache 2.4 Nginx is OFF, I had the following htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.js|\.css|\.png|\.jpg|\.gif|\.jpeg|\.bmp|\.ttf|\.woff|\.woff2|\.otf|\.eot|\.ico|\.svg|\.txt|\.xml|\.json)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^folder1/folder2/([0-9]+)/(.*)[/]$ /folder1/folder2/index.php?get_var=$1 [NC,QSA,L]
The rwwite works OK but its not passing the get_var
in index.php I had a var_dump ($_GET) and it is empty if I visit
https://sample_domain.com/folder1/folder2/123/
I want to get the 123 value into the get_var
tried disabling -MultiViews and no luck
That rule can't be working at all for the URL you presented. It would only work for something like:
https://sample_domain.com/folder1/folder2/123//
Or:
https://sample_domain.com/folder1/folder2/123/example/
Try this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !=/folder1/folder2/index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^folder1/folder2/([0-9]+)/$ /folder1/folder2/index.php?get_var=$1 [NC,QSA,L]
I removed unnecessary tests since it could only be a directory. And I guess you don't even need that test? Do you have existing directories in /folder1/folder2/ named only with numbers? If not remove that test to improve performance.
Update
To operate this out of a .htaccess file in /folder1/folder2/, change it to be as follows:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ index.php?get_var=$1 [NC,QSA,L]

Laravel on Plesk-Server - Only accessible by calling /public

When setting up a fresh Laravel installation, we have to run it using domain.com/public instead of domain.com.
I googled after this problem and I saw that this could be because of mod_rewrite not being enabled. However, we are running Plesk on our server and their documentation at http://download1.parallels.com/Plesk/PP12/12.0/Doc/en-US/online/plesk-administrator-guide/index.htm?fileName=74206.htm says:
The mod_rewrite module is enabled by default in Apache that comes with Plesk for Linux. To make use of the functionality, you will need to create an .htaccess file containing the desired rewrite rules - refer to the Apache documentation for more information.
Laravel is working fine, if I move the contents of public in the main folder, and the rest in a /laravel folder (simply have to adopt the main index.php file).
However I would like to keep the original file structure.
Any tips?
Just use .htaccess to redirect from / to /public. Maybe similar to this
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
You need two .htaccess files. One inside the Laravel Project and the other one inside your domain.com
Laravel Project (/var/www/vhosts/domain.com/public/.htaccess)
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/index.php?$1 [L,QSA]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
main domain (/var/www/vhosts/domain.com/.htaccess)
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]

AngularJS2 + Apache routing

I'm currently using Angular2 with Apache and I have a problem: when I use routing and navigate through pages, template syntax doesn't work, I just get an empty space in the place where {{}} is in the template. Also ngOnInit seems to be not working. When I use lite-server everything works fine.
EDIT1: I'm trying to run an example from developer's guide on routing: https://angular.io/docs/ts/latest/guide/router.html
https://angular.io/resources/live-examples/router/ts/plnkr.html
EDIT2: I'm also using CodeIgniter 3 and it already uses the following .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]
It could be an issue with redirects/not-found files.
Try adding .htaccess file:
# BEGIN ServeStatic
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
# END ServeStatic

.htaccess the requested url /public/index was not found on this server

Hello there i just finished setting up my server and all my code files but im having a problem. Whenever i go to https://www.frindse.com/index it gives me a error that says that the /public/index file is not found.
The way my site is setup is all the requests goes to the public folder which holds my index.php file that routes all of my pages. Not on my local server everything works flawlessly but on my digitalocean server whenever i go to the index page it doesn't works. But if i go to https://www.frindse.com/login the pages loads perfectly. So this is how my .htaccess file looks in my public folder which routes all my pages and requests:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Also here is the .htaccess file thats in the root of my website. It routes all the calls to the public folder:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
Also keep in mind that i have mod_rewrite activated on my server and also i have set AllowOverride set to All in my digitaloceans erver.
Try adding a rewritebase to your .htaccess in public folder. And turn off multiviews for good measure.
Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Actually, why do you even need an .htaccess file in public folder? Just route the request directly to the index file like this .htaccess file in your root and remove the the one in public folder.
Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^$ public/ [L]
RewriteRule ^(.*)$ /public/index.php?url=$1 [QSA,L]
</IfModule>

Having trouble with mod_rewrite on my Laravel app

Currently, I can only access links on my Laravel project if they are in the form of domain.com/index.php/somepage. I want to be able to be able to get access with a URL like domain.com/somepage. On my local Windows machine it is working fine. My server is on Ubuntu and running Apache 2.4.6 and the rewrite module was enabled via a2enmod rewrite. My .htaccess is the default one that ships with Laravel:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I've also tried the alternate in the Laravel docs both with and without the <IfModule>:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I realize this has been asked before but I still can seem to get it working with other examples that I've seen. I'm guessing it must be some Apache configuration that I don't have set.
Did you make sure that allowoverides all was in your httpd config?
That sounds like your issue.