重写规则不起作用 - 返回目录列表而不是重定向

Posted

技术标签:

【中文标题】重写规则不起作用 - 返回目录列表而不是重定向【英文标题】:Rewrite rules not working - directory list returned instead of redirect 【发布时间】:2011-11-06 16:46:10 【问题描述】:

请多多包涵,因为我是 mod_rewrite 的新手。我正在尝试设置一个规则,例如 domain.com/subfolder 将重定向到 domain.com/subfolder/subfolderindex.php,并且 domain.com/subfolder/subcategory 将重定向到 domain.com/subfolder/subcategory.php .我正在 XAMPP 中进行测试。我在 .htaccess 中有以下内容:

RewriteEngine On
RewriteBase /
RewriteRule ^([a-z]+)/$ $1/$1.php
RewriteRule ^([a-z]+)/([a-z]+)$ $1/$2.php

我得到的是子文件夹目录列表,而不是重定向。在 Mac 上的内置服务器上进行测试时,重写规则工作得非常好。也许差异的原因是 httpd.conf 中的某些东西? loadmodule 行未注释,每个 AllowOverride 设置为 All。

这是 httpd.conf:

AccessFileName .htaccess


ServerRoot "C:/xampp/apache"


Listen 80


LoadModule actions_module modules/mod_actions.so
LoadModule alias_module modules/mod_alias.so
LoadModule asis_module modules/mod_asis.so
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule auth_digest_module modules/mod_auth_digest.so
#LoadModule authn_alias_module modules/mod_authn_alias.so
#LoadModule authn_anon_module modules/mod_authn_anon.so
#LoadModule authn_dbd_module modules/mod_authn_dbd.so
#LoadModule authn_dbm_module modules/mod_authn_dbm.so
LoadModule authn_default_module modules/mod_authn_default.so
LoadModule authn_file_module modules/mod_authn_file.so
#LoadModule authnz_ldap_module modules/mod_authnz_ldap.so
#LoadModule authz_dbm_module modules/mod_authz_dbm.so
LoadModule authz_default_module modules/mod_authz_default.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_host_module modules/mod_authz_host.so
#LoadModule authz_owner_module modules/mod_authz_owner.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule autoindex_module modules/mod_autoindex.so
#LoadModule bucketeer_module modules/mod_bucketeer.so
#LoadModule cache_module modules/mod_cache.so
#LoadModule case_filter_module modules/mod_case_filter.so
#LoadModule case_filter_in_module modules/mod_case_filter_in.so
#LoadModule cern_meta_module modules/mod_cern_meta.so
LoadModule cgi_module modules/mod_cgi.so
#LoadModule charset_lite_module modules/mod_charset_lite.so
#LoadModule dav_module modules/mod_dav.so
#LoadModule dav_fs_module modules/mod_dav_fs.so
LoadModule dav_lock_module modules/mod_dav_lock.so
#LoadModule dbd_module modules/mod_dbd.so
#LoadModule deflate_module modules/mod_deflate.so
LoadModule dir_module modules/mod_dir.so
#LoadModule disk_cache_module modules/mod_disk_cache.so
#LoadModule dumpio_module modules/mod_dumpio.so
#LoadModule echo_module modules/mod_echo.so
LoadModule env_module modules/mod_env.so
#LoadModule example_module modules/mod_example.so
#LoadModule expires_module modules/mod_expires.so
#LoadModule ext_filter_module modules/mod_ext_filter.so
#LoadModule file_cache_module modules/mod_file_cache.so
#LoadModule filter_module modules/mod_filter.so
LoadModule headers_module modules/mod_headers.so
#LoadModule ident_module modules/mod_ident.so
#LoadModule imagemap_module modules/mod_imagemap.so
LoadModule include_module modules/mod_include.so
LoadModule info_module modules/mod_info.so
LoadModule isapi_module modules/mod_isapi.so
#LoadModule ldap_module modules/mod_ldap.so
#LoadModule logio_module modules/mod_logio.so
LoadModule log_config_module modules/mod_log_config.so
#LoadModule log_forensic_module modules/mod_log_forensic.so
#LoadModule mem_cache_module modules/mod_mem_cache.so
LoadModule mime_module modules/mod_mime.so
#LoadModule mime_magic_module modules/mod_mime_magic.so
LoadModule negotiation_module modules/mod_negotiation.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
#LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
#LoadModule proxy_connect_module modules/mod_proxy_connect.so
#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
#LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule setenvif_module modules/mod_setenvif.so
#LoadModule speling_module modules/mod_speling.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule status_module modules/mod_status.so
#LoadModule substitute_module modules/mod_substitute.so
#LoadModule unique_id_module modules/mod_unique_id.so
#LoadModule userdir_module modules/mod_userdir.so
#LoadModule usertrack_module modules/mod_usertrack.so
#LoadModule version_module modules/mod_version.so
#LoadModule vhost_alias_module modules/mod_vhost_alias.so

