【问题标题】:Ignore a certain part of the URL via mod_rewrite通过 mod_rewrite 忽略 URL 的特定部分
【发布时间】:2016-06-08 16:13:40
【问题描述】:

我有一个 Silex 应用程序,我收到如下请求: https://.../dev/foo/bar 并想将它们重写为https://.../index.php/foo/bar

而“...”可以是从“www.asd.com”到“localhost/~asd/folder”的任何内容。

我的 .htaccess 文件位于网络根目录 https://.../

这是我正在使用的普通 mod_rewrite 设置:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

我通过设置 RewriteBase 的天真尝试失败了:

  RewriteEngine On
  RewriteBase /dev
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^ index.php [QSA,L]

我得到了错误

Forbidden

You don't have permission to access /dev/index.php on this server.

也只是重写失败:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^dev/(.*) index.php [L]

奇怪的是,它不只是跳过 dev 部分,而是尝试访问 /dev/foo/bar 而不是所需的 /foo/bar。

有什么线索吗?

【问题讨论】:

  • 您的 .htaccess 在哪里?
  • 嗨,我已将其添加到问题中。
  • 或许将其更改为RewriteRule ^dev/(.+)/? index.php?url=$1 [QSA,L] 并使用$_GET['url'] 访问请求的路径。
  • 尝试在您的第一个 .htaccess 顶部添加 Options +FollowSymLinks
  • 目录结构如何?你有/path/to/dev/foo/bar,还是别的什么?

标签: php apache .htaccess mod-rewrite


【解决方案1】:

我通过像这样结合 mod_alias 和 mod_rewrite 为我找到了一个解决方案,RewriteBase 现在可以工作了:

Alias /dev /app/web
<Directory /app/web>
    Options -MultiViews

    RewriteEngine On
    RewriteBase /dev
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [QSA,L]
</Directory>

【讨论】:

    猜你喜欢
    • 2013-10-02
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多