重写将根文档指向特定页面的规则

时间:2016-04-20 19:36:26

标签: .htaccess mod-rewrite

我正在尝试重写http://www.foo.com/的请求以返回http://www.foo.com/homepage.php(不使用重定向)。我目前的.htaccess重写规则是:

RewriteRule ^$ /homepage.php [L]

安装了mod_rewrite,我在网站conf中将AllowOverride设置为All。其他重写规则按预期工作。如果我将重写规则更改为

RewriteRule ^$ /homepage.php [L,R=301]

...然后我几乎得到了理想的行为;页面被重定向,URL最终附加/homepage.php。但是,我想在不重定向页面的情况下从/homepage.php加载内容。

我还尝试了其他标记,例如PPTR=303等,但没有一个标记比R=301更接近。

我做错了什么?

编辑 - 根据要求提供更完整的.htaccess:

# Don't show directory listings for URLs which map to a directory.
Options -Indexes

# Follow symbolic links in this directory.
Options +FollowSymLinks

# Multiviews creates problems with aliased URLs
Options -Multiviews

# Handle any 404 errors.
ErrorDocument 404 /index.php

# Set the default handler.
DirectoryIndex index.php index.html index.htm

# PHP 5, Apache 1 and 2.
<IfModule mod_php5.c>
  php_flag magic_quotes_gpc                 off
  php_flag magic_quotes_sybase              off
  php_flag register_globals                 off
  php_flag session.auto_start               off
  php_value mbstring.http_input             pass
  php_value mbstring.http_output            pass
  php_flag mbstring.encoding_translation    off
  php_value memory_limit                    256M
</IfModule>

# Requires mod_expires to be enabled.
<IfModule mod_expires.c>
  # Enable expirations.
  ExpiresActive On

  # Cache all files for 2 weeks after access (A).
  ExpiresDefault A1209600

  <FilesMatch \.php$>
    # Do not allow PHP scripts to be cached unless they explicitly send cache
    # headers themselves.
    ExpiresActive Off
  </FilesMatch>
</IfModule>

<IfModule mod_rewrite.c>
  RewriteEngine on

  # Block access to "hidden" directories whose names begin with a period.
  RewriteRule "(^|/)\." - [F]

  # Site is running in a VirtualDocumentRoot
  RewriteBase /

  # link to homepage...
  RewriteRule  ^$  /homepage.php  [L]

  # Pass all requests not referring directly to files in the filesystem to
  # index.php.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^ index.php [L]
</IfModule>

1 个答案:

答案 0 :(得分:0)

您不需要重写规则。只需使用DirectoryIndex指令。

在.htaccess:

的顶部使用此行
DirectoryIndex homepage.php

在你的htaccess中试试这段代码:

  RewriteEngine on

  # Block access to "hidden" directories whose names begin with a period.
  RewriteRule "(^|/)\." - [F]

  # link to homepage...
  RewriteRule ^/?$ homepage.php [L]

  # Pass all requests not referring directly to files in the filesystem to
  # index.php.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^ index.php [L]