<IfModule !mpm_netware_module>
<IfModule !mpm_winnt_module>
#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.  
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User daemon
Group daemon

</IfModule>
</IfModule>


ServerName localhost:80


DocumentRoot "C:/xampp/htdocs"


<Directory />
    Options FollowSymLinks -Indexes
    AllowOverride All
    Order deny,allow
    Deny from all
</Directory>


<Directory "C:/xampp/htdocs">
   Options Indexes FollowSymLinks Includes ExecCGI
   AllowOverride All
   Order allow,deny
   Allow from all
</Directory>

<IfModule dir_module>
    DirectoryIndex index.php index.pl index.cgi index.asp index.shtml index.html index.htm \
                   default.php default.pl default.cgi default.asp default.shtml default.html default.htm \
                   home.php home.pl home.cgi home.asp home.shtml home.html home.htm
</IfModule>


<FilesMatch "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy All
</FilesMatch>


ErrorLog "logs/error.log"

#ScriptLog "logs/cgi.log"


LogLevel warn

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%Refereri\" \"%User-Agenti\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      # You need to enable mod_logio.c to use %I and %O
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%Refereri\" \"%User-Agenti\" %I %O"     combinedio
    </IfModule>


    #CustomLog "logs/access.log" common


    CustomLog "logs/access.log" combined
</IfModule>

<IfModule alias_module>
#
# Redirect: Allows you to tell clients about documents that used to 
# exist in your server's namespace, but do not anymore. The client 
# will make a new request for the document at its new location.
# Example:
# Redirect permanent /foo http://localhost/bar

#
# Alias: Maps web paths into filesystem paths and is used to
# access content that does not live under the DocumentRoot.
# Example:
# Alias /webpath /full/filesystem/path
#
# If you include a trailing / on /webpath then the server will
# require it to be present in the URL.  You will also likely
# need to provide a <Directory> section to allow access to
# the filesystem path.

#
# ScriptAlias: This controls which directories contain server scripts. 
# ScriptAliases are essentially the same as Aliases, except that
# documents in the target directory are treated as applications and
# run by the server when requested rather than as documents sent to the
# client.  The same rules about trailing "/" apply to ScriptAlias
# directives as to Alias.
#
ScriptAlias /cgi-bin/ "C:/xampp/cgi-bin/"

</IfModule>

<IfModule cgid_module>
#
# ScriptSock: On threaded servers, designate the path to the UNIX
# socket used to communicate with the CGI daemon of mod_cgid.
#
#Scriptsock "logs/cgi.sock"
</IfModule>

#
# "C:/xampp/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
<Directory "C:/xampp/cgi-bin">
    AllowOverride All
    Options None
    Order allow,deny
    Allow from all
</Directory>


DefaultType text/plain

