.htaccess RewriteRule php redirect folder to another - apache

I have a folder test. If a person navigates to the folder test, I want the browser to display new.
The folder new doesn't exist on the server, I just want it to be displayed as new, which on the server is linked to the folder test.
This is my config (httpd-vhosts.conf):
<VirtualHost server.com>
DocumentRoot "c:/www/"
ServerName server.com
Alias /new "c:/www/test"
<Directory "c:/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
So, to be clear, if someone browses to:
server.com/test (the server uses folder test, but server.com/new is shown in the browser.
server.com/ (the same as above => server.com/new is shown in the browser)
server.com/index.php (the server uses index.php from folder test, browser shows server.com/new/index.php
server.com/test/index.php (the same as above)
server.com/test/folder2/index.php?variable=1 (the server uses index.php from folder test/folder2 and gives the variables to the php file. browser shows server.com/new/folder2/index.php?variable=1)
I have tried different .htaccess rewrites, but I can't get it working.
Maybe I don't have to use the alias?
Don't just give me a link to the apache manual, because I've read it, but can't get it to work...

You don't need Alias for this so comment out:
Alias /new "c:/www/test"
Then have these rules:
<VirtualHost server.com>
DocumentRoot "c:/www/"
ServerName server.com
<Directory "c:/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^/?$ /new/ [L,R=301]
RewriteCond %{THE_REQUEST} \s/+test(\S*)\s [NC]
RewriteRule ^ /new%1 [R=301,NE,L]
RewriteRule ^/?new(/.*)?$ /test$1 [L,NC]
</VirtualHost>

Related

How to fix " Not found " when refresh page with vue js SPA in Apache Server?

I just deploy my vue js project to apache server. My file /dist located in /var/www/html/dist. When i visit the page it's work fine. But when i visit in another page and i refresh the page, in browser say 404 Not found. How can i fix this ?
You need a configuration something similar to this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
It will automatically serve index.html file for every request that doesn't have a corresponding static file. You will have to put this .htaccess file. The use of IfModule is explained here.
I have same error, on ubuntu after i install apache.
First i added in /etc/apache2/apache2.conf
AllowOverride All
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
In /etc/apache2/sites-available
<VirtualHost test.ru:80>
ServerName www.test.ru
ServerAlias test.ru
ServerAdmin webmaster#localhost
DocumentRoot /var/www/test.ru
<Directory /var/www/test.ru>
#Разрешение на перезапись всех директив при помощи .htaccess
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Then in console activate rewrite module and restart apache
sudo a2enmod rewrite
service apache2 restart

Setup subdirectory redirect to a Laravel project

I have three projects, First one is a static html website and other 2 projects are Laravel projects.
I have an SSL certificate set up for the main website. I want to setup the other Laravel projects as subdirectories of the main URL.
I added an alias to my conf file, but it doesn't work. I get this error: "Too many redirects occurred trying to open “https://example.com/admin/”. This might occur if you open a page that is redirected to open another page which then is redirected to open the original page."
Are there any changes that I should add to htaccess file?
Here is my website.conf file
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/***********.crt
SSLCertificateKeyFile /etc/apache2/ssl/*********.key
SSLCertificateChainFile /etc/apache2/ssl/*********.crt
ServerAdmin admin#example.com
DocumentRoot /var/www/html/example/public_html/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /admin "/var/www/html/admin"
<Directory "/var/www/html/admin">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Alias /admin2 "/var/www/html/admin2"
<Directory "/var/www/html/admin2">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I would suggest taking a look at Vagrant with Laravel Homestead prebuilt box. This will achieve what you want to do by editing the Homestead.yaml file, the /etc/hosts file, and then provisioning the vagrant machine. You will need virtual box installed to work with vagrant. You can SSH into the machine by typing vagrant ssh inside the homestead directory.
Documentation: https://laravel.com/docs/5.4/homestead
YouTube Video about Vagrant: https://www.youtube.com/watch?v=LyIyyFDgO4o
Youtube Videos about Installing Laravel Homestead: https://www.youtube.com/watch?v=tbPGuikTzKk&list=PLfdtiltiRHWEaImCreOC7UkK4U5Xv9LdI
You should use the public folder as your alias in the apache setup.
Try "/var/www/html/admin2/public" and also for the Directory as well.
Something like this:
Alias /admin2 "/var/www/html/admin2/public"
<Directory "/var/www/html/admin2/public">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Depends on the installation of Laravel, you should have an .htaccess file in the public directory, which might require some additional tweeking.
Also, restart apache once you have made the change.t
You need to correctly set 2 things :
Your sites's alias in WAMP's alias folder e.g. C:\wamp\alias to redirect URL to public\index.php in Laravel's folder
Rewrite base .htaccess in your Laravel's public folder so that Apache rewrites on top of a base path.
Assuming that your site's directory is in C:\wamp\www\site and your alias is localhost/alias
Setting alias to point to public folder:
Alias /site "/var/www/site/public"
<Directory "/var/www/site/public">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Add RewriteBase in .htaccess in Laravel's public folder
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteBase '/site/'
# 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_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

Not able to access laravel routes in apache2 server

I've deployed a new laravel installation to my server, While doing so I configured my apache2 as following:
I added 000-default.conf file in /etc/apache2/sites-available/ as following:
DocumentRoot /var/www/html
<Directory /var/www/html/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
Require all granted
</Directory>
I've my laravel installed into /var/www/html/stellar folder now I access my installation through:
http://52.39.175.55/stellar/public/
But while calling the routes it is not working, Like
http://52.39.175.55/stellar/public/oauth/token
Here is the screenshot:
But suppose I call through this:
http://52.39.175.55/stellar/public/index.php/oauth/token
I get the access,
I tried changing my public/.htaccess file to something like this:
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /stellar
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
Still there is no change, Help me out in this.
Your VirtualHost on /etc/apache2/sites-available/ should look like this:
<VirtualHost *:80>
DocumentRoot "/var/www/html/stellar/public"
ServerName yourdomain.com
<Directory /var/www/html/stellar/public>
AllowOverride All
</Directory>
</VirtualHost>
Then restart apache2 and it should work.
I hardly recommend you to duplicate the 000-default.conf file, rename it and include the VirtualHost, then enable it with a2ensite command, just because it's more easy to manage.

Multiples VirtualHost for Symfony3 projects in AWS development environment

We have a problem with access to multiple Symfony3 projects that are in different folders and targeted to the same IP address but in different ports on EC2, for example:
project1:
52.1.1.1:8080/login
/var/www/html/projects/project1/
project2:
52.1.1.1:8181/login
/var/www/html/projects/project2/
Happens that when entering the Project 1 (52.1.1.1:8080) it is displayed correctly, but afterward when accessing to Project 2 (52.1.1.1:8181), this fails, strangely deploys the information from Project 1.
In another scenario, when rebooting the apache service and entering the Project2 (52.1.1.1:8181) all is displayed correctly, but if after entering in the Project2, we enter the Project 1 (52.1.1.1:8080) the information from Project2 is displayed, instead of that of the Project 1.
Ports 8080 and 8181 are open.
Apache configuration on the server is the following:
Listen 8181
<VirtualHost *:8181>
DocumentRoot "/var/www/html/projects/project1/web"
DirectoryIndex app.php
<Directory "/var/www/html/projects/project1/web">
Require all granted
Order allow,deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
</Directory>
<Directory /var/www/html/projects/project1>
Options FollowSymlinks
</Directory>
<Directory /var/www/html/projects/project1/web/bundles>
<IfModule mod_rewrite.c>
RewriteEngine Off
</IfModule>
</Directory>
CustomLog /var/www/html/projects/project1/var/logs/reg-access.log combined
ErrorLog /var/www/html/projects/project1/var/logs/reg.error.log
</VirtualHost>
Listen 8080
<VirtualHost *:8080>
DocumentRoot "/var/www/html/projects/project2/web"
DirectoryIndex app.php
<Directory "/var/www/html/projects/project2/web">
Require all granted
Order allow,deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
</Directory>
<Directory /var/www/html/projects/project2>
Options FollowSymlinks
</Directory>
<Directory /var/www/html/projects/project2/web/bundles>
<IfModule mod_rewrite.c>
RewriteEngine Off
</IfModule>
</Directory>
CustomLog /var/www/html/projects/project2/var/logs/reg-rm.log combined
ErrorLog /var/www/html/projects/project2/var/logs/reg.error.log
</VirtualHost>
Any help or ideas about what happens?
UPDATED
To access the Project 1, logs the following error:
[2016-08-17 07:33:12] request.CRITICAL: Uncaught PHP Exception Symfony\Component\Debug\Exception\FatalErrorException: "Error: Cannot redeclare class Doctrine\Common\Annotations\Annotation\Target" at /var/www/html/projects/project2/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/Annotation/Target.php line 31 {"exception":"[object] (Symfony\Component\Debug\Exception\FatalErrorException(code: 0): Error: Cannot redeclare class Doctrine\Common\Annotations\Annotation\Target at /var/www/html/projects/project2/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/Annotation/Target.php:31)"} []
"Vendors" is referenced strangely to another project
I found a temporary solution, I had to modify the "app/autoload.php" file
Before
$ loader = require __DIR__.'/../vendor/autoload.php';
After
$ loader = require __DIR__.'/../../project1/vendor/autoload.php';
And it all worked properly.
But I communicated with Javier Eguiluz and he suggested to try installing APC on the server, I installed it, rolled back the changes to the file "app/autoload.php" and it worked properly.
EDIT #2 based on your comment.
Listen 8181
<VirtualHost *:8181>
Servername project1.com
...
Listen 8080
<VirtualHost *:8080>
Servername project2.com
DocumentRoot "/var/www/html/projects/project2/web"
DirectoryIndex app.php
On a remote host edit your hosts file to include the above host names.
For example, if you are on a remote Windows host, edit the file "c:\Windows\System32\Drivers\etc\hosts" as follows:
52.1.1.1 project1.com
52.1.1.1 project2.com
Then in your browser use:
http://project1.com:8181
http://project2.com:8080
This should work. Please investigate first before downvoting. Thanks!

Codeigniter controller in subdirectory not working in virtual host

I need help with CodeIgniter -- trying to it to work in virtual host envirnoment in Ampps.
I have been following the following tutorial:
http://net.tutsplus.com/tutorials/php/basecamp-style-subdomains-with-codeigniter/
I am trying to have two installations of CodeIgniter running in two directories:
/students and
/teachers
www.test.com -> goes to /teachers directory
www.user1.test.com -> goes to /students directory
I have the above working on my local machine. The different URL's take me to the correct directory. CI works. Problem arises when I try to access controller using redirect.
if I try to redirect('error') I get:
user2.test.com/user2.test.com/error
Also get an "internal server error"
if I type in
user2.test.com/error
I get an "internal server error"
if I type in
user2.test.com/index.php/error
I get desired result
It works if I try to load view directly in
$this->load->view('error')
I think I need help with the urls and paths. Any guidance most gratefully received.
Nothing particularly enlightening in CI log files
In Apache logs I find:
Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
Things I have changed:
htaccess in root - changed rewrite base. Also takes out index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /students/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
application/config.php
$config['base_url'] = $_SERVER['HTTP_HOST'];
As per tutorial. Needs to be dynamic.
my Apache hppd-vhosts.conf file
NameVirtualHost 127.0.0.1:80
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "C:/Program Files/Ampps/www/teachers"
ServerName test.com
ServerAlias test.com
<Directory "C:/Program Files/Ampps/www/teachers">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:/Program Files/Ampps/www/students"
ServerName test.com
ServerAlias *.test.com
<Directory "C:/Program Files/Ampps/www/students">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
index.php in root
kept as is. Things got worse when I tried to change these
$system_path = 'system';
$application_folder = 'application';
what about your host file? your host file should be 127.0.0.1 test.com and first try to disable the student sub domain and see if it dose work. Its look like your both ServerName test.com is same try to change the other one with student.com and see whats happen most probably both are conflicting