【问题标题】:Can I do this with an .htaccess file?我可以使用 .htaccess 文件执行此操作吗?
【发布时间】:2011-10-19 16:36:52
【问题描述】:

我使用的主机只允许 PHP 文件位于名为 /cgi/ 的子目录中

但现在我所有的链接都是/cgi/..

请原谅我的愚蠢,但我可以在我的 .htaccess 文件中重写,以使网站认为 /cgi/ 是根目录,以便我的所有链接和网站本身似乎都在根目录中?

这是我所拥有的,但它会导致 500 错误:

# Turn on rewrites.
RewriteEngine on

# Only apply to URLs on this domain
RewriteCond %{HTTP_HOST} ^(www.)?thegrancenturions.com$

# Only apply to URLs that aren't already under folder.
RewriteCond %{REQUEST_URI} !^/cgi/

# Don't apply to URLs that go to existing files or folders.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all those to insert /folder.
RewriteRule ^(.*)$ /cgi/$1

# Also redirect the root folder.
RewriteCond %{HTTP_HOST} ^(www.)?thegrancenturions.com$
RewriteRule ^(/)?$ cgi/index.php [L]

【问题讨论】:

  • 您的主机是否允许重写?如果他们让您将 PHP 放入 /cgi/,如果他们让您在 .htaccess 中覆盖很多内容,我会感到惊讶...
  • 是的,他们说他们会将 HTML 文件放在根目录中并重定向它..

标签: .htaccess url rewrite


【解决方案1】:
RewriteEngine On
RewriteRule ^(.+?)$ cgi/$1 [QSA,L]

我没有测试过,但类似的东西应该可以工作。您可能还需要添加一个RewriteCond 来检查传入的请求是否不是/cgi/..

【讨论】:

    猜你喜欢
    • 2018-07-24
    • 1970-01-01
    • 2013-05-18
    • 2010-10-29
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多