将所有请求重定向到index.php并在页面上显示URL

时间:2015-07-10 19:50:35

标签: javascript .htaccess

我正在尝试将所有请求重定向到index.php并在页面上显示URL。

.htaccess文件;

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

index.php文件;

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="assets/libs/js/jquery-1.11.3.min.js"></script>
        <script type="text/javascript">
            $(function() {
                document.getElementById("body").innerHTML = window.location.pathname;
            })();
        </script>
    </head>
    <body id="body"></body>
</html>

当我进入localhost / test时,一切运行正常,页面上会显示“/ test”,但是,如果我转到localhost/test/anythinghere,页面上不会显示任何内容。我希望发生的一个示例:如果我转到"/test/anything/you/are/cool",则会在页面上显示localhost/test/anything/you/are/cool

编辑:我解决了这个问题,谢谢。

2 个答案:

答案 0 :(得分:0)

因为你把它放在.htaccess中,而且只影响文件夹......你必须把它放在/etc/apache2/sites-available/your-site.conf

<VirtualHost *:8888 *:80>
  DocumentRoot /var/www/help/
  ServerName help.com

  <Directory /var/www/help/>
    Options FollowSymLinks
    AllowOverride All
    AddDefaultCharset utf-8

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ /index.php/$1 [L]
  </Directory>
</VirtualHost>

检查我的重写规则。

我说我的重写...“如果你找不到文件夹或文件,请转到index.php”。这是有道理的,因为我把它放在虚拟主机......

你正试图这样做,但.htaccess控制他的文件夹。

答案 1 :(得分:0)

你可以试试.htaccess Regex,Matt。它对我有用:

RewriteEngine on
RewriteCond  %{REQUEST_FILENAME}   !-f
RewriteCond  %{REQUEST_FILENAME}   !-d
RewriteRule  ^(.*)$  index.php/$1  [L]
相关问题