0

I have directory structure like this:

public_html
.htaccess[1]
  -apps     
  -.htaccess[2]
    -admin
    -myviwo

when I request http://localhost/mysite/admin it redirect me to http://localhost/mysite/apps/admin/ and shows me the content of the admin directory, if I request http://localhost/mysite/admin/ it doesn't redirect me but it shows me the content of admin directory again which is correct. But I want:

http://localhost/mysite/admin

http://localhost/mysite/admin/

Both of the above URLs shows me the content of admin directory without redirecting me.

.htaccess [1]

RewriteEngine On

RewriteBase /
RewriteRule (.*) apps/$1 [L]

.htaccess [2]

RewriteEngine On

RewriteBase /
RewriteRule ^admin/?(.*)$ admin/$1 [L]
RewriteRule ^(.*)$ myviwo/$1 [L]

How can I achieve this?

4

1 回答 1

1

In the admin directory, add an htaccess file with the following:

DirectorySlash Off

This makes it so mod_dir won't redirect requests for the directory admin. However, note that there's an important reason why directory slash is "on" by default:

Security Warning

Turning off the trailing slash redirect may result in an information disclosure. Consider a situation where mod_autoindex is active (Options +Indexes) and DirectoryIndex is set to a valid resource (say, index.html) and there's no other special handler defined for that URL. In this case a request with a trailing slash would show the index.html file. But a request without trailing slash would list the directory contents.

If that's ok with you, then that's all that you need.

Otherwise, you may need to add a special rule specifically for admin. In the .htaccess[1] file, add right below the rewrite base:

RewriteRule ^admin$ apps/admin/ [L]

EDIT: to make the above rule dynamic, you need to first check if it's a directory:

RewriteCond %{DOCUMENT_ROOT}/apps%{REQUEST_URI} -d
RewriteRule ^(.*[^/])$ apps/$1/ [L]
于 2013-08-16T17:36:16.940 回答