如何在没有.htaccess的情况下重写URL?

时间:2014-05-30 13:06:04

标签: php apache .htaccess mod-rewrite url-rewriting

我一直在网上阅读,但仍然无法弄清楚如何正确实施它。如果您能帮助我了解如何在没有.htaccess的情况下重写网址,我将非常感激。

要检查是否启用了mod_rewrite,我运行了命令~# sudo apachectl -t -D DUMP_MODULES。它产生了以下模块:rewrite_module (shared)。我不知道它是否与mod_rewrite相同?

文件夹/etc/apache2/mods-enabled/包含文件rewrite.load

我不清楚我应该在哪个文件中添加重写规则?

位于httpd.conf

文件/etc/apache2/为空。但是,000-default中有一个名为/etc/apache2/sites-enabled/的文件,它看起来像这样:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

我根本不清楚应该添加重写规则的地方。我尝试将这些000-default文件添加到<Directory /var/www/>部分,但它不起作用。

2 个答案:

答案 0 :(得分:1)

您将重写代码放在您拥有.htaccess文件的目录块中(如果您使用它们)。例如,如果/ var / www是你的docroot,你可以把它们写成:

    <Directory /var/www/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all

        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule . /index.php [L]
    </Directory>

答案 1 :(得分:1)

如果您使用的是apache 2.2.16或更高版本,请使用

FallbackResource /index.php