【问题标题】:.htaccess redirect all extension to php.htaccess 将所有扩展重定向到 php
【发布时间】:2013-02-16 00:36:42
【问题描述】:

我最近更新了我的 .htaccess 文件以隐藏 URL 的 .php 扩展名。效果很好,当我尝试访问 domain.com/index 时,它会显示 php 文件。

当我输入 domain.com/index.php 时,它会将我重定向到 domain.com/index url 并同时显示 .php 文件。

出于安全原因,我想将所有其他扩展重定向到无扩展 url 以显示 .php 文件。我的问题是,我无法将 .htaccess 配置为重定向众所周知的扩展名,例如 .html .asp。 aspx .shtml 等到非扩展的url,并获取.php文件显示为内容。

我的 .htaccess 文件如下所示:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# php extension to no extension url
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]


# no extension to the .php file
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

# this is for the index
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

【问题讨论】:

    标签: php html apache .htaccess redirect


    【解决方案1】:

    你可以试试这样的:

    # php extension to no extension url
    RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
    RewriteRule ^(.+)\.[a-zA-Z0-9]+$ $1 [R=301,L]
    

    那么它不仅会重定向.php 分机,还会重定向任何人。要使用它,请确保您的 URL 末尾没有带点和字母数字字符串。

    【讨论】:

      猜你喜欢
      • 2021-09-09
      • 1970-01-01
      • 2012-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-25
      • 2012-10-29
      • 1970-01-01
      相关资源
      最近更新 更多