采用。 htaccess仅重写子目录中的URL [英] Use. htaccess to rewrite urls in a subdirectory only
问题描述
我正在尝试配置.htaccess文件以将所有url重新路由到最近的索引子目录,例如:
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 应该留在地址栏中,但指向:
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 (如果存在),否则:
- http://example.com/admin/blah/test/index.php (如果存在),否则:
- http://example.com/admin/blah/index.php (如果存在),否则:
- http:// example .com / admin / index.php
- 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
如果url是实际文件的URL,则应始终转到那f ile。因此 http://example.com/admin/blah/file.css 或 http://example.com/admin/blah/item.inc 应该仍然可以正常使用存在,否则应重定向到该文件夹或最接近的上级文件夹(如上所示)中的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.
我也希望此操作仅影响子文件夹,如果有可能的话。在上面的示例中,我假设.htaccess文件位于/ admin /文件夹中。
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>
这通常有效。如果我键入 http://example.com/admin?p=2 ,它将按预期方式解析,但如果我使用URL http://example.com/admin/?p=2也可以解决,而无需显式删除斜杠。
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.
推荐答案
这是最终有效的方法。
Here's what eventually worked.
在我的.htaccess文件中,
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>
到现有目录的链接在该目录中查找index.php。指向现有文件的链接将显示该文件。其他所有内容都重定向到我的index.php。从那里,我可以照常读取$ _GET的任何查询,并且可以读取和解析完整的URL以查看要显示的页面。如果我不希望它应用于整个网站,那么它也可以在子目录中使用。
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.
这篇关于采用。 htaccess仅重写子目录中的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!