【Thing you want to do】
Redirect access to a specific URL to a fixed file
Assuming there was access to sunday.local / app / get / 1
Redirect to sunday.local / dispatch.php.
I want to parse app / get / 1 in dispatch.php and call it in the internal format (dispatch), but the redirect does not work as expected.
- OS: Ubuntu14.04
- Apache: 2.4.7
- Create VirtualHost settings /etc/apache2/sites-available/sunday.local.conf
AllowOverride All allows the use of .htaccess.
<VirsualHost *:80> ServerName sunday.local ServerAdmin firstname.lastname@example.org DocumentRoot /home/oono/bear/public ErrorLog /var/log/apache2/virtual.host.error.log CustomLog /var/log/apache2/virtual.host.access.log combined LogLevel warn <Directory "/home/oono/bear/public"> Require all granted AllowOverride All </Directory>
- Enable VirtualHost
Run the following command to enable
$ sudo a2ensite sunday.local $ sudo service apache2 reload
- Added to / etc / hosts
I added the following.
Access http://sunday.local/test.html from your browser and check that it is displayed.
- Enable mod_rewrite
Enable Apaceh module rewrite
$ sudo a2enmod rewrite $ sudo service apache2 restart
- Create .htaccess
Create .htaccess in the document root "/home/oono/bear/public" of sunday.local Refer to http://weblabo.oscasierra.net/apache-rewrite-1/
RewriteEngine on RewriteRule ^/$ http://www.google.co.jp [R=302,L]
- Access from a browser
First of all, since it is a test, I expect that if you access "sunday.local /" in an easy-to-understand manner, you will be redirected to "www.google.co.jp", but the screen will be 403 Forbidden.
Is something missing or wrong?
When using rewrite with .htaccess, the relative path from the installed path is entered.
http://sunday.local => "" http://sunday.local/ => "" 【注：最初の / は含まれません。】 http://sunday.local/test.html => "test.html"
Therefore, it does not match the RewriteRule regular expression ^ / $ and does not behave as expected.
If there is no argument, it will be written as ^ $, and if there is an argument, it will be written as ^ (. *) $.
For example, try the following .htaccess.
RewriteEngine on RewriteRule ^$ http://www.google.co.jp/ [R=302,L] RewriteRule ^(.*)$ http://www.google.co.jp/#q=$1 [R=302,L,NE]