Asked  8 Months ago    Answers:  5   Viewed   45 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

45

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.

Wednesday, March 31, 2021
 
tika
answered 8 Months ago
34

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

Wednesday, March 31, 2021
 
akohout
answered 8 Months ago
74

I doubt the magic cookie trick, also known as the shebang, would work on Windows. This is a UNIX/Linux feature.

You'll have to specify the PHP interpreter and the script as its argument (see also http://www.webmasterworld.com/forum92/859.htm):

RewriteMap router "prg:C:/Program Files/PHP5.3.2/php-win.exe C:/dev/web/www/routing.php"

If that doesn't work, it might be because of the space in Program Files. Windows supports a short name in such cases. For example PROGRA~1 is a typical short name, but the digit in the name is assigned on a first-come first served basis, so you should double-check with the DIR command.

Or else you could move your php-win.exe executable to a directory that doesn't contain spaces.

If it isn't totally clear already, I'll say this: Windows sucks.

Saturday, May 29, 2021
 
Hat
answered 5 Months ago
Hat
11

You should have these lines in your composer.json

"require-dev": {
    "heroku/heroku-buildpack-php": "*"
 }

be sure to run composer update after you add them.

Friday, August 6, 2021
 
Aidan D
answered 3 Months ago
99

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, August 19, 2021
 
e_i_pi
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share