Use. htaccess to rewrite urls in a subdirectory only

[亡魂溺海] 提交于 2019-12-25 02:46:53

问题


I am trying to configure an .htaccess file to reroute all urls to the nearest index subdirectory, like this:

http://example.com/admin/blah/test/whatever should stay in the address bar, but point to:

  • http://example.com/admin/blah/test/whatever/index.php if it exists, otherwise:
  • http://example.com/admin/blah/test/index.php if it exists, otherwise:
  • http://example.com/admin/blah/index.php if it exists, otherwise:
  • http://example.com/admin/index.php

If the url is that of an actual file, it should always go to that file. so http://example.com/admin/blah/file.css or http://example.com/admin/blah/item.inc should all still work if they exist, otherwise it should redirect to index.php in that folder, or nearest parent folder like shown above.

I'd also like to have this only affect a subfolder, if that's at all possible. In the above example, I'm assuming the .htaccess file would go in the /admin/ folder.

Update: Here's what currently works:

# No Directory Listings or trailing slashes
Options -Multiviews -Indexes +FollowSymLinks

<IfModule mod_rewrite.c>

RewriteEngine On
DirectorySlash Off

# See if the current request is a directory containing an index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule ^(.*)/?$ $1/index.php [L,QSA]


# if index.php doesn't exist in current dir then
# forward to the parent directory of current REQUEST_URI
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1$2/index.php !-f
RewriteRule ^(.*?/)?([^/]+)/?$ $1 [L,QSA]

</IfModule>

This mostly works. If I type in http://example.com/admin?p=2 it resolves as expected, but if I use the URL http://example.com/admin/?p=2 it ALSO resolves, without explicitly removing the trailing slash.


回答1:


This is fairly tricky rule. Try this in your your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

# if index.php doesn't exist in current dir then
# forward to the parent directory of current REQUEST_URI
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1$2/index.php !-f
RewriteRule ^(.*?/)?([^/]+)/?$ $1 [L]



回答2:


Here's what eventually worked.

In my .htaccess file I had:

# No Directory Listings
Options -Multiviews -Indexes +FollowSymLinks

<IfModule mod_rewrite.c>

RewriteEngine On
DirectorySlash Off

# see if the current directory contains an index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule ^(.*)/?$ $1/index.php [L,QSA]


# if index.php doesn't exist in current dir then
# forward to the parent directory of current REQUEST_URI
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1$2/index.php !-f
RewriteRule ^(.*?/)?([^/]+)/?$ $1 [L,QSA]

</IfModule>

Links to existing directories look for an index.php in that directory. Links to existing files display that file. Everything else redirects to my index.php. From there, I can read any queries with $_GET as normal and I can read and parse the full URL to see what page to display. This also works in a subdirectory if I don't want it to apply to an entire site.



来源:https://stackoverflow.com/questions/21147556/use-htaccess-to-rewrite-urls-in-a-subdirectory-only

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