【问题标题】:Issue with including a page and mod_rewrite包含页面和 mod_rewrite 的问题
【发布时间】:2014-09-19 21:35:00
【问题描述】:

我有一个小问题,包括文件,然后用 mod_rewrite 重写了 url。 我正在使用 $_GET 在我的 index.php 中包含页面。我的代码如下所示:

if(isset($_GET['page']))
{
    $file = "./pages/$page.php";
    if (file_exists($file))
    {
        include($file);
    }

这段代码运行良好,我可以使用 domain.tld/index.php?page=example 访问所有页面。但我想在它上面使用 mod_rewrite,这样我就可以通过 domain.tld/Example 访问页面。这就是我的问题。每当我打开这个链接时,我都可以看到这个页面的内容,所以它可以工作,但是没有 css,当我打开 .css 文件时,它向我显示 404 错误。在 $_GET 上使用 print_r 后,我的 $_GET['page'] 看起来像这样:数组( [page] => css/menu.css )。所以 php 将指向我的 css 页面的链接解释为包含页面的值。任何人都可以在这个话题上帮助我,我很困惑如何处理它......

Options +FollowSymLinks -MultiViews


ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 500 /500.html

RewriteEngine on
RewriteBase /timetable
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L]`

提前抱歉我的英语不好^^

【问题讨论】:

  • 你能展示你现有的重写规则吗?
  • Options +FollowSymLinks -MultiViews ErrorDocument 401 /401.php ErrorDocument 403 /403.php ErrorDocument 404 /404.php ErrorDocument 500 /500.html RewriteEngine on RewriteBase /timetable RewriteRule ^(.+)$ index.php?page=$1 [L,QSA] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.+)$ $1.php [L] 这是我完整的.htaccess
  • 请使用问题下方的编辑按钮将其编辑到您的问题中。

标签: php regex apache .htaccess mod-rewrite


【解决方案1】:

RewriteCond 仅适用于最近的RewriteRule。您的第一条规则是重写每个文件和目录,这也是您需要通过使用 RewriteCond 来避免的。

用这个替换你的规则:

Options +FollowSymLinks -MultiViews

ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
ErrorDocument 500 /500.html

RewriteEngine on
RewriteBase /timetable/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]

【讨论】:

  • 啊,感谢您的快速回答,忘记在此规则上使用条件。 :)
猜你喜欢
  • 2011-09-20
  • 1970-01-01
  • 2015-04-03
  • 1970-01-01
  • 2020-12-14
  • 1970-01-01
  • 2011-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多