Apache Mod Rewrite for Laravel

Apache Mod Rewrite For Laravel

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:\wamp\bin\apache\YOUR APACHE VERSION\conf\extra 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:\windows\system32\drivers\etc\hosts" 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.

Apache rewrite for Laravel /public

I solved it partly. If I have a .htaccess in the root instead of /public with

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/index.php/$1 [L]

I can open http://kemtime2_neu.pr.domain.de/login but the images and css is still wrong. I need to check first if the files exist in /public. I think this is a new question.

Laravel 5 Apache mod_rewrite not working

The .htaccess file won't load up because it is in the public directory, which is really meant to be your document root. As such, you should be accessing the app by going to localhost/page/public/subpage.

If you want to use localhost/page/subpage whilst keeping your directory structure intact, then you need to add a new .htaccess file to the page directory, and delete the .htaccess file from the public directory.

/page/.htaccess contents:

<IfModule mod_rewrite.c>

<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>

RewriteEngine On
RewriteBase /page/

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Send requests to public directory...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ public/index.php [L]

</IfModule>

Unfortunately, due to the way in which Laravel obtains the request information, you will need to group your routes to the page directory in your route configuration:

Route::group(['prefix' => 'page'], function()
{
Route::get('subpage', ['as' => 'subpage', 'uses' => 'GenericPageController@subpage']);
// Your other routes ...
});

Essentially, and in my opinion, this is the easiest way to work around this. You could also move the contents of your public directory up one level and change the paths accordingly in the bootstrap file.

Apache mod_rewrite in IIS

The final solution was editing the web.config file.

In this case, for Laravel 5.7 and IIS, with the value:

<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)/$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="/{R:1}" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Ubuntu 12.04 Laravel and mod rewrite

First, if you are using Laravel 3 make sure that you have updated /application/config/application.php and made the "application index" var to a null value, like "". https://github.com/laravel/laravel/blob/master/application/config/application.php#L42

If you've already done that, try setting up a vhost. It sounds like you are using Apache 2.

First create an additional vhosts file, for example /etc/apache2/sites-available/laravel

<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName laravel.dev
ServerAlias *.laravel.dev
DocumentRoot /home/kriss/projects/laravel/public
</VirtualHost>

Then update your /etc/hosts file and add

127.0.0.1 laravel.dev

Then (and this may be the step you missed before)

sudo a2ensite laravel

This will make a sim link in /etc/apache2/sites-enabled to your vhost config file.
Finally restart your server:

sudo service apache2 restart

You should be able to connect with the url http://laravel.dev, and your rewrites should be working.



Related Topics



Leave a reply



Submit