开发者

always preserve URL when redirecting to subfolder

开发者 https://www.devze.com 2023-01-08 06:41 出处:网络
I want to redirect all requests to a certain path on my server (/app) to a subdirectory at /app/app_site. Following rewrite rules do the job for requests like \'http://localhost/app/somepage.htm\':

I want to redirect all requests to a certain path on my server (/app) to a subdirectory at /app/app_site. Following rewrite rules do the job for requests like 'http://localhost/app/somepage.htm':

RewriteCond %{REQUEST_URI} !^/app/app_site.*$
RewriteCond %{REQUEST_URI} !^/app_site.*$
RewriteRule ^/开发者_如何学Capp(.*)     /app/app_site$1 [L,PT]

This results in the correct page, while preserving the URL. Also, 'http://localhost/app/' will fetch the index page at /app/app_site/index.html, while preserving the URL 'http://localhost/app/'.

However, when I enter 'http://localhost/app', following things happen:

  • the correct page is fetched, at /app/app_site/index.html
  • yet, the URL is redirected to 'http://localhost/app/app_site/'

I'm nearly there, but would like to preserve the URL in all cases (also those without trailing slash). Anyone have a clue how to do this? Thanks!


This is the expected behaviour with DirectorySlash enabled, because you've rewritten to a directory that lacks a trailing slash, and mod_dir performs this cleanup after you've rewritten the URL with mod_rewrite.

The easiest solution is to rewrite the URL so that it always at least matches the slash-terminated directory path, like so:

RewriteCond %{REQUEST_URI} !^/app/app_site.*$
RewriteCond %{REQUEST_URI} !^/app_site.*$
RewriteRule ^/app/?(.*)$     /app/app_site/$1 [L,PT]

This prevents mod_dir from having to add the trailing slash, and therefore avoids the external redirection to /app/app_site/ you're experiencing now.

0

精彩评论

暂无评论...
验证码 换一张
取 消