0

我确定这已得到解答,我已经阅读了几个小时,但无处可去。我今晚需要这个工作,我的大脑正在受伤,所以我正在向美妙的互联网寻求帮助。

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

  • /about => index_new.php?page=about
  • /installations => index_new.php?page=about
  • /installations/{site_id} => index_new.php?page=site&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文件在那里。

4

1 回答 1

2

假如:

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

  2. .htaccess 文件位于根目录。

您可以尝试这个而不是您问题中的规则集:

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] 
于 2013-03-13T01:48:33.463 回答