繁体   English   中英

采用。 htaccess仅重写子目录中的URL

[英]Use. htaccess to rewrite urls in a subdirectory only

我正在尝试配置.htaccess文件以将所有url重新路由到最近的索引子目录,如下所示:

http://example.com/admin/blah/test/所有应保留在地址栏中的内容,但请指向:

如果该url是实际文件的url,则应始终转到该文件。 因此,如果http://example.com/admin/blah/file.csshttp://example.com/admin/blah/item.inc存在,则它们都应该仍然可以工作,否则应该重定向到index.php文件夹或最近的父文件夹,如上所示。

我也希望只在可能的情况下影响子文件夹。 在上面的示例中,我假设.htaccess文件将放在/ admin /文件夹中。

更新:这是当前有效的方法:

# 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,它也会进行解析,而不会明确删除斜杠。

这是相当棘手的规则。 在您的DOCUMENT_ROOT/.htaccess文件中尝试以下操作:

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]

这是最终有效的方法。

在我的.htaccess文件中,我有:

# 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以查看要显示的页面。 如果我不希望它应用于整个站点,那么它也可以在子目录中使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM