简体   繁体   English

如何不使用.htaccess重写URL?

[英]How to rewrite URL without .htaccess?

I've been reading up on this on the web, but still could not figure out how to implement it properly. 我一直在网上阅读有关内容,但仍然不知道如何正确实施。 I'd greatly appreciate if you could help me understand how to make url rewrite work without .htaccess. 如果您能帮助我了解如何在没有.htaccess的情况下进行网址重写,将不胜感激。

To check if mod_rewrite is enabled, I ran command ~# sudo apachectl -t -D DUMP_MODULES . 要检查是否启用了mod_rewrite ,我运行了命令~# sudo apachectl -t -D DUMP_MODULES It produced, among others the following module: rewrite_module (shared) . 除其他外,它产生了以下模块: rewrite_module (shared) I don't know if it is the same as mod_rewrite ? 我不知道它是否与mod_rewrite相同?

Folder /etc/apache2/mods-enabled/ has file rewrite.load 文件夹/etc/apache2/mods-enabled/具有文件rewrite.load

I'm not clear which file exactly I should add rewrite rules to? 我不清楚应将重写规则确切添加到哪个文件?

File httpd.conf located in /etc/apache2/ is empty. /etc/apache2/文件httpd.conf为空。 However there's a file named 000-default located in /etc/apache2/sites-enabled/ and it looks like this: 但是,在/etc/apache2/sites-enabled/有一个名为000-default的文件,看起来像这样:

<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>

I'm not clear at all where I should add rewrite rules. 我根本不清楚应该在哪里添加重写规则。 I tried adding them to this 000-default file into <Directory /var/www/> section, but it did not work. 我尝试将它们添加到此000-default文件的<Directory /var/www/>部分中,但是没有用。

You put the rewrite code in the directory block where you're have the .htaccess file (if you use them). 您将重写代码放在具有.htaccess文件的目录块中(如果使用它们)。 For example if /var/www is your docroot, you could put it their: 例如,如果/ var / www是您的文档根目录,则可以将其输入:

    <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>

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

FallbackResource /index.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM