【问题标题】:Use. htaccess to rewrite urls in a subdirectory only采用。 htaccess 仅在子目录中重写 url
【发布时间】:2014-02-04 12:29:03
【问题描述】:

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

http://example.com/admin/blah/test/whatever 应该留在地址栏中,但指向:

如果 url 是一个实际文件的 URL,它应该总是转到那个文件。所以 http://example.com/admin/blah/file.css 或 http://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 它也会解析,而无需显式删除尾部斜杠。

【问题讨论】:

  • 如果您想要特定于子文件夹的规则,请将 .htaccess 文件放在子文件夹中。

标签: php apache .htaccess mod-rewrite


【解决方案1】:

这是相当棘手的规则。在你的 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]

【讨论】:

  • 这似乎运作良好。我注意到的一件事是在我的本地测试服务器上,我在根目录的子文件夹中有多个站点,所以我不得不将 RewriteBase 硬编码到 /sitefolder/ 这在测试环境中不是问题。不过,有趣的是:它与示例 url 配合得很好,并且 localhost/sitefolder/subfolder/index.php 转到了正确的文件,但 localhost/sitefolder/subfolder/index.php/ 转到了 localhost/ 可能只是测试服务器的怪癖.我会再做一些测试并在一分钟内报告。
  • 非常感谢您的帮助。除了一个小问题外,它都可以工作:我添加了 [L,QSA] 来保存任何查询字符串,效果很好,但是,有没有办法删除尾部斜杠?目前,网址是:example.com/admin/?p=3,我想要这个example.com/admin?p=3,如果我尝试使用它,它会重新添加斜杠。
  • 此规则不添加斜杠。此外,如果/admin/ 是一个目录,那么mod_dir 模块会添加斜杠。
【解决方案2】:

这就是最终奏效的方法。

在我的 .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 以查看要显示的页面。如果我不希望它应用于整个网站,这也适用于子目录。

【讨论】:

    猜你喜欢
    • 2014-01-27
    • 2010-10-26
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    相关资源
    最近更新 更多