Remove index.php from URL of Laravel - apache

I am using Laravel 5.5.12 in Linux Mint.I am using LAMP stack. I would like to remove index.php from URL. My mod_rewrite apache module enabled.
My .htaccess file located in public folder and it contains following code.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
Options +FollowSymLinks
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
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>
I renamed the server.php in the Laravel root folder to index.php and copy the .htaccess file from /public directory to Laravel root folder. But it is not working.
I placed below code in .htaccess file
<IfModule mod_rewrite.c>
# Turn Off mod_dir Redirect For Existing Directories
DirectorySlash Off
# Rewrite For Public Folder
RewriteEngine on
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
But this not working.
Could anyone help me in this regard ?

Reset anything you have changed back to the default.
In your apache virtual host configuration, ensure you have the DocumentRoot correct (it will be something like /var/www/html/laravelproject/public).
You should not need to make any changes to the .htaccess file in the public folder as this handles rewriting the url to remove index.php. However, in some environments, I have had to add
RewriteBase /laravelproject
to the .htaccess in the public folder.

I found the answer here:
https://ma.ttias.be/remove-index-php-from-the-url-in-laravel/
To summarise, I added the following to the .htaccess file, directly below the existing index.php entry:
RewriteRule ^index.php/(.+) /$1 [R=301,L]

Go to httpd.conf file, search for the line DocumentRoot and change it to where ever your index.php is. For example:
DocumentRoot "C:/xampp/htdocs/myappfolder/public"

Related

htaccess - subfolder as DocumentRoot for CSS files?

How do I use the .htaccess file to set a custom DocumentRoot for CSS files? All CSS files are within a folder named "assets", and I'd like to omit the "assets" folder when loading in the CSS files for an HTML page.
This is the current code I am using:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.com$
RewriteCond %{REQUEST_URI} !assets/
RewriteRule (.*) /assets/$1 [L]
This code makes links relative to the assets folder, but it doesn't apply to CSS files for some reason, as I still need to use href="assets/styles.css" in order to load a CSS file from /assets. I'd like to simply use href="styles.css".
I would create only one .htaccess file to solve all your problems. Please put this into your /root folder and delete the other .htaccess files.
# This first part should be done by the webserver,
# if not than thing about to change you hoster but I put it here:
# Preventing direct access to any .ht file (.htaccess, .htpasswd, etc.)
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
Options +FollowSymlinks
Options -Indexes
# Start to Rewrite
RewriteEngine On
# For all URL starting with /css, /fonts, /img or /js
RewriteCond %{REQUEST_URI} ^/?(css|fonts|img|js)(/.*)?$ [NC]
RewriteRule ^.*$ /site/public/%1%2 [L]
# Redirect all to the Application if not done already
RewriteCond %{REQUEST_URI} !^/?site/public/index\.php [NC]
# but not if the URL starts with css, fonts, img or js
RewriteCond %{REQUEST_URI} !^/?(css|fonts|img|js)(/.*)?$ [NC]
# or if request is a real file
RewriteCond %{REQUEST_FILENAME} !-f
# or if request is a real directory but not the root directory
RewriteCond %{REQUEST_URI} ^/?$ [OR]
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite the rest to the index.php file in your public folder
RewriteRule ^.*$ /site/public/index.php [NC,L]
It worked... I just refreshed my browser cache, and the CSS files was included properly.

htaccess: Redirect / to specific file and keep all symfony redirects

