mod_rewrite с .htaccess для подмены подкаталога - PullRequest
1 голос
/ 29 декабря 2010

У меня есть приложение django, работающее на поддомене subdomain.domain.com/appname, но я не хочу, чтобы имя приложения отображалось ни в одном из моих URL-адресов. Я сделал это через .htaccess

RewriteEngine On

RewriteCond %{REQUEST_URI} !admin
RewriteCond %{REQUEST_URI} !appname
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /appname/$1 [L]

Это завершает случай, когда запрошенный URL-адрес является subdomain.domain.com/home, и он обслуживается из subdomain.domain.com/appname/home.

Однако я также хотел бы выполнить обратное, когда запрошенный URL-адрес является subdomain.domain.com/appname/home, а отображаемый URL-адрес изменяется на subdomain.domain.com/home, что затем вызывает правило, приведенное выше и подается с subdomain.domain.com/appname/home

Я попробовал следующее, но получил ошибку, что у меня есть цикл

RewriteEngine On

RewriteCond %{REQUEST_URI} appname
RewriteRule ^appname/(.*)$ /$1 [N,R=301]

RewriteCond %{REQUEST_URI} !admin
RewriteCond %{REQUEST_URI} !appname
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /appname/$1 [L]

1 Ответ

5 голосов
/ 29 декабря 2010

Попробуйте без флага 'N':

'next|N' (next round)<br> Re-run the rewriting process (starting again with the first rewriting rule). This time, the URL to match is no longer the original URL, but rather the URL returned by the last rewriting rule. This corresponds to the Perl next command or the continue command in C. Use this flag to restart the rewriting process - to immediately go to the top of the loop.<br> Be careful not to create an infinite loop!

http://httpd.apache.org/docs/current/mod/mod_rewrite.html

...