Trying to remove index.php from urls - .htaccess

**I'm working on an apache secured web server hosted on an ubuntu-server.16.04
I'm sending on prod my first project on symfony and I did quite well since now. I'm trying to rewrite url so when I want to go on my project dashboard, the url would be "../dashboard/" instead of (actually like this) "../index.php/dashboard/"
But when I try to go on this url, the server redirect me out of the symfony app and throw me a basic apache 404 error, not a symfony handled error.
I think this can be edited in the .htaccess file but I can't find out how to make him perform all url as symfony routes, that's my question.
Tried multiple code found on google
htaccess
DirectoryIndex index.php
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /index.php/
</IfModule>
</IfModule>

You need to enable apache rewrite.
Please follow these steps :
In a terminal use the following commands:
$sudo a2enmod rewrite
Change AllowOverride in apache conf file:
$sudo nano /etc/apache2/apache2.conf
Change the AllowOverride from None to All in this block
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
At last, restart apache2
$sudo service apache2 restart

Related

Codeigniter4 Shared web hosting. Issue with configuring URLs

here is the thing. localhost development works fine. as I move to shared hosting try to configure codeigniter4 I'm facing issues with routes.
I use softaculouse to install CI4 on the main directory (public_html). when I try to configure the routes things work fine except
when I write the domain name.
example
https://example.com/ (Issue) it will send me to https://example.com/public/ (Code 500)
I contacted support they claim the issue in public folder I'm not allowed to add .htaccess so they deleted it for me .htaccess file in public. things work fine. however; when I write https://example.com/ works but the URL in the address bar changes to https://example.com/public
How to remove /public ?
is it a .htaccess configuration?
is it an issue with Apache configuration from hosting company side?
by the way
https://example.com/home (No problem)
https://example.com/en (No Problem) after modifying routes using {locale}
htaccess in public_html
# To prevent access to .env and other files
<Files .*>
# Apache 2.2
<IfModule !mod_authz_core.c>
Order deny,allow
Deny from all
</IfModule>
# Apache 2.4
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
</Files>
<IfModule mod_alias.c>
RedirectMatch 301 ^/$ https://example.com/public
</IfModule>
This is htaccess which initially was generated inside public folder (Its standard generated by composer i think)
# Disable directory browsing
Options All -Indexes
# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------
# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# If you installed CodeIgniter in a subfolder, you will need to
# change the following line to match the subfolder you need.
# http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
# RewriteBase /
# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Rewrite "www.example.com -> example.com"
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,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 the front controller, index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]
# Ensure Authorization header is passed along
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</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>
# Disable server signature start
ServerSignature Off
# Disable server signature end

Not Found The requested URL was not found on this server

I have an front-end application on apache server.
When i go to BASE URL it's fine but if go to (Base URL)/example and refresh this show the next message:
Not Found
The requested URL /example was not found on this server.
On my server linux (Centos 7) on /var/www/html have the ".htaccess" file with the next content:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
Somehelp how resolve this ?
Thank !
I have the next wrapper <Directory "/var/www/html"> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory>
It looks like .htaccess overrides are not enabled for the specific directory you have the .htaccess file.
It should be AllowOverride All in the <Directory "/var/www/html"> container.
Then restart Apache for the changes to take effect.

Apache2 rewrite condition with https

