【问题标题】:Redirect urls with .php to urls without .php extension (.htaccess)将带有 .php 的 url 重定向到没有 .php 扩展名的 url (.htaccess)
【发布时间】:2017-06-09 13:11:21
【问题描述】:

Duplicate

使用 .htaccess 的重定向不会将带有 .php 的链接重定向到不带 .php 的链接

.htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
</IfModule>

# END WordPress

即使这样我也可以看到 example.com/example.php 重定向到 404。

有什么想法或建议吗?

【问题讨论】:

    标签: apache .htaccess redirect mod-rewrite


    【解决方案1】:

    您必须更改指令的顺序。使用:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    
    # browser requests PHP
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
    RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
    
    # check to see if the request is for a PHP file:
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^/?(.*)$ /$1.php [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    

    【讨论】:

      猜你喜欢
      • 2012-10-24
      • 1970-01-01
      • 2021-09-09
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      • 2012-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多