I am working on laravel 5.4 project and want to remove public from url.Right now my url is localhost/project_name/public/login.
Please suggest me way remove public from url.
Step 1: Rename File
In first step it is very easy and you need to just rename file name. you have to rename server.php to index.php at your laravel root directory.
server.php
INTO
index.php
Step 2: Update .htaccess
first of all you have to copy .htaccess file and put it laravel root folder. You just copy .htaccess file from public folder and then update bellow code:
.htaccess
Options -MultiViews -Indexes
RewriteEngine On
# 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]
# Handle Front Controller...
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]
Documentation : https://hdtuto.com/article/laravel-remove-public-from-url-using-htaccess
Create a new .htaccess file in your root and paste following code.
Options -MultiViews
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
ONE OF THE BEST SOLUTION
Related
I had originally built my website in React before learning that my client is using SiteGround so I had to change everything over to a basic HTML format. I have my navbar using href = "fileName.html" to navigate the page. This leads the URL to become www."siteName".com/"fileName.html" once navigating off the homepage.
This is what I have so far in my .htaccess to remove the .html from the end:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule (.*) $1.html [L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>
This does not seem to be working. Should I change my .htaccess file? Should I change the way my navbar navigates the pages?
With your shown samples/attempts, please try following .htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.
Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine ON
RewriteBase /
###Rules for handling .html to without html uri in browser.
RewriteCond %{REQUEST_URI} \s/([^.]*)\.html\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.*)/?$ $1.html [L]
##Rule from OP's already existing .htaccess file.
RewriteRule ^index\.html$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>
<center><button onclick="window.location.href='/shop.html'">SHOP</button></center>
I am trying to remove the .html tag from /shop.html. I have created a .htaccess file inside of my root folder. I included the follow inside;
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
I then removed the .html from /shop.html and had no luck with error 404.
Cheers, Ben.
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule (.*) $1.html [L]
If the requested URL does not have an extension:
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
And it is not a directory:
RewriteCond %{REQUEST_FILENAME} !-d
And it is not a file:
RewriteCond %{REQUEST_FILENAME} !-f
And if by adding .html we find a file:
RewriteCond %{REQUEST_FILENAME}.html -f
Then add .html and serve that file:
RewriteRule (.*) $1.html [L]
I have Codeigniter running on Apache and have Apache Mod Rewrite that works well.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I've now changed the front-end to be HTML5 and my app uses emulated URLs that suppose to be handled by the same 1-page application.
So I need something like the following URL rewrite:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
So, I'd like to combine those together.
First I'd like to see if the first part of URL path is a CI controller (a file in ./application/controllers) and if yes then rewrite it to index.php.
If not, I'd like to rewrite it to index.html.
I'm trying to build something like
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# -- RewriteCond ^/([^/]+)/ (application/controllers/$1.php) is a file
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteRule ^ index.html [L]
I understand that my other option it to manually specify all possible routes and controllers but would be nice to do it automatically...
UPDATE:
The proposed solution below helped me to solve the problem.
This is what worked for me eventually:
RewriteCond C:/projects/someproject/WS/application/controllers/$1.php -f [OR]
RewriteCond C:/projects/someproject/WS/application/controllers/$1 -d
RewriteRule ^(.+)$ index.php/$1 [L]
RewriteCond $1 ^(.*)/$
RewriteCond C:/projects/someproject/WS/application/controllers/%1 -d
RewriteRule ^(.+)$ index.php/$1 [L]
RewriteCond $1 ^(.*)/[^/]*/?$
RewriteCond C:/projects/someproject/WS/application/controllers/%1.php -f
RewriteRule ^(.+)$ index.php/$1 [L]
# else, rewrite the request to /index.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.html [L]
I've realised I had to check for more types of URLs and also I found I couldn't use %{DOCUMENT_ROOT} because I'm using VirtualDocumentRoot settings and DOCUMENT_ROOT isn't working
Try :
# If /document_root/application/controllers/request.php is a file
RewriteCond %{DOCUMENT_ROOT}/application/controllers/$1.php -f
# Rewrite the request to /index.php
RewriteRule ^(.+)$ /index.php/$1 [NC,L]
# else, rewrite the request to /index.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.html
Is there any rule I can write to make all requests to my image folder point up one folder. I'm dealing with a lot of links so I don't want to put ../
Eg: http://hostingxtreme.com/ajax/images/logo.png.
Should become http://hostingxtreme.com/images/logo.png
Thanks
Here the code I'm using
Dosent seem to be working!
php_value register_long_arrays On
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteEngine On
RewriteRule ^/?ajax/images/logo.png$ /images/logo.png [L,R=301]
</IfModule>
In the htaccess file in your document root, add either:
Redirect 301 /ajax/images/logo.png /images/logo.png
Or if you need to use mod_rewrite:
RewriteEngine On
RewriteRule ^/?ajax/images/logo.png$ /images/logo.png [L,R=301]
I got it!
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (ajax/|ajax/.*/)images/(.*)
RewriteRule (ajax/|ajax/.*/)images/(.*) images/$2
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (ajax/|ajax/.*/)stylesheets/(.*)
RewriteRule (ajax/|ajax/.*/)stylesheets/(.*) stylesheets/$2
My URL structure is like
http://www.example.com/folder/index.php?dir=dir1
To be able to access it from
http://www.example.com/folder/dir1
and at the same time redirect the 1st URL to 2nd one, my htaccess (in 'folder') is
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301,NE]
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php?dir=$1 [L,QSA]
The Redirect and Rewrite were working perfect until recently I switched from NGINX back to APACHE.The trouble is that it is now also rewriting file extensions which I don't want!How can I only apply it only to directories and exclude ALL files from it?
Figured it out! :)
Actually had to add
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
before the first rewrite!
So the htaccess looks like:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]