Changing PHP Laravel Web-based Application URL

Posted on

URL Laravel-based Web Application’s Modification

Main Problem :

There has been a request which is made to change URL of a Laravel-based Web Application. We will lay out the scenarios as follows :

We have a main server which can be accessed public or from the internet connection in general as follows :

http://www.domain.com

The domain name above is a fake one which is not the actual name used, it is just for an example. And we have another application which is a Laravel-based Web Application which has already been set up in another server but cannot be accessed directly from the internet since it is not available in public address.

We are using  Apache Webserver ProxyPass feature to access the Laravel-based Web Application which is currently available in the following  address :

http://www.domain.com/old_url_laravel_app

And now are we going to change it to another URL Address for an example :

http://www.domain.com/new_url_laravel_app

Below is a diagram describing the situation above :

This is the existing condition which is now have already implemented :

existing-condition
Existing Implementation Diagram of accessing web-based application Laravel from other Server using Apache Webserver’s ProxyPass feature

And the new  condition which is going to be like the one shown below :

new-condition

The Public or Main Apache Webserver and also the Local or Internal Apache Webserver has the the same configuration. Both of them has the following specification :

Operating System            : CentOS 7.1

The operating system’s version can be checked by typing the following command :

cat /etc/redhat-release
[root@hostname ]# cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)
[root@hostname ]#
Webserver                         : Apache Webserver  2.4.6

Solution :

Reconfigure the Apache Webserver configuration file both in the Public’s Server and in the Local’s server as follows :

  1. Server Public :

Operating System            : CentOS 7.1

[root@publicserver ]# cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)
[root@publicserver ]#

Webserver                         : ApacheWebserver 2.4.6

[root@publicserver ]$ httpd –v
Server version: Apache/2.4.6 (CentOS)
Server built:   Aug 24 2015 18:11:25
[root@publicserver ]$

This server just act as the gate or initial entry to be passed to the local server. It is needed since this server located in the DMZ (Demiliterized Zone) area so it can be publicly accessed. To be able to pass the request, the Apache Webserver configuration which is actually exists in the form of file is needed to be changed. The location of the file can be found in :

/etc/apache2/conf/httpd.conf

We can use any editor available but in this context I prefer the vi editor. Type the following command to do it :

vim /etc/apache2/conf/httpd.conf

The editing steps of the Apache Webserver configuration is shown below :

Remark the old configuration which is pointing to the old address :

# ProxyPass /old_url_laravel_app   http://192.168.xxx.xxx/old_url_laravel_app
# ProxyPassReverse /old_url_laravel_app http://192.168.xxx.xxx/old_url_laravel_app

Below is the snippet code which is needed to be inserted in the above Apache Webserver configuration file :

ProxyPass /new_url_laravel_app http://192.168.xxx.xxx/new_url_laravel_app
ProxyPassReverse /new_url_laravel_app http://192.168.xxx.xxx/new_url_laravel_app

After  changing the configuration file we need to restart the Apache Webserver’s service as follows :

[root@publicserver ]$ systemctl restart httpd.service
[root@publicserver ]$
  1. Internal Server :

In this context, we give an example for the Internal Server which is represented by a server with an IP Address 192.168.xxx.xxx. In reality, it can be any IP address which is used as a local or intranet server.

Operating System            : CentOS 7.1

[root@localserver ]# cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)
[root@localserver ]#

Webserver                         : Apache Webserver 2.4.6

[root@localserver ]$ httpd –v
Server version: Apache/2.4.6 (CentOS)
Server built:   Aug 24 2015 18:11:25
[root@localserver ]$

This server is a server which is located in the internal network but it is actually the real server which is used to store the source code of the application itself. It acts as the server which is originally processing the request and running the application.

In the Apache Webserver configuration which is actually exists in the form of file is needed to be changed. The location of the file can be found in :

/etc/apache2/conf/httpd.conf

The content which is needed to be added is shown below :

Alias /new_url_laravel_app /var/www/html/laravel_root_folder_app
  1. Remark the old alias script for accessing Laravel’s based Web-Application from
Alias /old_url_laravel_app /var/www/html/laravel_root_folder_app/public

to

# Alias /old_url_laravel_app
/var/www/html/laravel_root_folder_app /public
  1. And after that, add the following line below to the Apache Webserver file configuration :
Alias /new_url_laravel_app /var/www/html/laravel_root_folder_app/public/pre>

So, the whole snippet code for configuration file which is used to access Laravel’s based Web Application is shown as follows :

<VirtualHost *:80>
        ...
        # Alias /old_url_laravel_app /var/www/html/laravel_root_folder_app/public
        Alias /new_url_laravel_app /var/www/html/laravel_root_folder_app/public
        ...
        <Directory /var/www/html>
        ...
        Options -Indexes +FollowSymLinks +MultiView
        AllowOverride all
        Require all granted
        </Directory>
        LogFormat "%h %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
        CustomLog "/var/log/httpd/laravel_app-access.log" combined
        ErrorLog "/var/log/httpd/laravel_app_web-error.log"
</VirtualHost>

The other script which is needed to be changed is the Laravel .htaccess script which is normally located in :

laravel_root_folder_app/public/.htaccess

In the above context, it is located in :

/var/www/html/laravel_root_folder_app/public/.htaccess

Change the following line :

RewriteBase /old_url_laravel_app/

to the following line :

RewriteBase /new_url_laravel_app/

So, the overall .htaccess file’s content is shown as follows :

<IfModule mod_rewrite.c>
            <IfModule mod_negotiation.c>
                  Options -MultiViews
            </IfModule>
            RewriteEngine On
            # RewriteBase /old_url_laravel_app/
            RewriteBase /new_url_laravel_app/
            # Redirect Trailing Slashes If Not A Folder...
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.*)/$ /$1 [L,R=301]
            # Handle Front Controller...
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^ index.php [L]
            # Setting PHP.INI
            php_value post_max_size 100M
            php_value upload_max_filesize 100M
</IfModule>

After  changing the configuration file we need to restart the Apache Webserver’s service as follows :

[root@localserver]$ systemctl restart httpd.service
[root@localserver]$

One thought on “Changing PHP Laravel Web-based Application URL

Leave a Reply