ARRW - Another RewriteRule wondering - model-view-controller

I'm sorry for this as there has been lots instances of such question, but I can't get through.
Request
In the Symfony2 project, I want to access pages without having the php controller in the URL, e.g :
# This is the present use :
GET /af/index.php/myURI HTTP/1.1
OK
~
# And this is the wanted use :
GET /af/monURI HTTP/1.1
But I get a 404 error, either with the default .htaccess or an own version.
.htaccess
Default .htaccess :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
I modified this as follows :
<IfModule mod_rewrite.c>
RewriteEngine On
# RewriteBase /af
# Options +FollowSymLinks
# Isn't the request for a regular file ?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Directory Structure
Here is my directory structure :
/var/www/af
├── app
├── bin
├── src
├── vendor
└── web
├── app_dev.php
├── apple-touch-icon.png
├── bundles
├── config.php
├── favicon.ico
├── .htaccess
├── index.php
└── robots.txt
I have dynamically linked the web/ directory to /var/www/af
Edit - I have found this :
When using the rewrite engine in .htaccess files the per-directory
prefix (which always is the same for a specific directory) is
automatically removed for the RewriteRule pattern matching and
automatically added after any relative (not starting with a slash or
protocol name) substitution encounters the end of a rule set. See the
RewriteBase directive for more information regarding what prefix will
be added back to relative substitions.

Don't change .htaccess file rules.
If you have access to webserver config files (i.e. local dev or virtual machine), then you need to add VirtualHost rule into httpd.conf.
Something like this:
<VirtualHost *:80>
DocumentRoot "/var/www/af/web"
ServerName af
<directory "/var/www/af/web">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</directory>
</VirtualHost>
If you don't have access to webserver config files (i.e. shared hosting), then you need to have web folder linked as the public access folder (sometimes named public_html)

Problem solved, thanks to Mac : My document root didn't allow overrites for htaccess.

Related

working .htaccess configs don't work on httpd.conf

An early heads up - I'm a beginner student with Back-end programming and for now, even .htaccess URL rewrites was a huge pain to implement.
I have XAMPP Apache installed on my Mac (not XAMPP-VM) with a website folder called "Project" inside "/htdocs". So basically a website that I'm practicing with URL looks like this - "localhost/Project"
There was one .htaccess file in my "root" ("root" is the "/Project" folder) folder and another one inside a "PHP" folder (i.e. root/PHP/.htaccess).
Root's .htaccess had the following configs:
Options -Indexes
ErrorDocument 403 /Project/index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !(.*)Pages
RewriteRule ^(.*)$ Pages/$1.php [L,NC]
</IfModule>
Whilst root/PHP's .htaccess had this:
Deny from all
Everything worked and after reading a bit more about .htaccess best practices I wanted to move all of the above configs to httpd.conf, specifically the one located inside "/Applications/XAMPP/xamppfiles/apache2/conf". I moved the code to that httpd (properly?), commented out everything inside the previously mentioned .htaccess files, and here's how the httpd now looks like inside:
Alias /bitnami/ "/Applications/XAMPP/xamppfiles/apache2/htdocs/"
Alias /bitnami "/Applications/XAMPP/xamppfiles/apache2/htdocs"
<Directory "/Applications/XAMPP/xamppfiles/apache2/htdocs">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory "/Applications/XAMPP/xamppfiles/apache2/htdocs/Project">
Options -Indexes
ErrorDocument 403 /Project/index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !(.*)Pages
RewriteRule ^/(.*)$ /Pages/$1.php [L,NC]
</IfModule>
</Directory>
<Directory "/Applications/XAMPP/xamppfiles/apache2/htdocs/Project/PHP">
Deny from all
</Directory>
And it doesn't work. I've tried to google a solution for a while and so far completely nothing. Just in case, I'll also mention that the goal for this "CMS" project is to "write once, install anywhere".
[EDIT]
With some clarifications from #MrWhite, this is what the configs look like in xamppfiles. Also, also, Options -Indexes and /Project/PHP > Require all denied don't work as I can browse folders and access "PHP" folder from Browser. And it did not work prior to this EDIT as well.
-xamppfiles/apache2/conf/httpd.conf
Alias /bitnami/ "/Applications/XAMPP/xamppfiles/apache2/htdocs/"
Alias /bitnami "/Applications/XAMPP/xamppfiles/apache2/htdocs"
<Directory "/Applications/XAMPP/xamppfiles/apache2/htdocs">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Include "/Applications/XAMPP/xamppfiles/apache2/conf/httpd.conf"
-xamppfiles/apache2/conf/project.conf
<VirtualHost *:80>
DocumentRoot "/Applications/XAMPP/xamppfiles/apache2/htdocs/Project">
Options -Indexes
ErrorDocument 403 /Project/index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !(.*)Pages
RewriteRule ^(.*)$ Pages/$1.php [L,NC]
</IfModule>
<Directory "/Applications/XAMPP/xamppfiles/apache2/htdocs/Project/PHP">
Require all denied
</Directory>
</VirtualHost>
I'd greatly appreciate any help.
Original:
RewriteRule ^(.*)$ Pages/$1.php [L,NC]
New:
RewriteRule ^/(.*)$ /Pages/$1.php [L,NC]
You shouldn't have changed anything here. You are moving the directives from .htaccess to a <Directory> section (in the server config). These are both directory contexts and work the same way. (You are incorrectly thinking this is a server or virtualhost context, although the target URL-path would still be wrong.)
By introducing a slash at the start of the URL-path in the RewriteRule pattern (first argument) the rule will never match. If it did... the additional slash you've introduced at the start of the substitution string (second argument) would incorrectly rewrite the request to /Pages in the document root, not /Project/Pages as would seem to be the intention.
Note that if you are moving the .htaccess config to <Directory> containers in the server config then you should probably disable .htaccess overrides altogether, since a .htaccess file that contains mod_rewrite directives will completely override the <Directory> container in the server config. For example:
AllowOverride None
Note also that Order, Allow and Deny are Apache 2.2 directives and formerly deprecated on Apache 2.4. On 2.4 you should be using the equivalent Require directive instead. For example:
Require all granted
Require all denied
Extra:
Look into creating additional <VirtualHost> containers for each project, store these in separate files and include them in the main server config, rather than modifying the main server config directly. This will allow you to host multiple (separate) websites on the same webserver.
I have found the issue, which was my blind mistake.
<Directory "/Applications/XAMPP/xamppfiles/apache2/htdocs/Project">
Options -Indexes
//...
I was pointing to the wrong htdocs folder that contains my Project and the correct way was:
<Directory "/Applications/XAMPP/xamppfiles/htdocs/Project">
Hope this saves some time for anyone else that would come across this.

