LoginSignup
11
12

More than 5 years have passed since last update.

Apacheの.htaccessで詰まった話

Posted at

.htaccessを使って
phpファイルの拡張子を消して下記のように
ドメイン/index.php
    ↓
ドメイン/index
アクセスできるようにしようとしたら詰まったのでそれの解決方法のお話+備忘録

.htaccessを利用できるように設定ファイルの変更

設定ファイルとディレクトリのところは読み替えてください

変更前
$ sudo vim /etc/apache2/sites-available/****.conf

<Directory "/home/name/public_html/">
                Options Indexes FollowSymLinks
                AllowOverride None
                Require all granted
</Directory>

.htaccessを利用したいディレクトリの設定のところのAllowOverrideのNoneをAllに変更

変更後
<Directory "/home/name/public_html/">
                Options Indexes FollowSymLinks
                AllowOverride All
                Require all granted
</Directory>

.htaccessを配置する

.htaccess
$ vim ~/public_html/.htaccess

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

ファイルを作ってアクセスする

index.php
<html>
 <body>
 <?php
   echo "拡張子なしでアクセスできるかのテスト";
 ?>
 </body>
</html>

こんなファイルを作りアクセスしてみるが
ドメイン/indexでも、ドメイン/indexでも500エラーが返る。

原因を調べる

$ less /etc/var/log/apache2/error.log

[Mon Apr 11 22:49:20.736625 2016] [core:alert] [pid 19815]  
/home/name/public_html/.htaccess: Invalid command 'RewriteEngine', 
perhaps misspelled or defined by a module not included in the server configuration

moduleが入ってないみたいなエラーを見つける。
RewriteEngineがないようなのでmod_rewriteを有効にする。詳しくはここ

$ cd /etc/apache2/mods-available/
$ sudo a2enmod rewrite
$ sudo service apache2 restart

再度アクセスする

アクセスができるようになりました。
スクリーンショット 2016-04-11 23.33.32.png

最後に

なんか困ったらエラーログ見てググるとだいたい解決します。

11
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
12