mod_rewrite规则给予404

时间:2013-03-13 01:29:19

标签: apache mod-rewrite

我确信这已经得到了解答,我现在已经阅读了几个小时而且无处可去。今晚我需要这个工作,我的大脑很伤人,所以我要求美妙的网络寻求帮助。

我正试图通过index_new.php运行我的所有页面(当我决定这样做时,这一切都是有道理的,我发誓)。我有两种类型的页面,静态和动态。动态页面都是相同类型的页面,但基于数据库(MySQL)的数据不同。我正在尝试进行这些重写

  • / about => index_new.php?页=约
  • / installations => index_new.php?页=约
  • / installations / {site_id} => ?index_new.php页=站点&安培;的siteID = {} SITE_ID

(如果about可能是通用的,我很乐意,但我不想推动我的运气)我的.htaccess文件是:

# enable rewrite
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

# rewrite all physical existing file or folder
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d

# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} "/statics/" [OR]
RewriteCond ${REQUEST_URI} "/ajax/"

# rewrite rules
RewriteRule ^about index_new.php?page=about
RewriteRule ^/installations index_new.php?page=list
RewriteRule ^/installations/(.*) index_new.php?page=site&id=$1

当我尝试转到/about或任何其他页面时,我会收到HTTP 404.请注意,我的根目录是http://localhost/~user/public_html/new,而.htaccess文件就在那里。

1 个答案:

答案 0 :(得分:2)

本发明提供:

  1. 每条规则都必须符合问题的条件。 (它们被重复,因为它们仅对下一个重写规则有效。)

  2. .htaccess文件位于root。

  3. 您可以在问题中尝试此操作而不是规则集:

    Options +FollowSymLinks -MultiViews
    RewriteEngine On
    
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI}  /about/?$      [NC]
    RewriteRule .*  /index_new.php?page=about [NC,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI}  /installations/?$ [NC]
    RewriteRule .*  /index_new.php?page=list     [NC,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI}  /installations/([^/]+)/? [NC]
    RewriteRule .*       /index_new.php?page=site&id=%1 [NC,L]
    

    静默地映射:

    http://example.com/about

    http://example.com/index_new.php?page=about


    http://example.com/installations

    http://example.com/index_new.php?page=about


    http://example.com/installations/Anything

    http://example.com/index_new.php?page=site&Anything


    对于永久和可见的重定向,将[NC,L]替换为[R = 301,NC,L]。

    注意:

    问题中的这些条件没有被使用,因为它们的目的不明确:

       RewriteCond %{REQUEST_URI} "/statics/" [OR]
       RewriteCond ${REQUEST_URI} "/ajax/"
    

    要将它们包含在一个或多个规则中,请尝试以下方法:

    # For NOT condition, include the next line before the corresponding rewrite rule:
    RewriteCond %{REQUEST_URI} !(statics|ajax) [NC]
    
    # For positive condition, include the next line before the corresponding rewrite rule:
    RewriteCond %{REQUEST_URI}  (statics|ajax) [NC]