Laravel show blank page in local ubuntu 20

I am using ubuntu 20, laravel project run using php artisan serve but when I access the site using this URL laravel-local.com it shows blank page
Could not get any error
sites-available
<VirtualHost *:80>
ServerAdmin admin#MyWebsite.com
DocumentRoot /var/www/html/laravel-local.com/public
ServerName laravel-local.com
<Directory /var/www/html/laravel-local.com/public>
Options +FollowSymlinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
What I tried
Give project 777 access
Change the ownership
Delete the folder inside the storage/framework
Change the storage folder permission
composer dump-autoload, php artisan cache and config clear
Delete .htaccess file in public folder
I tried all the above steps still got a blank page
.htaccess file in root folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
.htaccess in the public folder
<IfModule mod_rewrite.c>
Options -Indexes
<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]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
sites-available
UPDATE: Your vHost config should be defined in "sites-enabled" for the config to actually be applied. Create a symlink from "sites-enabled" to the actual config file in "sites-available". And restart Apache.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
You don't need an .htaccess file like this (which would need to go in the directory above the /public directory) because you have already configured the /public directory as the DocumentRoot in the server config.
If you had also deleted the .htaccess file inside the /public subdirectory then this would have resulted in a rewrite-loop (500 error).
You just need the standard Laravel front-controller (.htaccess file) inside the document root, ie. in the /public subdirectory.
However, you should define the DirectoryIndex (if not already set somewhere in the server config), either in your vHost (<Directory> container) or at the top of your main Laravel .htaccess file (inside the /public folder). If this is not defined then requests to the Laravel directory itself (the homepage) will not be routed through Laravel and will likely result in a 403 Forbidden response (or worse, a directory listing if Indexes have been enabled) from Apache.
For example:
DirectoryIndex index.php

After Deployment, Images not showing in Laravel

I created Common Helper for Image Upload & Common Trait Function for get Images Full URL from Storage & I tested it in Laravel's default inbuilt server it's 1005 perfectly works.
but after Deployment I can't Display Images. it's show 404 error to me
I shared my Common function of Get Full URL path of Image from storage.
app/Traits>HasProfilePhoto.php file
public function getProfilePhotoUrlAttribute()
{
return $this->profile_photo_path
? Storage::disk($this->profilePhotoDisk())->url($this->profile_photo_path)
: \URL::to('/').'/adminLTE/dist/img/user-img.png';
}
above function will returns Image's Full URL Path Like."test.com/storage/users/user.png"
using Laravel's Default Inbuilt server it returns Full URL like this. http://localhost:8000/storage/users/user.png & using that path image display perfectly
but after the deployment in local Windows using XAMPP Server, it returns Full URL like this. http://localhost/ProjectFolderName/storage/users/user.png & it's show 404 Not Found
NOTE:- If I use FULL Url like this:- http://localhost/ProjectFolderName/public/storage/users/user.png then image displayed
Please Help me what's my mistake there?
*
**How I deployed my Laravel Project see below:- **
*
root/server.php file renamed with index.php
& root/public/.htaccess file copied at root directory
Rename the server.php file to index.php is not good practice for security reasons as well. So if you need to hide the public path from the URL then create a .htaccess at root level:
<ifmodule mod_rewrite.c>
<ifmodule mod_negotiation.c>
Options -MultiViews
</ifmodule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
</ifmodule>
Updated by Harsh Patel:- 8th September 2021
Actually Problem is about defining Virtual Host in Server... I was defined DocumentRoot path & Directory path as like "C:/xampp/htdocs/projectFolderName/ this.
I was identified my mistake in deployment from here medium's Blog
but as per Laravel Documentation , we need to direct all requests to public/index.php in other words Laravel's Public Folder is an Our Server's Root (public_html) folder that's why I need to define DocumentRoot & Directory up to public folder like this C:/xampp/htdocs/projectFolderName/public
We need to define our Laravel Web in Apache Server's Virtual Host Config.
for XAMPP Server
step 1: open C:\xampp\apache\conf\extra path location in XAMPP Server
step 2: Open httpd-vhosts.conf File
step 3: define virtual host rule as like below
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot "C:/xampp/htdocs/projectFolderName/public"
<Directory "C:/xampp/htdocs/projectFolderName/public">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
IF you faced any problem to define Virtual Host in XAMPP then you can see Full detailed answer about How to define Virtual Host in XAMPP
& If you Use WAMPP Server then you can see Full detailed answers about How to define Virtual Host in WAMPP
Maybe you just need to set your projects URL inside .env to start with http://
APP_URL=http://your-project-url
Without this, images will not have proper URL but when you copy image path and open it inside new tab, image will be available anyway.

