Redirect root to CodeIgniter app in subdir, avoiding infinite loop

こ雲淡風輕ζ 提交于 2019-12-24 08:38:10

问题


I currently have deployed the following .htaccess in the directory of my CodeIgniter app:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

and the following .htaccess in my htdocs root:

RewriteEngine On

# Map / to /foo.
RewriteRule ^$ /foo/ [L]

# Map /x to /foo/x unless there is a x in the web root.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/foo/
RewriteRule ^(.*)$ /foo/$1

Where foo is the directory were my CodeIgniter app lives. This redirects example.com/foo/whatever to example.com/whatever, except if it matches other directories where other apps live (fox example example.com/admin/whatever, which should be kept that way). That part of the behaviour is desired. But now I have two urls for every page on foo, I have example.com/foo/whatever and example.com/whatever. I want to make a 301 redirect for all requests from /foo/x to /x, but if I add

# Redirect /foo/x to /x
RewriteRule ^foo/(.*)$ /$1 [R=301,L]

to the .htaccess I obviously get a redirection infinite loop. The solution described here for a related question doesn't work, because it doesn't redirect to the index.php when needed. How can I achieve what I want? Thanks in advance for any help you may provide.


回答1:


Have this new rule in /foo/.htaccess to redirect /foo/abc to /abc:

RewriteEngine On

RewriteCond %{THE_REQUEST} /foo/(\S*)\s [NC]
RewriteRule ^ /%1 [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]


来源:https://stackoverflow.com/questions/25731052/redirect-root-to-codeigniter-app-in-subdir-avoiding-infinite-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!