<IfModule mime_module>
    TypesConfig "conf/mime.types"
    #AddType application/x-gzip .tgz
    #AddEncoding x-compress .Z
    #AddEncoding x-gzip .gz .tgz
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddHandler cgi-script .cgi .pl .asp
    #AddHandler type-map var
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>

<IfModule mime_magic_module>
    MIMEMagicFile "conf/magic"
</IfModule>


#EnableMMAP off
#EnableSendfile off


# XAMPP specific settings
Include "conf/extra/httpd-xampp.conf"

# Perl settings
Include "conf/extra/httpd-perl.conf"

# Server-pool management (MPM specific)
Include "conf/extra/httpd-mpm.conf"

# Multi-language error messages
Include "conf/extra/httpd-multilang-errordoc.conf"

# Fancy directory listings
Include "conf/extra/httpd-autoindex.conf"

# Language settings
Include "conf/extra/httpd-languages.conf"

# User home directories
Include "conf/extra/httpd-userdir.conf"

# Real-time info on requests and configuration
Include "conf/extra/httpd-info.conf"

# Virtual hosts
Include "conf/extra/httpd-vhosts.conf"

# Distributed authoring and versioning (WebDAV)
# Attention! WEB_DAV is a security risk without a new userspecific configuration for a        secure authentifcation 
# Include "conf/extra/httpd-dav.conf"

Include "conf/extra/httpd-proxy.conf"

Include "conf/extra/httpd-default.conf"

Include "conf/extra/httpd-ssl.conf"

<IfModule ssl_module>
    SSLRandomSeed startup builtin
    SSLRandomSeed connect builtin
    SSLSessionCache "shmcb:logs/ssl.scache(512000)"
    SSLSessionCacheTimeout 300
</IfModule>

# AJP13 Proxy
<IfModule mod_proxy.c>
<IfModule mod_proxy_ajp.c>
Include "conf/extra/httpd-ajp.conf"
</IfModule>
</IfModule>

【问题讨论】:

除了子文件夹/子文件夹索引和子文件夹/子类别之外,你能举个例子吗? 例如domain.com/services -> domain.com/services/services.php & domain.com/services/printing.php 【参考方案1】:

试试这个:

RewriteEngine On
RewriteBase /
RewriteRule ^([a-z]+)(/?)$ /$1/$1.php [NC,QSA,L]
RewriteRule ^([a-z]+)/([a-z]+)(/?)$ /$1/$2.php [NC,QSA,L]

这些规则将给出以下结果:

http://www.domain.tld/services => http://www.domain.com/services/services.php
http://www.domain.tld/services/ => http://www.domain.com/services/services.php

http://www.domain.tld/services/printing => http://www.domain.com/services/printing.php
http://www.domain.tld/services/printing/ => http://www.domain.com/services/printing.php

【讨论】:

谢谢,但这并没有改变任何东西 - 我仍在获取目录列表。 是否启用了 mod_rewrite?我尝试使用我的服务器并且它工作正常,您能否提供代码示例或您正在使用您的 php 文件做什么? 从 httpd.conf 中可以看到,应该启用 mod_rewrite。链接到的 php 文件没有任何东西会干扰其他任何东西。它也可以在我的 Mac 上运行,只是无法弄清楚为什么它不能在我的 Windows 服务器上运行(在 Xampp 和 QuickPHP 中都尝试过,结果相同)。 不幸的是,我不使用 Windows,所以我会在这里猜测。尝试禁用自动索引:#Include "conf/extra/httpd-autoindex.conf"

以上是关于重写规则不起作用 - 返回目录列表而不是重定向的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的 .htaccess 301 重定向规则不起作用?

添加代理通行证后,apache重写规则不起作用

当查询为“索引”时,htaccess 重定向不起作用

当 REQUEST_URI 不以不同的模式开头时,Htaccess 重写规则不起作用

从 http 到 https 的重定向不起作用

magento:某些 htaccess 重定向不起作用