LoginSignup
6
8

More than 5 years have passed since last update.

apacheのrewrite_modで特定のパスだけ除外する

Posted at

PHPでフロントコントローラでリクエストを捌くようにしたのだが、その時のURLの設定で悩んだのでメモ。
(もしもっとエキセレントなやり方があったらぜひコメントで教えていただけると嬉しいです・・!)

1.URLがhttp://xxx.xxx.com/index.php/xxx となってしまうのでindex.phpを無くしたいとおもった
2.apacheを利用していたのでhttpd.confでhttp://xxx.xxx.com/ にアクセスしたときに、http://xxx.xxx.com/index.php にリライトされるようバーチャルホスト設定をした。

<VirtualHost *:80>
 ServerName xxx.xxx.com
 DocumentRoot "/var/www/html/xxx.xxxx"
 DirectoryIndex index.html index.php
 ErrorLog /var/log/httpd/xxx.xxx.com_error_log
 AddDefaultCharset UTF-8
 RewriteEngine On
 RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
 RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-d
 RewriteRule . /index.php [L]
 <Directory "/var/www/html/xxx.xxx">
 AllowOverride All
 </Directory>
</VirtualHost>

3.これでok!かと思っていたのだが画像ファイルが軒並み開けなくなる。
4.http://xxx.xxx.com/resouces 以下においてあったファイルが、リライト設定によりhttp://xxx.xxx.com/index.php/resouces と判断されてしまい開けないのだとわかる
5.リライト時にresourcesフォルダだけ除外するようにする。

#下記を追加
 RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !^/resources/(.*)$

6.これで画像ファイルも開けるようになった。

これがメジャーなやり方なのかわからないです。

6
8
6

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
6
8