I have 2 .htacces file
/usr/local/apache2/htdocs .htacess file with
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteCond %{REQUEST_URI} !^/test2/
RewriteRule ^(.*)$ /test2/$1 [NC,QSA]
/usr/local/apache2/htdocs/test2 .htacess file with
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
RewriteRule !^(ui/|index.php|favicon.ico) - [F,L,NC]
#RewriteCond %{HTTPS} off
#RewriteCond %{REQUEST_URI} !^/admin
#RewriteRule .* https://%{HTTP_HOST}%/admin [L,R=301]
</IfModule>
The application will force the application to the url below while hiding the index.php
http://www.example.com/admin
additionally there is another link with http://www.example.com/display
default directory configured in httpd.conf are :
DocumentRoot "/usr/local/apache2/htdocs/test2"
<Directory "/usr/local/apache2/htdocs/test2"
However if I try to run with https the link
https://www.example.com/admin
I will get this error :
Forbidden
You don't have permission to access /admin/ on this server.
I have tried using the #commented code and still has error. Please help
These problem were fix by applying
I have found out that there are another file, ssl.conf that has a DocumentRoot, fix the problem via adding the same document root in ssl.conf
DocumentRoot "/usr/local/apache2/htdocs/test2"
<Directory "/usr/local/apache2/htdocs/test2"
and
Change a file php.conf to (# was the old code)
# Depreciated in Apache 2.2
# Order allow,deny
# Deny from all
# Satisfy All
# New in Apache 2.4
<RequireAll>
Require valid-user Require ip 127.0.0.1
</RequireAll>

How can i change my my URL with .htaccess on Symfony 2

I would like to change my url using only .htaccess, how can i change my url from this : http://s1rd-ubuntu-01/vpscraplist/web/app_dev.php/myproject/ to this http://vpscraplist/platform/ or this http://vpscraplist/
i have also more pages on my project as http://s1rd-ubuntu-01/vpscraplist/web/app_dev.php/myproject/add and http://s1rd-ubuntu-01/vpscraplist/web/app_dev.php/myproject/edit ...
I've already used thoses scripte but not work for me :(
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Explicitly disable rewriting for front controllers
RewriteRule ^app_dev.php - [L]
RewriteRule ^app.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
# Change below before deploying to production
#RewriteRule ^(.*)$ /app.php [QSA,L]
RewriteRule ^(.*)$ /app_dev.php [QSA,L]
or this one :
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Explicitly disable rewriting for front controllers
RewriteRule ^/web/app_dev.php - [L]
RewriteRule ^/web/app.php - [L]
# Fix the bundles folder
RewriteRule ^bundles/(.*)$ /web/bundles/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
# Change below before deploying to production
#RewriteRule ^(.*)$ /web/app.php [QSA,L]
RewriteRule ^(.*)$ /web/app_dev.php [QSA,L]
Thank you and sorry about my english !
In my opinion the best approach is to keep the default htaccess of symfony, and to configure your apache vhost as explained there:
http://symfony.com/doc/current/cookbook/configuration/web_server_configuration.html
(don't forget to enable the apache rewrite if it is not dude: sudo a2enmod rewrite )
create the vhost file:
sudo nano /etc/apache2/sites-available/yoursite.conf
with:
<VirtualHost *:80>
ServerName mydomain.net
ServerAlias www.mydomain.net
DocumentRoot /var/www/myprojectSymfony/web
<Directory /var/www/myprojectSymfony/web>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
Once you have have created your virtual file, you must enable them
sudo a2ensite yoursite.conf
When you are finished, you need to restart Apache to make these changes take effect:
sudo service apache2 restart

Symfony .htaccess DirectoryIndex not working

I am deploying my symfony application to production, i have placed the symfony application in the root folder
/var/www/html and have set the apache2 root directory to /var/www/html/web but the DirectoryIndex in the .htaccess file is not redirecting to the file app.php
On the url xxx.xxx.xx.xx/ i get the list of all the documents in /var/www/html/web folder, and if i do xxx.xxx.xx.xx/app.php the application loads.
I don't understand how to get the DirectoryIndex in the .htaccess to work.
I have been going through the following stack questions types but they don't work for me:
Redirect all requests to web/app.php in Symfony
Symfony: htaccess to hide app.php or app_dev.php
symfony2 rewrite rules .htaccess app.php
Another post suggested to set the apache2 root directory as /var/html instead of /var/html/web but that doesn't work either. Because the url xxxx.xxx.xx.xx/ deplays the content of the directory /var/html
The closest to a solution i came to was to rename the file app.php to index.php which got the root url xxx.xxx.xx.xx/ to load, but then all the 'sub' urls ( like xxxx.xxx.xx.xx/offer/9/Alternance_Apprentissage_graphiste_chez_Eggs) fails with a 404 Not Found error.
It is due to .htaccess file not redirecting properly the urls, because the url xxxx.xxx.xx.xx/app.php/offer/9/Alternance_Apprentissage_graphiste_chez_Eggs works fine.
My .htaccess file looks like this (it is the default generated file):
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /app.php/
</IfModule>
</IfModule>
Any help appreciated. This is critical because i cannot get my application to go on production.
UPDATE
#shevron 's is right. From his answer "It is possible that .htaccess files are not parsed at all by your Apache setup." and "Check your main Apache configuration for AllowOverride on that directory. Try to set it to AllowOverride All and restart your Apache - see if that works."
I did the following:
I set my Apache Configuration as following :
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/web
<Directory "/var/www/html/web">
AllowOverride All
Allow from All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
This did not work.
#shevron did i not write that properly?
But further i put all the configurations from the .htaccess file into the Main Apache Configurations as such and this made the redirection work:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/web
<Directory "/var/www/html/web">
AllowOverride All
Allow from All
</Directory>
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /app.php/
</IfModule>
</IfModule>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
So now the redirection is working.
It is possible that .htaccess files are not parsed at all by your Apache setup (in fact, this is probably recommended for single-tenant production servers for both performance and security reasons).
Check your main Apache configuration for AllowOverride on that directory. Try to set it to AllowOverride All and restart your Apache - see if that works.
If it works, I suggest moving the contents of your .htaccess file into a <Directory> or <VirtualHost> section in the main Apache configuration file (or better yet in a separate file included by it) and setting AllowOverride None.
I found this: http://symfony-check.org/permalink/optimize-apache-avoid-htaccess which is specific for Symfony with some details, although I don't know if its up-to-date as I'm not a Symfony user.

Resources