I have an index.html, that is the directory root of the vhost. Parallel there is an symfony application.
I want to have the index.html as the root when example.com or example.com/ ist called.
This index file includes the symfony app via an iframe:
<iframe src="app.php" allowfullscreen></iframe>
But even when I have index.html set as the doc root, it will still call the app.php. I tried and tried to get it to work, I have no idea what I am missing.
This is what my .htaccess looks like:
DirectoryIndex index.html
<IfModule mod_rewrite.c>
RewriteEngine On
# Rewrite path "/" to index.html
RewriteRule ^[/]?$ /index.html [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
# When mod_rewrite is not available, we instruct a temporary redirect of
# the start page 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>
The first RewriteRule is my own, but it doesn't work. It calls the index.html, but also redirects app.php to index.html creating an infinite recursion.
You will need to place the following lines directly behind the first RewriteEngine On:
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^$ index.html [L]
With these lines, only when example.com is requested and nothing is specified, it will redirect the main page to index.html
[EDIT]
Your code looks ok. Could you try to change the line RewriteCond %{ENV:REDIRECT_STATUS} ^$ into RewriteCond %{ENV:REDIRECT_STATUS} 200 ?
(reference Apache mod_rewrite REDIRECT_STATUS condition causing directory listing)

can not access directory without specify index.php

I want to make my app root directory can be accessed without specify index.php like
www.domainname.com/dev/
My .htaccess now is
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
In my localhost it works, but in my hosting, when i access, it says "directory access is forbidden"
What should i change?
regards
Maybe this will solve the problem.
Add something like this in your .htaccess file at root directory:
DirectoryIndex index.php index.html
The first file found, from left to right, will be loaded by default. You can add more files or remove index.html as appropriate. This is just an example.

CodeIgniter Remove index.php with .htaccess

I've been working on this for an hour already so I figured I might as well ask.
I am trying to remove the index.php from my CodeIgniter app's URL and can not do it.
The app is running on a dedicated server in my office and I access the app through the url http://smr_local
This is my base virtual host block
<VirtualHost 192.168.1.90:80>
ServerAdmin admin#server.com
DocumentRoot "/var/www/smr.dev/app"
ServerName smr_local
ErrorLog "/etc/httpd/logs/error_log"
CustomLog "/etc/httpd/logs/access_log" common
<Directory /var/www/smr.dev/app>
Order allow,deny
Allow from all
AllowOverride All
</Directory>
<IfModule mod_rewrite.c>
RewriteEngine on
</IfModule>
</VirtualHost>
Here is my .htaccess file
RewriteEngine on
RewriteBase /
# Hide the application and system directories by redirecting the request to index.php
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]
And my config file
$config['base_url'] = "http://smr_local/";
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
Right now when I'm trying to access my base url http://smr_local/user/courses I get an error in my apache error log I get this.
File does not exist: /var/www/smr.dev/app/user
I really don't know what to try next. Any help would be appreciated.
Have you checked official user guide?
example.com/index.php/news/article/my_article
You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the "negative" method in which everything is redirected except the specified items:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
In the above example, any HTTP request other than those for index.php, images, and robots.txt is treated as a request for your index.php file.
I have been working with Codeigniter previously and that is how I get it to work:
Here is my .htaccess (assuming you have default folder structure):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes access to the system folder by users.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
# Prevents user access to the application folder
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /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 ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
That is relevant part of config.php:
$config['base_url'] = '';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
$config['url_suffix'] = '';
If this won't help, your problem likely in Apache configuration.
Then provide your httpd.conf and any other relevant configurations, not only virtual-hosts.

How can I remove "index.php" from my links?

Normally I access a page like this: http://localhost/codeigniter/index.php/auth/login
However I want to be able to access to the page like this : http://localhost/codeigniter/auth/login
I have no information about http.conf and .htaccess configurations.
I have set $config['index_page'] = ''; in codeigniter/application/config/config.php
my http.conf file is at its default except uncommentnig "LoadModule rewrite_module modules/mod_rewrite.so"
I inspected many proposed .htaccess files but I could not understand the logic behind the rewriting rules.
Here is the most successful .htaccess content for me. It is located at /www/CodeIgniter/ .
(by the most successful I mean that this is the only one that gives 404 error created by the CodeIgniter not by the server.)
RewriteOptions Inherit
Options -Indexes
Options +FollowSymLinks
RewriteBase /CodeIgniter/
# Set the default file for indexes
DirectoryIndex index.php
<IfModule mod_rewrite.c>
# activate URL rewriting
RewriteEngine on
# do not rewrite links to the documentation, assets and public files
RewriteCond $1 !^(images|assets|uploads|captcha)
# do not rewrite for php files in the document root, robots.txt or the maintenance page
RewriteCond $1 !^([^\..]+\.php|robots\.txt)
# but rewrite everything else
RewriteRule ^(.*)$ 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.
ErrorDocument 404 index.php
</IfModule>
I hate requesting direct solutions however unfortunately, it is my last resort :/
Can you give me a right solution to my problem?
It's my repeat answer:
Codeigniter issues with paths in localhost (XAMPP)
please create .htaccess file in project folder and write:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
You don't need to define in base_url in config file:
$config['base_url'] = ''; // blank it.
put this on your htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
make sure the rewrite module is enabled on your server
This is the simplest one i found which works for me :-
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]