【问题标题】:How can i remove .php file extensions from my webpages on apache2如何从我在 apache2 上的网页中删除 .php 文件扩展名
【发布时间】:2019-11-05 23:44:16
【问题描述】:

我正在 /var/www/html/app 中运行一个 php Web 应用程序,我正在尝试修改 apache 配置,以便在浏览器中隐藏 .php 文件扩展名。 我已经阅读了有关堆栈溢出的多个其他答案,但没有一个解决方案适合我。

我的申请在/var/www/html/app

我的网站使用HTTPS,我一直在修改的配置文件是:

/etc/apache2/sites-enabled/application-ssl.conf

conf文件的内容是:

<VirtualHost *:443>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    ServerName myapplication.com 
    DocumentRoot /var/www/html

    SSLEngine on 
    SSLCertificateFile /etc/ssl/myapplication_com.crt
    SSLCertificateKeyFile /etc/ssl/private/myapplication.com/myapplicationcom.key 
    SSLCertificateChainFile /etc/ssl/myapplication_com.ca-bundle

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf


SecRuleEngine On




<IfModule mod_headers.c>
  <Directory />
    Header always set X-XSS-Protection "1; mode=block"
    Header always set x-Frame-Options "SAMEORIGIN"
    Header always set X-Content-Type-Options "nosniff"
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
    Header always set Referrer-Policy "strict-origin"
  </Directory>
</IfModule>

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
<Directory /var/www/html/app>
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all

        AuthType Basic
        AuthName "Restricted Content"
        AuthUserFile /etc/apache2/.htpasswd
        Require valid-user

</Directory>

目前我已经添加:

Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all

因为这似乎是 apache2 最推荐的解决方案。 我不在/var/www/html/app 中使用 .htaccess 文件,并且 模组重写已启用

我在我的网络应用程序中看不到任何更改,当我在没有 php 的情况下访问文件时,我收到 404 错误,并且日志中出现以下错误:

AH00687: Negotiation: discovered file(s) matching request: /var/www/html/app/betfinder (None could be negotiated).

如何修改我的配置以使其正常工作?

【问题讨论】:

    标签: php ubuntu mod-rewrite apache2


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 当我重新启动 apache2 时,第二行给我一个错误
    【解决方案2】:

    我得到了它的工作:

    <Directory /var/www/html/app>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule (.*) $1.php [L]
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      • 2017-01-16
      • 1970-01-01
      • 2023-04-05
      • 2013-01-09
      • 1970-01-01
      相关资源
      最近更新 更多