Viewed   308 times

I have an installation of Laravel on Wampserver. The directory is as follows:

C:wampwwwlaravel

Now URLs are like this:

http://localhost/laravel/public/index.php/home/index

So I used the following htaccess code

Options +FollowSymLinks
Options -indexes
DirectoryIndex index.PHP
RewriteEngine on
RewriteCond $1 !^(index.PHP|images|robots.txt)
RewriteCond %{REQUEST_ FILENAME} !-f
RewriteCond %{REQUEST_ FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L, QSA]

To reduce the URL to

http://localhost/laravel/public/home/index

But the laravel framework insists that all application files reside in the public folder.

So I would like to know what I need to add to (or subtract from) the htaccess file so that the URL can look like

http://localhost/laravel/home/index

Thanks

 Answers

3

When testing locally I do one of two things.

  1. Create a new .htaccess below the public directory with the following.

    <IfModule mod_rewrite.c>
        RewriteEngine on
    
        RewriteRule ^(.*)$ public/$1 [L]
    </IfModule>
    
  2. Create a new virtual host. With WAMP you can navigate to C:wampbinapacheYOUR APACHE VERSIONconfextra and find your httpd-vhosts.conf file, in there you can see example virtual hosts. Here's one of mine:

    <VirtualHost *:80>
        DocumentRoot "c:/wamp/www/laravel/public"
        ServerName laravel.dev
        ServerAlias www.laravel.dev
    </VirtualHost>
    

    Make sure that your vhosts configuration file is being included. Open up your httpd.conf file and search for the vhosts file, uncomment the include line if it's commented out. Then I open the CLI and enter notepad "C:windowssystem32driversetchosts" which opens up your hosts file. Underneath the item that mentions localhost place your new host. Here's an example.

    127.0.0.1  laravel.dev
    

    Make sure you restart Apache and bingo, you should be able to navigate to http://laravel.dev and you won't have any annoying public directory. This is how I achieve it, as I prefer the nicer looking virtual host rather then a long winded localhost URL.

Hope this helps.

Monday, August 29, 2022
5

The serve command is just a shortcut for the PHP Built-in Webserver, something PHP has out of the box, so the point of using it is to start testing your application as fast as you could, you just need to install PHP, Composer and your application is up (if you don't need anything else, of course). But if you already have Nginx installed, there is no point at all, just use it.

It's not wise to use the Builtin Webserver in production.

Wednesday, October 12, 2022
 
2

It is a Laravel & Apache problem, this line in public/.htaccess fixed it for me:

RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

The fix is from https://github.com/dingo/api/issues/54

Friday, November 18, 2022
 
kooiinc
 
3

Basically, you need two rules. One rule to redirect the client to a clean URL and another to internally rewrite the pretty URL to the real content via index.php.

Assuming the index.php and .htaccess is in a directory cdi

RewriteEngine on

# prevent endless loop
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

# redirect the client
RewriteCond %{QUERY_STRING} URL=pages/(.+?).php
RewriteRule ^index.php$ /cdi/%1? [R,L]

# exclude rewriting all files located in /cdi/files
RewriteCond %{REQUEST_URI} !^/cdi/files/
# rewrite to real content
RewriteRule ^.*$ /cdi/index.php?URL=pages/$0.php [L]

Update:

When the request is /cdi/index.php?URL=pages/abc.php, the second rule extracts the needed URL part and redirects the client to the new URL path. The client then requests the new URL /cdi/abc and the third rule takes this and does an internal rewrite to the real content.

This all works fine as it should, but would rewrite and redirect indefinitely. To break this endless rule, the first rule checks the environment %{ENV:...}, if the request was already redirected REDIRECT_STATUS and then stops the cycle with the RewriteRule

RewriteRule ^ - [L]

which matches everything ^ and does no substitution, but ends the rewrite cycle with the flag [L]

Instead of using the system provided environment STATUS/REDIRECT_STATUS, you can also set a variable yourself with the flag E=SEO:1 for example, and then test for this variable with

RewriteCond %{ENV:REDIRECT_SEO} 1

For the REDIRECT_ prefix, see Available Variables.

Thursday, December 8, 2022
 
4

Found a solution that works

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([^.]+)$ /bar.php?code=$1 [QSA,L]
http://example.com/ directs to index.php properly (without showing index.php)
http://example.com/abc directs to bar.php?code=abc
http://example.com/foo.php operates normally.
Wednesday, August 3, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :