Fixing mod_rewrite and mod_alias conflict
I have a VirtualHost setup like this :
Alias /somedir /some/other/dir
http://example.com/somedir works fine
However, I need to setup mod_rewrite
for /somedir (a CodeIgniter app for which I want clean URLs). Here's the bit from the CodeIgniter wiki :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
开发者_开发百科Usually, when mod_rewrite
is used in subdirs changing RewriteBase
to match the name of the dir is enough to make it work :
RewriteBase /somedir
...but it doesn't seem to work with mod_alias
(getting 404s). Is there a workaround for this problem ?
The following worked on my test server, so trying this might solve your problem..
In your virtual host:
Alias /somedir /some/other/dir
RewriteEngine On
# I think these conditions should work correctly; the other ones shouldn't
# because the alias has not been applied at this stage of processing yet
RewriteCond /some/other/dir%{REQUEST_URI} !-d
RewriteCond /some/other/dir%{REQUEST_URI} !-f
# L is unnecessary in this context, but be sure to PT so mod_alias picks up
# on the rewrite
RewriteRule ^/somedir/(.*)$ /somedir/index.php/$1 [PT]
Although I haven't tested your sample, in my own configurations, I haven't had much success with the PT flag, perhaps because of interactions with re-write rules in my config.
I've found it easier to restrict my use of aliases to the rewrite conditions, and always use the a file system path in the rewrite output.
Define SOME_DIR = "/somedir"
Define SOME_OTHER_PATH = "/some/other/dir"
Alias "${SOME_DIR}" "${SOME_OTHER_PATH}"
RewriteEngine On
RewriteCond ${SOME_DIR}%{REQUEST_URI} !-d
RewriteCond ${SOME_DIR}%{REQUEST_URI} !-f
RewriteRule ^${SOME_DIR}/(.*)$ ${SOME_OTHER_PATH}/index.php/$1 [NC,QSA,L]
精彩评论