【问题标题】:ubuntu 14.04LTS apache2 pretty url's not workingubuntu 14.04LTS apache2 漂亮的网址不起作用
【发布时间】:2015-06-26 23:10:57
【问题描述】:

我已经用头撞墙了一段时间,但没有成功。我进行了多次搜索并尝试实施所有建议,但仍然没有乐趣。对于我的设置为什么不起作用的任何建议,我将不胜感激。

我们刚刚设置了带有 LAMP 环境的 Ubuntu 14.04LTS 的服务器,自从我尝试让它与一个测试站点一起工作以来,它在我的本地 XAMPP 设置上运行得非常好。

这是我的设置:

使用 a2enmode 重写启用 mod_rewrite

更新 /etc/apache2.conf 文件以允许覆盖

<Directory /var/www/>
   Options Indexes FollowSymLinks
   AllowOverride All
   Require all granted
</Directory>

在 /etc/apache2/sites-available/example.dev.conf 中设置新的虚拟主机

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/example.dev/public_html
    ServerName example.dev
    ServerAlias www.example.dev
    <Directory /var/www/example.dev/public_html>
        Options Indexes FollowSymLinks
        Require all granted
        AllowOverride all
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

使用 a2ensite example.dev.conf 启用虚拟主机

将 .htaccess 文件从我的本地环境复制到 /var/www/example.dev/public_html 目录

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php !-f
    RewriteRule ^([^\.]+)/$ [NC,L]
</IfModule>
ErrorDocument 404 /404.php

终于用 apache2 reload 重启了 apache

当我尝试转到辅助页面时,我最终还是进入了 404 页面。

我的网站设置是:

example.dev/index.php
    subdirectory
        file1.php
        file2.php
etc.

链接是example.dev/subdirectory/file1.php

如果有人对如何完成这项工作有任何建议,我将不胜感激。

谢谢。

【问题讨论】:

    标签: apache .htaccess ubuntu mod-rewrite


    【解决方案1】:

    您的规则缺少目标 URL,将其用作:

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

    【讨论】:

    • 我已经尝试了你的建议,但仍然不满意。我仍然发送到 404 页面。
    • 它是活动的,当我把废话放进去时它会产生 500 错误,并且 ErrorDocument 工作得很好,因为它把我送到了那里。同样,当我将 .php 添加到文件名的末尾时,它也可以工作。这让我完全困惑。
    • 所以这行得通:example.dev/subdirectory/file1.php 但这是 404 example.dev/subdirectory/file1
    • 谢谢,您更新的规则起到了作用。问题是这个重写条件: RewriteCond %{REQUEST_FILENAME}/.php !-f 一旦我将它更新到这个 RewriteCond %{REQUEST_FILENAME} !-f 一切正常。
    猜你喜欢
    • 2015-10-17
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 2015-11-24
    • 2015-06-01
    • 1970-01-01
    相关资源
    最近更新 更多