Cannot remove public/index.php from url after changing configuration and .htaccess file

I am working on my local server right now and trying to get rid of '/public/index.php' from http://localhost/example/public/index.php/ in the url. I have followed content from the site: https://www.tutsmake.com/how-to-remove-public-from-url-in-laravel/ and added the config file laravelex.conf with the following content:
<VirtualHost *:80>
ServerName localhost
ServerAdmin webmaster#localhost
DocumentRoot /var/www/example/public
<Directory /var/www/example>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
In my project, uder the public folder I have .htaccess file as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
But when I'm trying to open http://localhost/laravel_project_folder/ or even http://localhost/laravel_project_folder/index.php/ I'm getting
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at webmaster#localhost to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
Apache/2.4.29 (Ubuntu) Server at localhost Port 80
The error in the log file is
AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
Note: I have also tried to change .htaccess in the following way by looking at the answer by #stephen:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
change the file structure like this:
├── project folder
│ ├── core
│ │ └── laravel files
│ ├── html
│ ├── css
│ └── index.php
copy all of the project files to the core folder and copy the content of the public folder to the main folder.
then fix path of autoload.php and app.php in index.php:
require __DIR__.'/core/vendor/autoload.php';
$app = require_once __DIR__.'/core/bootstrap/app.php';
Sorry for the late reply first. But I found that I eventually had an answer from different sources. First I put my project files in the following order :
├──.cpanel/
├── public_html/
├── .htaccess
├── index.php
├── web.config ...
├── public_ftp/
├── laravel_project_folder
│ ├── app
│ ├── bootstrap
│ ├── config ...
│ └── index.php
Now I have changed file content in two places.
Changed the index.php in the following lines :
require DIR.'/../vendor/autoload.php';
and $app = require_once DIR.'/../bootstrap/app.php';
to
require DIR.'/../laravel_project_folder/vendor/autoload.php'; and $app = require_once DIR.'/../laravel_project_folder/bootstrap
/app.php';
changed the content of .htaccess too.

How to point bookstack (Laravel) application's public folder on a mydomain.com/bookstack

Hi I am trying to host bookstack application which is basically a larvel application on a shared hosting, After uploading database dump from local and uploading all the files to public_html/bookstack folder.
Now I am able to visit the webapp at https://example.com/bookstack/public , but I want to host the site at example.com/bookstack only and don't want the "public" in url.
I have two questions,
1) How to achieve it using .htaccess file?
2) Inside the .env file there is an option
# Application URL
# Remove the hash below and set a URL if using BookStack behind
# a proxy, if using a third-party authentication option.
# This must be the root URL that you want to host BookStack on.
# All URL's in BookStack will be generated using this value.
#APP_URL=https://example.com
So if here I put APP_URL as https://example.com/bookstack , and remove the comment, its first giving "403 Forbidden - You don't have permission to access this resource." error and if I visit to https://example.com/bookstack/public it works fine.
Update : So I found this subdirectory setup page in bookstack documentation but it only describe how to setup if I have access of apache, which is not possible in case of shared hosting
Move the .htaccess file in /public to root.
Rename server.php file in root to index.php.
you can dos this
<VirtualHost *:80>
...
# BookStack Configuration
Alias "/bookstack" "/var/www/bookstack/public"
<Directory "/var/www/bookstack/public">
Options FollowSymlinks
AllowOverride None
Require all granted
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]
</Directory>
<Directory "/var/www/bookstack">
AllowOverride None
Require all denied
</Directory>
# End BookStack Configuration
...
for more deatail you can see this reference.
Setup Sub Path in BookStack
Thanks

Resources