簡體   English   中英

Apache-在httpd.conf中使用mod_rewrite刪除.php和.html文件擴展名

[英]Apache - remove .php and .html file extensions using mod_rewrite in httpd.conf

在Ubuntu 14.04上運行Apache 2。 啟用了rewrite_module( sudo apachectl -M )。

在/etc/apache2/apache2.conf(httpd.conf的Ubuntu版本)中,我具有以下代碼塊:

<Directory /var/www/>
    <IfModule mod_rewrite.c>
        RewriteEngine On

        RewriteCond /%{REQUEST_FILENAME}.php -f
        RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php

        RewriteCond /%{REQUEST_FILENAME}.html -f
        RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.html
    </IfModule>

    <IfModule mod_expires.c>
        ...

        <IfModule mod_headers.c>
            ...
        </IfModule>
    </IfModule>
</Directory>

sudo service apache2 restart

當我訪問服務器上沒有.php文件擴展名的URL時,我得到404! 為什么這不起作用?

您的規則在htaccess上下文中對我來說很好用。 但是,當我將這些規則添加到serverConfig上下文中時,服務器返回了未找到的404。 實際上,問題在於在serverConfig上下文中,需要在RewriteRule的模式使用前導斜線 因此,您需要在Rule的模式中添加一個斜杠:

RewriteRule ^/([a-zA-Z0-9_-\s]+)/?$ /$1.php

我終於弄明白了。 對我有用的是:

<Directory /var/www/>
    <IfModule mod_rewrite.c>
        RewriteEngine On

        RewriteCond %{REQUEST_FILENAME}.php -f
        RewriteRule ^(.*)$ $1.php [L]

        RewriteCond %{REQUEST_FILENAME}.html -f
        RewriteRule ^(.*)$ $1.html [L]
    </IfModule>

    <IfModule mod_expires.c>
        ...

        <IfModule mod_headers.c>
            ...
        </IfModule>
    </IfModule>
</Directory>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM